doubei2231 2015-03-19 08:39
浏览 30
已采纳

PHP菜单:用于父项的循环

I'm trying to work my way with PHP and create a PHP menu that uses items stored in database and a PHP code to construct the menu items in the right order. The values on the database can be changed so i want to keep the parent items exported first then the nested items added below.

I want to use a loop (do-while? i guess) to get the data from the database so i can get the menu arranged correctly with the nested items and outputted with a class that I want to use.

My database table stores the title and url for each item and the nested items contain the id of the parent items, the parent items has id 0 set as parent and it looks like this:

  1. --------------------
  2. | id | int(11) |
  3. --------------------
  4. | title | varchar |
  5. --------------------
  6. | url | varchar |
  7. --------------------
  8. | parent | int(11) |
  9. --------------------

Here is my PHP code:

  1. try {
  2. $stm = $db->prepare('SELECT * FROM menu');
  3. $stm->execute();
  4. $result = $stmt->fetchAll();
  5. // requested loop / function
  6. }

The output should look like this:

  1. $menu = menu::create()
  2. ->add('Homepage', '/', menu::create()
  3. ->add('Item1', '/item1/', menu::create()
  4. ->add('Subitem1', '/subitem1/')
  5. ->add('Subitem1', '/subitem1/')
  6. ->add('Item2', '/item2/', menu::create()
  7. ->add('Subitem3', '/subitem3/')
  8. ->add('Subitem4', '/subitem4/')

Please suggest how to get this output with the database items... Thank you.

展开全部

  • 写回答

1条回答 默认 最新

  • douhao7677 2015-03-19 09:48
    关注

    There must be some adjustments to do but I hope this will give you a lead

    1. function sort_parent($parent)
    2. {
    3. $j=0;
    4. $flag = true;
    5. $temp=0;
    6. while ( $flag )
    7. {
    8. $flag = false;
    9. for( $j=0; $j < count($parent)-1; $j++)
    10. {
    11. if ($parent[$j]->id > $parent[$j+1]->id)
    12. {
    13. $temp = $parent[$j]->id;
    14. $parent[$j] = $parent[$j+1]->id;
    15. $parent[$j+1]->id = $temp;
    16. $flag = true;
    17. }
    18. }
    19. }
    20. return ($parent);
    21. }
    22. function sort_children($children)
    23. {
    24. $j=0;
    25. $flag = true;
    26. $temp=0;
    27. while ( $flag )
    28. {
    29. $flag = false;
    30. for( $j=0; $j < count($children)-1; $j++)
    31. {
    32. if ($children[$j]->parent > $children[$j+1]->parent)
    33. {
    34. $temp = $children[$j]->parent;
    35. $children[$j] = $children[$j+1]->parent;
    36. $children[$j+1]->parent = $temp;
    37. $flag = true;
    38. }
    39. }
    40. }
    41. return ($children);
    42. }
    43. try {
    44. //First we fetch the parents
    45. $stm = $db->prepare('SELECT * FROM menu WHERE parent = 0');
    46. $stm->execute();
    47. $parent = $stmt->fetchAll();
    48. // Then the childrens. Note: In some versions of SQL this operator may be written as !=
    49. $stm = $db->prepare('SELECT * FROM menu WHERE parent <> 0');
    50. $stm->execute();
    51. $children = $stmt->fetchAll();
    52. }
    53. // We sort parent and children with a basic buble sort
    54. $parent = sort_parent($parent);
    55. $children = sort_object($children);
    56. // To finish as Parent and Children are in the same order we can do that
    57. foreach ($parent as $parent_menu_item) {
    58. $menu = menu::create() -> add($parent_menu_item->title, $parent_menu_item->url);
    59. for ($j=0; $children[$i]->parent == $parent_menu_item->id; j++)
    60. {
    61. $menu = menu::create() -> add($children[$j]->title, $children[$j]->url);
    62. }
    63. }

    Does it help ?

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部