[英]Get a mysqli row display under data in json
我有一個看起來像這樣的mysqli表:
在此表之外,我正在創建一個Json-File以便以后在其他Files中使用它。
這是我的代碼:
<?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": []
}, {......
我無法回答這個問題,我想在代碼中更改什么,或者是否必須更改表中的內容才能正確顯示...。
希望你理解我在做什么
問候阿明
也許這就是您想要的:
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時都會變成對象。
請不要從靜態函數中調用構造函數。 僅在創建實際對象實例時運行。 同樣,一個人只需要一個遞歸循環來拾取遞歸下降的子代。
編輯:啊,然后要將該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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.