0

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\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
        });
    }
});
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Sam
  • 195
  • 1
  • 18

2 Answers2

1

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.

Cosmin_Victor
  • 154
  • 1
  • 13
  • i did not added that script on php, i have added on html file only to display, and i have tried with print_r() its printing all the rows from db. – Sam Nov 06 '17 at 10:27
  • So if you do a var_dump(json_encode($data)) you have all the data coming? – Cosmin_Victor Nov 06 '17 at 12:05
  • yes var_dump giving results (but not displaying on html page) without giving sql LIMIT if i add mysqli_set_charset($conn, "utf8");, if not var_dump giving bool(false) – Sam Nov 07 '17 at 04:58
0

I have encountered such a case, and here is how to handle it:

json_encode(array_values($data));
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68