数据表,Php 使用 where 子句的过滤器不起作用

datatable, Php filter using where clause not working

我将此脚本用于数据table。

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

将显示 table 中的所有值,但我需要

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

如果我应用 where 子句 那么数据table 将不会显示任何内容。

是不是因为where子句已经被isset使用了? 我如何根据 child_place.

制作过滤器

谢谢



    <?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);

?>

很简单。只需像这样将您的条件添加到代码中

<?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);

?>