PHP 菜单:用于 parent 项目的循环
PHP Menu: which loop to use for parent items
我正在尝试使用 PHP 并创建一个 PHP 菜单,该菜单使用存储在数据库中的项目和一个 PHP 代码来构建右侧的菜单项命令。数据库中的值可以更改,所以我想先导出 parent 项,然后在下面添加嵌套项。
我想使用循环(do-while?我猜)从数据库中获取数据,这样我就可以使用嵌套项正确排列菜单并输出 class我要用。
我的数据库 table 存储每个项目的标题和 url 并且嵌套项目包含 parent 项目的 ID,parent 项目的 ID 为 0设置为 parent,它看起来像这样:
--------------------
| id | int(11) |
--------------------
| title | varchar |
--------------------
| url | varchar |
--------------------
| parent | int(11) |
--------------------
这是我的 PHP 代码:
try {
$stm = $db->prepare('SELECT * FROM menu');
$stm->execute();
$result = $stmt->fetchAll();
// requested loop / function
}
输出应如下所示:
$menu = menu::create()
->add('Homepage', '/', menu::create()
->add('Item1', '/item1/', menu::create()
->add('Subitem1', '/subitem1/')
->add('Subitem1', '/subitem1/')
->add('Item2', '/item2/', menu::create()
->add('Subitem3', '/subitem3/')
->add('Subitem4', '/subitem4/')
请建议如何使用数据库项获取此输出...
谢谢你。
必须进行一些调整,但我希望这能给您带来指导
function sort_parent($parent)
{
$j=0;
$flag = true;
$temp=0;
while ( $flag )
{
$flag = false;
for( $j=0; $j < count($parent)-1; $j++)
{
if ($parent[$j]->id > $parent[$j+1]->id)
{
$temp = $parent[$j]->id;
$parent[$j] = $parent[$j+1]->id;
$parent[$j+1]->id = $temp;
$flag = true;
}
}
}
return ($parent);
}
function sort_children($children)
{
$j=0;
$flag = true;
$temp=0;
while ( $flag )
{
$flag = false;
for( $j=0; $j < count($children)-1; $j++)
{
if ($children[$j]->parent > $children[$j+1]->parent)
{
$temp = $children[$j]->parent;
$children[$j] = $children[$j+1]->parent;
$children[$j+1]->parent = $temp;
$flag = true;
}
}
}
return ($children);
}
try {
//First we fetch the parents
$stm = $db->prepare('SELECT * FROM menu WHERE parent = 0');
$stm->execute();
$parent = $stmt->fetchAll();
// Then the childrens. Note: In some versions of SQL this operator may be written as !=
$stm = $db->prepare('SELECT * FROM menu WHERE parent <> 0');
$stm->execute();
$children = $stmt->fetchAll();
}
// We sort parent and children with a basic buble sort
$parent = sort_parent($parent);
$children = sort_object($children);
// To finish as Parent and Children are in the same order we can do that
foreach ($parent as $parent_menu_item) {
$menu = menu::create() -> add($parent_menu_item->title, $parent_menu_item->url);
for ($j=0; $children[$i]->parent == $parent_menu_item->id; j++)
{
$menu = menu::create() -> add($children[$j]->title, $children[$j]->url);
}
}
有帮助吗?
我正在尝试使用 PHP 并创建一个 PHP 菜单,该菜单使用存储在数据库中的项目和一个 PHP 代码来构建右侧的菜单项命令。数据库中的值可以更改,所以我想先导出 parent 项,然后在下面添加嵌套项。
我想使用循环(do-while?我猜)从数据库中获取数据,这样我就可以使用嵌套项正确排列菜单并输出 class我要用。
我的数据库 table 存储每个项目的标题和 url 并且嵌套项目包含 parent 项目的 ID,parent 项目的 ID 为 0设置为 parent,它看起来像这样:
--------------------
| id | int(11) |
--------------------
| title | varchar |
--------------------
| url | varchar |
--------------------
| parent | int(11) |
--------------------
这是我的 PHP 代码:
try {
$stm = $db->prepare('SELECT * FROM menu');
$stm->execute();
$result = $stmt->fetchAll();
// requested loop / function
}
输出应如下所示:
$menu = menu::create()
->add('Homepage', '/', menu::create()
->add('Item1', '/item1/', menu::create()
->add('Subitem1', '/subitem1/')
->add('Subitem1', '/subitem1/')
->add('Item2', '/item2/', menu::create()
->add('Subitem3', '/subitem3/')
->add('Subitem4', '/subitem4/')
请建议如何使用数据库项获取此输出... 谢谢你。
必须进行一些调整,但我希望这能给您带来指导
function sort_parent($parent)
{
$j=0;
$flag = true;
$temp=0;
while ( $flag )
{
$flag = false;
for( $j=0; $j < count($parent)-1; $j++)
{
if ($parent[$j]->id > $parent[$j+1]->id)
{
$temp = $parent[$j]->id;
$parent[$j] = $parent[$j+1]->id;
$parent[$j+1]->id = $temp;
$flag = true;
}
}
}
return ($parent);
}
function sort_children($children)
{
$j=0;
$flag = true;
$temp=0;
while ( $flag )
{
$flag = false;
for( $j=0; $j < count($children)-1; $j++)
{
if ($children[$j]->parent > $children[$j+1]->parent)
{
$temp = $children[$j]->parent;
$children[$j] = $children[$j+1]->parent;
$children[$j+1]->parent = $temp;
$flag = true;
}
}
}
return ($children);
}
try {
//First we fetch the parents
$stm = $db->prepare('SELECT * FROM menu WHERE parent = 0');
$stm->execute();
$parent = $stmt->fetchAll();
// Then the childrens. Note: In some versions of SQL this operator may be written as !=
$stm = $db->prepare('SELECT * FROM menu WHERE parent <> 0');
$stm->execute();
$children = $stmt->fetchAll();
}
// We sort parent and children with a basic buble sort
$parent = sort_parent($parent);
$children = sort_object($children);
// To finish as Parent and Children are in the same order we can do that
foreach ($parent as $parent_menu_item) {
$menu = menu::create() -> add($parent_menu_item->title, $parent_menu_item->url);
for ($j=0; $children[$i]->parent == $parent_menu_item->id; j++)
{
$menu = menu::create() -> add($children[$j]->title, $children[$j]->url);
}
}
有帮助吗?