dongpang1232 2017-11-06 10:14
浏览 146

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条回答 默认 最新

  • dongsha9208 2017-11-06 10:19
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题