我有一个mysqli表,看起来像这样:
从表中我开始创建一个Json-File,以便稍后在其他文件中使用它。这是我的代码:
<?php
$categories = Category::getTopCategories();
$categories = array_values($categories);
echo json_encode($categories);
class Category
{
/**
* The information stored in the database for each category
*/
public $id;
public $parent;
public $name;
public $vlink;
// The child categories
public $children;
public function __construct()
{
// Get the child categories when we get this category
$this->getChildCategories();
}
/**
* Get the child categories
* @return array
*/
public function getChildCategories()
{
if ($this->children) {
return $this->children;
}
return $this->children = self::getCategories("parent = {$this->id}");
}
////////////////////////////////////////////////////////////////////////////
/**
* The top-level categories (i.e. no parent)
* @return array
*/
public static function getTopCategories()
{
return self::getCategories('parent = 0');
}
/**
* Get categories from the database.
* @param string $where Conditions for the returned rows to meet
* @return array
*/
public static function getCategories($where = '')
{
if ($where) $where = " WHERE $where";
$conn=mysqli_connect('localhost', 'root', '','praktikum');
mysqli_set_charset($conn,"UTF8");
$sql = "SELECT id,name,parent,vlink FROM mdl_praktikum$where";
$result = mysqli_query($conn,$sql);
$categories = array();
while ($category = mysqli_fetch_object($result, 'Category'))
$categories[] = $category;
mysqli_free_result($result);
return $categories;
}
}
?>
这是我的结果的片段:
[{
"id": "1"
, "parent": "0"
, "name": "root"
, "vlink": "0" //this is wrong
, "children": [{
"id": "2"
, "parent": "1"
, "name": "Verschiedenes"
, "vlink": "0"
, "children": []
}, {
"id": "3"
, "parent": "1"
, "name": "EBSN"
, "vlink": "0"
, "children": [{
"id": "6"
, "parent": "3"
, "name": "EBSN 1415"
, "vlink": "0"
, "children": [{
"id": "23"
, "parent": "6"
, "name": "General Information"
, "vlink": "0"
, "children": [{
"id": "208"
, "parent": "23"
, "name": "03-05 SocialMediaMining3"
, "vlink": "198"
, "children": []
}, {
"id": "209"
, "parent": "23"
, "name": "06-08 Business Model Blocks - Relationship"
, "vlink": "221"
, "children": []
}, {........
现在我想要一行&#34; vlink&#34;显示为json文件的数据对象:
[{
"id": "1"
, "parent": "0"
, "name": "root"
, "data": {
, "data": {"vlink": "0"} // This should be standing here
, "children": [{
"id": "2"
, "parent": "1"
, "name": "Verschiedenes"
, "data": { "vlink": "0"}
, "children": []
}, {
"id": "3"
, "parent": "1"
, "name": "EBSN"
, "data": { "vlink": "0"}
, "children": [{
"id": "6"
, "parent": "3"
, "name": "EBSN 1415"
, "data": { "vlink": "0"}
, "children": [{
"id": "23"
, "parent": "6"
, "name": "General Information"
, "data": { "vlink": "0"}
, "children": [{
"id": "208"
, "parent": "23"
, "name": "03-05 SocialMediaMining3"
, "data": { "vlink": "198"}
, "children": []
}, {
"id": "209"
, "parent": "23"
, "name": "06-08 Business Model Blocks - Relationship"
, "data": { "vlink": "221"}
, "children": []
}, {......
我无法得到答案,我需要在代码中更改内容,或者我是否必须更改表格中的内容才能正确显示....
希望你明白我要做的事情
问候阿明
答案 0 :(得分:0)
也许这就是你想要的:
while ($category = mysqli_fetch_object($result, 'Category')) {
$vlink = array('vlink' => $category->vlink);
unset($category->vlink);
$category->data = $vlink;
$categories[] = $category;
}
这将从行对象中删除vlink
属性,将其替换为包含vlink作为对象的data
属性。它实际上是一个PHP关联数组,但是当编码为JSON时,关联数组和对象都会变成对象。
答案 1 :(得分:0)
请不要从静态函数调用构造函数。这仅在创建实际对象实例时运行。此外,只需要一个递归循环来获取递归递减的子节点。
编辑:啊,并将该vlink移动到数据元素中,复制并取消设置。
$categories = Category::getCategories();
echo '<pre>'.json_encode($categories);
class Category
{
/**
* The information stored in the database for each category
*/
public $id;
public $parent;
public $name;
public $vlink;
// The child categories
public $children;
/**
* Get categories from the database.
* @param string $where Conditions for the returned rows to meet
* @return array
*/
public static function getCategories($parent_id='0')
{
$parent_id = (integer) $parent_id;
$conn=mysqli_connect('localhost', 'root', '','praktikum');
mysqli_set_charset($conn,"UTF8");
$sql = "SELECT id, name, parent, vlink FROM mdl_praktikum WHERE parent = $parent_id";
$result = mysqli_query($conn,$sql);
$categories = array();
while ($category = mysqli_fetch_object($result, 'Category')) {
$category->children = Self::getCategories($category->id);
$category->data = array('vlink'=>$category->vlink);
unset($category->vlink);
$categories[] = $category;
}
mysqli_free_result($result);
return $categories;
}
}