Bootstrap treeview无法从数据库

时间:2017-11-06 10:14:01

标签: php json twitter-bootstrap mysqli treeview

我的mysql数据库中有超过2000行( arabic 中的文本),我正在使用bootstrap treeview pulgin将所有行作为树视图中的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
        });
    }
});

2 个答案:

答案 0 :(得分:1)

注意:这不是解决方案,但由于50以下的声誉,我无法在帖子上添加评论。

正如一个建议我认为如果你从php中删除js并尝试首先调试php并查看你得到的值更好。在你知道你得到之前使用js中的数据是错误的你需要什么。

在查询中使用不带LIMIT的行计数功能,以查看在执行任何其他操作之前获得的结果数。

答案 1 :(得分:0)

我遇到过这种情况,下面是处理方法:

json_encode(array_values($data));