我从数据库查询中得到以下数组 我想创建一个树视图,父视图是' win_name'而且孩子是' item_name'。在过去的几天里,我尝试了foreach和while循环的组合,这个论坛中的其他回复,在线教程,jquery插件和jstree。我似乎无法让它发挥作用
parent =" win_name"
--child =" item_name"
array(857) {
[0]=>
object(stdClass)#11 (5) {
["item_name"]=>
string(6) "$rocks"
["item_id"]=>
string(4) "3045"
["win_name"]=>
string(12) "Tequila Mods"
["win_id"]=>
string(1) "3"
["sales_mode"]=>
string(1) "1"
}
[1]=>
object(stdClass)#24 (5) {
["item_name"]=>
string(13) "Queso Fundido"
["item_id"]=>
string(4) "1101"
["win_name"]=>
string(6) "Snacks"
["win_id"]=>
string(2) "29"
["sales_mode"]=>
string(1) "1"
}
[2]=>
object(stdClass)#25 (5) {
["item_name"]=>
string(9) "Texaz Dip"
["item_id"]=>
string(4) "1102"
["win_name"]=>
string(6) "Snacks"
["win_id"]=>
string(2) "29"
["sales_mode"]=>
string(1) "1"
}
[3]=>
object(stdClass)#26 (5) {
["item_name"]=>
string(10) "Summer Sea"
["item_id"]=>
string(4) "1481"
["win_name"]=>
string(6) "Snacks"
["win_id"]=>
string(2) "29"
["sales_mode"]=>
string(1) "1"
}
答案 0 :(得分:0)
<?php
class treeView
{
public $treeview = array();
// In the construct, we transform your current array to an array that we can use
function __construct($main)
{
$main = (array) $main;
foreach($main as $view)
{
// We add the parent ONLY if we don't already have it
$this->treeview['parent'][$view['win_name']]['win_name'] = $view['win_name'];
$this->treeview['parent'][$view['win_name']]['win_id'] = $view['win_id'];
$this->treeview['parent'][$view['win_name']]['sales_mode'] = $view['sales_mode'];
// Adding all of the childs
$this->treeview['child'][$view['win_name']][$view['item_id']]['item_name'] = $view['item_name'];
$this->treeview['child'][$view['win_name']][$view['item_id']]['item_id'] = $view['item_id'];
}
}
public function buildTree()
{
// Looping through each parent and outputting it's content and it's children
foreach($this->treeview['parent'] as $branch)
{
if(isset($this->treeview['child'][$branch['win_name']]))
{
echo "<ol>";
echo "<li>Win Name: {$branch['win_name']}</li>";
echo "<li>Win ID: {$branch['win_id']}</li>";
echo "<li>Sales Mode: {$branch['sales_mode']}</li>";
$this->buildChild($branch['win_name']); // Our call which builds the children
echo "</ol>";
}
else
{
echo "<ol>";
echo "<li>Win Name: {$branch['win_name']}</li>";
echo "<li>Win ID: {$branch['win_id']}</li>";
echo "<li>Sales Mode: {$branch['sales_mode']}</li>";
echo "</ol>";
}
}
}
private function buildChild($parent)
{
// Looping through each child... technically we could make this recursive and allow children to have children
foreach($this->treeview['child'][$parent] as $child)
{
echo "<li style=\"list-style-type: none;\">";
echo "<ol>";
echo "<li>Item Name: {$child['item_name']}</li>";
echo "<li>Item ID: {$child['item_id']}</li>";
echo "</ol>";
echo "</li>";
}
}
}
$array = array (
array (
'item_name' => "\$rocks",
'item_id' => "3045",
'win_name' => "Tequila Mods",
'win_id' => "3",
'sales_mode' => "1",
),
array (
'item_name' => "Queso Fundido",
'item_id' => "1101",
'win_name' => "Snacks",
'win_id' => "29",
'sales_mode' => "1",
),
array (
'item_name' => "Texaz Dip",
'item_id' => "1102",
'win_name' => "Snacks",
'win_id' => "29",
'sales_mode' => "1",
),
array (
'item_name' => "Summer Sea",
'item_id' => "1481",
'win_name' => "Snacks",
'win_id' => "29",
'sales_mode' => "1",
)
);
// Initiating our class
$tree = new treeView($array);
// Building our tree
$tree->buildTree();
?>
这将生成以下列表
<ol><li>Win Name: Tequila Mods</li><li>Win ID: 3</li><li>Sales Mode: 1</li><li style="list-style-type: none;"><ol><li>Item Name: $rocks</li><li>Item ID: 3045</li></ol></li></ol><ol><li>Win Name: Snacks</li><li>Win ID: 29</li><li>Sales Mode: 1</li><li style="list-style-type: none;"><ol><li>Item Name: Queso Fundido</li><li>Item ID: 1101</li></ol></li><li style="list-style-type: none;"><ol><li>Item Name: Texaz Dip</li><li>Item ID: 1102</li></ol></li><li style="list-style-type: none;"><ol><li>Item Name: Summer Sea</li><li>Item ID: 1481</li></ol></li></ol>
说实话,这很脏又丑。我使用这个代码来处理你的数组,但这不是最好的方法。更好的方法是使用PDO,并使用FETCH_GROUP按win_name (the parent)
进行分组,并以此方式获取所需的数组。
学习Veerendra's solution'谢谢ficuscr '并且可能尝试根据您的要求实施类似的系统,这不会是坏事。