dreamice2013 2009-12-11 10:17
浏览 24

从sql查询创建php数组

I'm trying to create an array of arrays so I can build a dynamic menu, but I'm getting lost amongst the code. My output looks like this:

$menu = Array ( 
        [0] => Array ( 
            [text] => Home 
            [class] => 875 
            [link] => //Home 
            [show_condition] => TRUE 
            [parent] => 0 
            ) 
        [1] => Array ( 
            [text] => About 
            [class] => 326 
            [link] => //About 
            [show_condition] => TRUE 
            [parent] => 0 
            ) 
         etc 
         etc 
         etc       
        [339] => Array ( 
            [text] => Planner 
            [class] => 921 
            [link] => //Planner 
            [show_condition] => TRUE 
            [parent] => 45 
            ) 
    ) 

And the two functions which should build the menu are:

    function build_menu ( $menu )   {
            $out = '<div class="container4">' . "
";
            $out .= '   <div class="menu4">' . "
";
            $out .= "
".'<ul>' . "
";

            for ( $i = 1; $i <= count ( $menu )-1; $i++ )
            {

if ( is_array ( $menu [ $i ] ) ) {//must be by construction but let's keep the errors home
                    if ( $menu [ $i ] [ 'show_condition' ] && $menu [ $i ] [ 'parent' ] == 0 ) {//are we allowed to see this menu?
                        $out .= '<li class="' . $menu [ $i ] [ 'class' ] . '"><a href="' . $menu [ $i ] [ 'link' ] . '">';
                        $out .= $menu [ $i ] [ 'text' ];
                        $out .= '</a>';
                        $out .= get_childs ( $menu, $i );
                        $out .= '</li>' . "
";
                    }
                }
                else {
                    die ( sprintf ( 'menu nr %s must be an array', $i ) );
                }
            }

            $out .= '</ul>'."
";
            $out .= "
\t" . '</div>';
            return $out . "
\t" . '</div>';
        }

    function get_childs ( $menu, $el_id )   {
            $has_subcats = FALSE;
            $out = '';
            $out .= "
".'  <ul>' . "
";
            for ( $i = 1; $i <= count ( $menu )-1; $i++ )
            {

                if ( $menu [ $i ] [ 'show_condition' ] && $menu [ $i ] [ 'parent' ] == $el_id ) {//are we allowed to see this menu?
                    $has_subcats = TRUE;
                    $add_class = ( get_childs ( $menu, $i ) != FALSE ) ? ' subsubl' : '';
                    $out .= '       <li class="' . $menu [ $i ] [ 'class' ] . $add_class . '"><a href="' . $menu [ $i ] [ 'link' ] . '">';
                    $out .= $menu [ $i ] [ 'text' ];
                    $out .= '</a>';
                    $out .= get_childs ( $menu, $i );
                    $out .= '</li>' . "
";
                }
            }
            $out .= '   </ul>'."
";
            return ( $has_subcats ) ? $out : FALSE;
        }

But the menu is refusing to show any submenu levels - it only displays top level. Any ideas?

Thanks!

  • 写回答

3条回答 默认 最新

  • duanpanhuo0618 2009-12-11 10:21
    关注

    Your code is almost there - you may want to change mysql_fetch_array to mysql_fetch_assoc, and you can convert values as returned into the appropriate types using functions like intval:

    $menu = array();
    $sql = "SELECT TabName as text, TabID as class, TabPath as link, IsVisible as show_condition, ParentId as parent FROM dnn_SMA_Tabs WHERE PortalID = 3 AND IsVisible = 'True' ORDER BY TabOrder ASC";
    $result = mysql_query($sql);
    $index = 1;
    while($row = mysql_fetch_assoc($result)) {
        $row['parent'] = intval($row['parent']);
        $menu[$index] = $row;
        $index++;
    }
    

    You'll need to convert show_condition to the appropriate type - how to do that probably depends on what column type IsVisible is.

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教