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