我有一个mysqli表,如下所示:

在这个表中,我正在创建一个Json文件,以便稍后在其他文件中使用该文件。
这是我的密码:
<?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": []
}, {........现在,我希望将行"vlink“显示为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": []
}, {......我不能给出答案,我必须修改代码,或者如果我必须改变表中的什么东西,才能正确地显示这个.
希望你能理解我想做的事
阿明问候
发布于 2016-10-28 16:26:17
也许这就是你想要的
while ($category = mysqli_fetch_object($result, 'Category')) {
$vlink = array('vlink' => $category->vlink);
unset($category->vlink);
$category->data = $vlink;
$categories[] = $category;
}这将从row对象中删除vlink属性,将其替换为包含作为对象的vlink的data属性。它实际上是一个PHP关联数组,但是当编码为JSON时,关联数组和对象都会变成对象。
发布于 2016-10-28 16:46:28
请不要从静态函数调用构造函数。它仅在创建实际对象实例时运行。另外,一个只需要一个递归循环就可以获得递归下降的子级。
编辑:啊,要将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;
}
}https://stackoverflow.com/questions/40308700
复制相似问题