【问题标题】:Bootstrap treeview not fetching all rows from database引导树视图未从数据库中获取所有行
【发布时间】: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


    【解决方案1】:

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

    作为一个建议,我认为最好将 js 从 php 中分离出来并尝试首先调试 php 并查看你得到的值。在你知道你得到之前使用 js 中的数据是错误的你需要什么。

    还可以在查询中使用不带 LIMIT 的行计数函数来查看在执行任何其他操作之前获得了多少结果。

    【讨论】:

    • 我没有在 php 上添加该脚本,我添加了 html 文件只是为了显示,我尝试使用 print_r() 打印来自 db 的所有行。
    • 所以如果你做一个 var_dump(json_encode($data)) 你有所有的数据吗?
    • 是的 var_dump 给出结果(但不显示在 html 页面上)如果我添加 mysqli_set_charset($conn, "utf8"); 不给出 sql LIMIT,如果不是 var_dump 给出 bool(false)
    【解决方案2】:

    我遇到过这样的情况,下面是如何处理的:

    json_encode(array_values($data));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-28
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 2014-09-24
      相关资源
      最近更新 更多