我正在尝试从函数发送JSON数组。
我的代码是:
function categoryTree($parent_id = 0, $sub_mark = ''){
global $connection;
$query = 'SELECT * FROM ws_categories WHERE parent_id = :parent_id ORDER BY sort_order ASC, name ASC';
$statement = $connection->prepare($query);
$statement->execute(['parent_id' => $parent_id]);
while ($row = $statement->fetch()) {
echo ''.$sub_mark.''.$row['name'].'';
categoryTree($row['category_id'], $sub_mark.'– ');
}
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $filtered_rows,
"recordsFiltered" => get_total_all_records(),
"data" => categoryTree()
);
echo json_encode($output);
我不确定我的代码在哪里错误。只是尝试从categoryTree()收集所有数据,然后通过JSON发送数据,但没有成功。
答案 0 :(得分:0)
只需尝试这样的操作,但再没有成功:
function categoryTree($parent_id = 0, $sub_mark = ''){
global $connection;
$query = 'SELECT * FROM ws_categories WHERE parent_id = :parent_id ORDER BY sort_order ASC, name ASC';
$statement = $connection->prepare($query);
$statement->execute(['parent_id' => $parent_id]);
while ($row = $statement->fetch()) {
$sub_array = array();
$sub_array[] = $row['name'];
$data[] = $sub_array;
categoryTree($row['category_id'], $sub_mark.'– ');
return $data;
}
}
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $filtered_rows,
"recordsFiltered" => get_total_all_records(),
"data" => $data
);
echo json_encode($output);
答案 1 :(得分:0)
现在向我显示类别,但仅显示父类别而不显示子类别:
$output = array();
function categoryTree($parent_id = 0, $sub_mark = ''){
global $connection;
$query = 'SELECT * FROM ws_categories WHERE parent_id = :parent_id ORDER BY sort_order ASC, name ASC';
$statement = $connection->prepare($query);
$statement->execute(['parent_id' => $parent_id]);
$filtered_rows = $statement->rowCount();
while ($row = $statement->fetch()) {
$sub_array = array();
$sub_array[] = $sub_mark.$row['name'];
$sub_array[] = '<button type="button" name="update" id="'.$row["category_id"].'" class="btn btn-info btn-sm update"><span class="glyphicon glyphicon-pencil"></span></button>';
$array[] = $sub_array;
categoryTree($row['category_id'], $sub_mark.'– ');
}
return $array;
}
$data = categoryTree();
$output = array(
"draw" => intval($_POST["draw"]),
"recordsTotal" => $filtered_rows,
"recordsFiltered" => get_total_all_records(),
"data" => $data
);
echo json_encode($output);
我确定这已经完成了,但是我不知道我错过了什么。