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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧