【发布时间】:2017-11-06 10:14:01
【问题描述】:
我的 mysql 数据库中有超过 2000 行(arabic 中的文本),我正在使用 bootstrap treeview pulgin 来获取所有行作为 treeview 中的 json 节点。在 chrome 控制台中,它给出错误消息“无法设置未定义的属性 'nodeId'”。 from this question answers 如果我在我的 sql 中使用 LIMIT 618 查询它的工作。我添加了 mysqli_set_charset() 仍然无法获取所有行。我在下面添加了我的两个页面代码,任何建议表示赞赏。
request.php
<?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);
index.html
$(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
});
}
});
【问题讨论】:
标签: php json twitter-bootstrap treeview