Search code examples
phpmysqldatatable

datatable, Php filter using where clause not working


I use this script for datatable.

$query = "SELECT * FROM tbl_child "; enter code here

will show all the value in the table but i need

$query = "SELECT * FROM tbl_child where child_place='us' ";

if i apply where clause then datatable will not show anything.

is it because the where clause is already used by the isset? how can i make a filter based on child_place.

Thanks



    <?php
$column = array("childpid", "child_name", "child_house", "child_gender", "child_age", "child_place");

$query = "SELECT * FROM tbl_child ";

if(isset($_POST["search"]["value"]))

{

 $query .= '
 WHERE child_name LIKE "%'.$_POST["search"]["value"].'%" 

 OR child_house LIKE "%'.$_POST["search"]["value"].'%" 

 OR child_gender LIKE "%'.$_POST["search"]["value"].'%" 

 OR child_age LIKE "%'.$_POST["search"]["value"].'%" 

 OR child_place LIKE "%'.$_POST["search"]["value"].'%"
 
 ';

}

if(isset($_POST["order"]))

{

 $query .= 'ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';

}
else
{
 $query .= 'ORDER BY childpid DESC ';
}
$query1 = '';

if($_POST["length"] != -1)

{

 $query1 = 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];

}


$statement = $pdo->prepare($query);

$statement->execute();


$number_filter_row = $statement->rowCount();


$statement = $pdo->prepare($query . $query1);

$statement->execute();

$result = $statement->fetchAll();

$data = array();

foreach($result as $row)

{

 $sub_array = array();

 $sub_array[] = $row['childpid'];

 $sub_array[] = $row['child_name'];

 $sub_array[] = $row['child_house'];

 $sub_array[] = $row['child_gender'];

 $sub_array[] = $row['child_age'];

 $sub_array[] = $row['child_place'];


 $data[] = $sub_array;

}

function count_all_data($pdo)

{

 $query = "SELECT * FROM tbl_child ";

 $statement = $pdo->prepare($query);

 $statement->execute();

 return $statement->rowCount();

}

$output = array(

 'draw'   => intval($_POST['draw']),

 'recordsTotal' => count_all_data($pdo),

 'recordsFiltered' => $number_filter_row,

 'data'   => $data
);

echo json_encode($output);

?>

Solution

  • it is very simple. just add your condition to the code like this

    <?php
    $column = array("childpid", "child_name", "child_house", "child_gender", "child_age", "child_place");
    
    $query = "SELECT * FROM tbl_child ";
    
    if(isset($_POST["search"]["value"]))
    
    {
    
     $query .= '
     WHERE (child_name LIKE "%'.$_POST["search"]["value"].'%" 
    
     OR child_house LIKE "%'.$_POST["search"]["value"].'%" 
    
     OR child_gender LIKE "%'.$_POST["search"]["value"].'%" 
    
     OR child_age LIKE "%'.$_POST["search"]["value"].'%" 
    
     OR child_place LIKE "%'.$_POST["search"]["value"].'%")
     
     AND child_place = "us"
     ';
    
    }else{
     $query .= '
      WHERE child_place ="us" ';
    }
    
    if(isset($_POST["order"]))
    
    {
    
     $query .= 'ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
    
    }
    else
    {
     $query .= 'ORDER BY childpid DESC ';
    }
    $query1 = '';
    
    if($_POST["length"] != -1)
    
    {
    
     $query1 = 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
    
    }
    
    
    $statement = $pdo->prepare($query);
    
    $statement->execute();
    
    
    $number_filter_row = $statement->rowCount();
    
    
    $statement = $pdo->prepare($query . $query1);
    
    $statement->execute();
    
    $result = $statement->fetchAll();
    
    $data = array();
    
    foreach($result as $row)
    
    {
    
     $sub_array = array();
    
     $sub_array[] = $row['childpid'];
    
     $sub_array[] = $row['child_name'];
    
     $sub_array[] = $row['child_house'];
    
     $sub_array[] = $row['child_gender'];
    
     $sub_array[] = $row['child_age'];
    
     $sub_array[] = $row['child_place'];
    
    
     $data[] = $sub_array;
    
    }
    
    function count_all_data($pdo)
    
    {
    
     $query = "SELECT * FROM tbl_child ";
    
     $statement = $pdo->prepare($query);
    
     $statement->execute();
    
     return $statement->rowCount();
    
    }
    
    $output = array(
    
     'draw'   => intval($_POST['draw']),
    
     'recordsTotal' => count_all_data($pdo),
    
     'recordsFiltered' => $number_filter_row,
    
     'data'   => $data
    );
    
    echo json_encode($output);
    
    ?>