【问题标题】:"No matching records found" NOT displayed in "bootstrap-table"“未找到匹配记录”未显示在“引导表”中
【发布时间】:2015-09-14 09:04:25
【问题描述】:

我正在为我的网站使用“bootstrap-table”(https://github.com/wenzhixin/bootstrap-table) 插件,目前正在使用它的服务器端分页属性。

表格已正确填充,搜索功能也有效。 但是当搜索不存在的东西时,它会显示所有记录而不是显示“未找到匹配记录”。

这是我正在使用的 html 代码...

<table data-toggle="table"
       data-url="1.php"
       data-pagination="true"
       data-side-pagination="server"
       data-page-list="[5, 10, 20, 50, 100, 200]"
       data-search="true"
       data-height="300">
    <thead>
    <tr>
        <th data-field="state" data-checkbox="true"></th>
        <th data-field="memberID" data-align="right" data-sortable="true">Member ID</th>
        <th data-field="name" data-align="center" data-sortable="true"> Name</th>
        <th data-field="dob" data-sortable="true">Date of Birth</th>
    </tr>
    </thead>


</table>

这是我用来创建 JSON 响应的 php 脚本...

<?php

require_once('db-connect.php');

if(isset($_GET["limit"])) {
    $limit = $_GET["limit"];
} else {
    $limit = 10;
}

if(isset($_GET["offset"])) {
    $offset = $_GET["offset"];
} else {
    $offset = 0;
}

if(isset($_GET["sort"])) {
    $sort = $_GET["sort"];
} else {
    $sort = "";
}

if(isset($_GET["order"])) {
    $order = $_GET["order"];
} else {
    $order = "asc";
}

 if(isset($_GET["search"])) {
    $search = $_GET["search"];
 } else {
    $search = "";
 }


if($search == "") {
    $result = mysqli_query($connection,"select memberID, name, dob from memberdetails" );
} else {

    $result = mysqli_query($connection,"select memberID, name, dob from memberdetails WHERE memberID LIKE '%$search%'"  );
}


$row = array();

if ( mysqli_num_rows($result) > 0 ) {
            while($row = mysqli_fetch_assoc($result)) {
                $result_2d_arr[] = array (  'memberID' => $row['memberID'],
                                            'name' => $row['name'],
                                            'dob' => $row['dob']);
            }




//get the result size
$count = sizeof($result_2d_arr);

//order the array
if($order != "asc") {
    $result_2d_arr = array_reverse($result_2d_arr);
}

//get the subview of the array
$result_2d_arr = array_slice($result_2d_arr, $offset, $limit);



echo "{";
echo '"total": ' . $count . ',';
echo '"rows": ';
echo json_encode($result_2d_arr);
echo "}";

}

?>

JSON 响应如下...

{"total": 23,"rows": [{"memberID":"1","name":"asd","dob":"2015-06-03"},{"memberID":"2","name":"asd","dob":"2015-06-03"},{"memberID":"3","name":"asd","dob":"2015-06-03"},{"memberID":"4","name":"asd","dob":"2015-06-03"},{"memberID":"5","name":"asd","dob":"2015-06-03"},{"memberID":"6","name":"asd","dob":"2015-06-03"},{"memberID":"7","name":"asd","dob":"2015-06-03"},{"memberID":"8","name":"asd","dob":"2015-06-03"},{"memberID":"9","name":"asd","dob":"2015-06-03"},{"memberID":"10","name":"asd","dob":"2015-06-03"}]}

【问题讨论】:

  • 当您的表中没有搜索到的条目时,您检查过 num_rows 吗?如果返回 0,则为 if ( mysqli_num_rows($result) &gt; 0 ) { .. } else { echo "No results found";} 添加 else 条件并将值传递给您的 html 文件
  • 我假设 memberID 将始终是一个整数,我不会在搜索整数时使用 LIKE '%$var%',因为 ID 1 将匹配 11 或 101 我猜这是问题所在,因为您的字段 memberID 可能分配为整数,如果您打算使用 LIKE 运算符,则使用 WHERE CAST(memberID AS TEXT) LIKE '%$search%' 可能很有用。我只需切换到 = 运算符并专门搜索 ID
  • 感谢您的回复。我确实尝试了上述两种方法,但结果保持不变。我一直在参考这些文档来编写我的大部分代码。 linklink希望这能让问题更清楚

标签: php json twitter-bootstrap bootstrap-table


【解决方案1】:

终于……让它工作了。我把这段代码放在else部分。

else {
$count=0;
echo "{";
echo '"total": ' . $count . ',';
echo '"rows": ';
echo json_encode(array());
echo "}";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多