Php 引导树视图未从数据库获取所有行
我的mysql数据库中有2000多行(阿拉伯语文本),我使用引导treeview pulgin将所有行作为treeview中的json节点获取。在chrome控制台中,给出错误消息“无法设置未定义的属性'nodeId'。如果我在sql查询中使用LIMIT 618,它将正常工作。我添加了mysqli_set_charset(),但仍然无法获取所有行。我已经在下面添加了我的两个页面代码,任何建议都将不胜感激 request.phpPhp 引导树视图未从数据库获取所有行,php,json,twitter-bootstrap,treeview,Php,Json,Twitter Bootstrap,Treeview,我的mysql数据库中有2000多行(阿拉伯语文本),我使用引导treeview pulgin将所有行作为treeview中的json节点获取。在chrome控制台中,给出错误消息“无法设置未定义的属性'nodeId'。如果我在sql查询中使用LIMIT 618,它将正常工作。我添加了mysqli_set_charset(),但仍然无法获取所有行。我已经在下面添加了我的两个页面代码,任何建议都将不胜感激 request.php <?php error_reporting(E_ALL); i
<?php
error_reporting(E_ALL); ini_set('display_errors', 1); // check if there is any error
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbganem";
$data =array();
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
mysqli_set_charset($conn, "utf8");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sql = "SELECT * FROM books LIMIT 618";
$results = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
while($row = mysqli_fetch_assoc($results) ) {
$tmp = array();
$tmp['id'] = $row['id'];
$tmp['parent_id'] = $row['parent_id'];
$tmp['name'] = $row['name'];
$tmp['text'] = $row['name'];
$tmp['type'] = $row['type'];
$tmp['description'] = $row['description'];
$tmp['path'] = $row['path'];
$tmp['order_display'] = $row['order_display'];
$tmp['has_pages'] = $row['has_pages'];
array_push($data, $tmp);
}
$itemsByReference = array();
// Build array of item references:
foreach($data as $key => &$item) {
$itemsByReference[$item['id']] = &$item;
// Children array:
$itemsByReference[$item['id']]['nodes'] = array();
}
// Set items as children of the relevant parent item.
foreach($data as $key => &$item) {
//echo "<pre>";print_r($itemsByReference[$item['parent_id']]);die;
if($item['parent_id'] && isset($itemsByReference[$item['parent_id']])) {
$itemsByReference [$item['parent_id']]['nodes'][] = &$item;
}
}
// Remove items that were added to parents elsewhere:
foreach($data as $key => &$item) {
if(empty($item['nodes'])) {
unset($item['nodes']);
}
if($item['parent_id'] && isset($itemsByReference[$item['parent_id']])) {
unset($data[$key]);
}
}
// Encode:
echo json_encode($data);
注意:这不是一个解决方案,但我不能在帖子上添加评论,因为声誉低于50 作为一个建议,我认为最好是将js从php中分离出来,并尝试首先调试php,看看得到了什么值。在知道得到了需要的数据之前使用js中的数据是错误的
在执行任何其他操作之前,还可以在查询中不受限制地使用行计数函数查看得到的结果数。我遇到过这种情况,下面是如何处理它:
我没有在php上添加该脚本,我在html文件上添加该脚本只是为了显示,我尝试使用print_r()打印数据库中的所有行。因此,如果您执行var_转储(json_encode($data)),您就有了所有数据?是的,如果我添加mysqli_set_charset($conn,“utf8”),var_转储会给出结果(但不会在html页面上显示),而不会给出sql限制;, 如果不是var_转储,则给出bool(false)
$(document).ready(function () {
var treeData;
$.ajax({
type: "POST", //or GET
url: "request.php",
dataType: "json",
success: function (response) {
initTree(response)
}
});
function initTree(treeData) {
$('#received_data').treeview({
data: treeData
});
}
});
json_encode(array_values($data));