Bootstrap treeview不从数据库中获取所有行

i have more than 2000 rows in my mysql db (text in arabic), i'm using bootstrap treeview pulgin to fetch all rows as json nodes in treeview. in chrome console its giving error message "Cannot set property 'nodeId' of undefined". from this question answers if i use LIMIT 618 in my sql query its working. i have added mysqli_set_charset() still could not get all rows. I have added my both page codes below, any advice appreciated.

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
", 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
        });
    }
});

1个回答



注意:这不是解决方案,但由于声誉低于50,我无法在帖子上添加评论。</ p>

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

在查询中使用没有LIMIT的行计数功能来查看在执行任何其他操作之前你得到了多少结果。</ p>
</ DIV>

展开原文

原文

NOTE: This is not a solution but i can't add a comment on the post because of reputation under 50.

Just as a suggestion i think it is better if you issolate the js from the php and try to first debug the php and see what values you get.It is wrong to use the data in the js before you know you are getting what you need.

Also use the row count function without the LIMIT in the query to see how many results you get before performing any other operations.

doujiaoang69440
doujiaoang69440 是的var_dump给出结果(但不显示在html页面上),如果我添加mysqli_set_charset($ conn,“utf8”);而不给出sql LIMIT,如果不是var_dump给出bool(false)
2 年多之前 回复
dongxizhe9755
dongxizhe9755 那么如果你做一个var_dump(json_encode($ data))你会得到所有的数据吗?
2 年多之前 回复
douduo2407
douduo2407 我没有在PHP上添加该脚本,我已经在html文件中添加了仅显示,并且我已经尝试使用print_r()从db打印所有行。
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问