php - 带有来自 mysql 的数组的动态菜单

标签 php mysql

谁能帮我动态重建这个?

<?php
    error_reporting ( E_ALL );
    $menu = array 
    (
        1 =>    array 
                (
                    'text'      =>  'Articles',
                    'class'     =>  'articles',
                    'link'      =>  '#',
                    'show_condition'=>  TRUE,
                    'parent'    =>  0
                ),
        2 =>    array
                (
                    'text'      =>  'Users',
                    'class'     =>  'users',
                    'link'      =>  '#',
                    'show_condition'=>  TRUE,
                    'parent'    =>  0
                ),
        3 =>    array
                (
                    'text'      =>  'Groups',
                    'class'     =>  'groups',
                    'link'      =>  '#',
                    'show_condition'=>  TRUE,
                    'parent'    =>  0
                ),
        4 =>    array
                (
                    'text'      =>  'Settings',
                    'class'     =>  'settings',
                    'link'      =>  '#',
                    'show_condition'=>  TRUE,
                    'parent'    =>  0
                ),
        5 =>    array
                (
                    'text'      =>  'Add new',
                    'class'     =>  'add_article',
                    'link'      =>  '#',
                    'show_condition'=>  TRUE,
                    'parent'    =>  1
                ),
        6 =>    array
                (
                    'text'      =>  'Categories',
                    'class'     =>  'categories',
                    'link'      =>  '#',
                    'show_condition'=>  TRUE,
                    'parent'    =>  1
                ),
        7 =>    array
                (
                    'text'      =>  'Add new',
                    'class'     =>  'add_user',
                    'link'      =>  '#',
                    'show_condition'=>  TRUE,
                    'parent'    =>  2
                ),
        8 =>    array
                (
                    'text'      =>  'Delete',
                    'class'     =>  'delete',
                    'link'      =>  '#',
                    'show_condition'=>  TRUE,
                    'parent'    =>  1
                ),
        9 =>    array
                (
                    'text'      =>  'Show',
                    'class'     =>  'show',
                    'link'      =>  '#',
                    'show_condition'=>  TRUE,
                    'parent'    =>  2
                ),
        10 =>   array
                (
                    'text'      =>  'Last created',
                    'class'     =>  'last',
                    'link'      =>  '#',
                    'show_condition'=>  TRUE,
                    'parent'    =>  9
                ),
        11 =>   array
                (
                    'text'      =>  'First created',
                    'class'     =>  'first',
                    'link'      =>  '#',
                    'show_condition'=>  TRUE,
                    'parent'    =>  9
                ),
        12 =>   array
                (
                    'text'      =>  'All',
                    'class'     =>  'all',
                    'link'      =>  '#',
                    'show_condition'=>  TRUE,
                    'parent'    =>  9
                ),
        13 =>   array
                (
                    'text'      =>  'None',
                    'class'     =>  'none',
                    'link'      =>  '#',
                    'show_condition'=>  TRUE,
                    'parent'    =>  9
                )
    );  

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

        for ( $i = 1; $i <= count ( $menu ); $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>' . "\n";
                       }
                    }
                    else 
                    {
                      die ( sprintf ( 'menu nr %s must be an array', $i ) );
                    }
                  }

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

    function get_childs ( $menu, $el_id )
    {
        $has_subcats = FALSE;
        $out = '';
        $out .= "\n".'  <ul>' . "\n";
        for ( $i = 1; $i <= count ( $menu ); $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>' . "\n";
                    }
                 }

        $out .= '   </ul>'."\n";
        return ( $has_subcats ) ? $out : FALSE;
    }

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Dynamic PHP/CSS menu by roScripts</title>
    <link href="css/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div style="width:700px;margin:100px auto">

       <h2>Dynamic PHP/CSS menu by <a href="http://www.roscripts.com" title="programming articles and tutorials" target="_blank">roScripts</a></h2>
        <?= build_menu ( $menu ) ?>
    </div>
</body>
</html>

我的数据库:

mysql> describe menuSystem;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| id        | int(11)      | NO   | PRI | NULL    |       |
| title     | varchar(50)  | NO   |     | NULL    |       |
| class     | varchar(30)  | NO   |     | NULL    |       |
| link_url  | varchar(100) | NO   |     | NULL    |       |
| parent_id | int(11)      | NO   |     | 0       |       |
| show      | varchar(6)   | NO   |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

我正在尝试这个,但不起作用:

$sql = "SELECT * FROM menuSystem ORDER BY id ASC";
    $res = mysql_query($sql) or die (mysql_error());

            if(mysql_num_rows($res) != 0) {
                    while($row = mysql_fetch_assoc($res)) {
                            $id = mysql_real_escape_string ($row['id']);
                            $title = mysql_real_escape_string ($row['title']);
                            $class = mysql_real_escape_string ($row['class']);
                            $link_url = mysql_real_escape_string ($row['link_url']);
                            $parent_id = mysql_real_escape_string ($row['parent_id']);
                            $show = mysql_real_escape_string ($row['show']);
    $menu = array   (
            "$id" =>        array
                            (
                                    'text'          =>      "$title",
                                    'class'         =>      "$class",
                                    'link'          =>      "$link_url",
                                    'show_condition'=>      "$show",
                                    'parent'        =>      "$parent_id"
                            )
                    );

                    }

            }

最佳答案

您好,这可能不是您问题的确切答案,但我之前做过类似的事情,所以可能会有所帮助!基本上它是一个基于初始父 ID 构建站点树的递归循环,因此也许您可以看一下并稍微修改一下

/**
* build_site_tree
*
* @return void
* @author Mike Waites
**/
public function build_site_tree($parent_id)
{
    return $this->find_children($parent_id);
}

/** end build_site_tree **/

// -----------------------------------------------------------------------

/**
* find_children
* Recursive loop to find parent=>child relationships
*
* @return array $children
* @author Mike Waites
**/
public function find_children($parent_id)
{
    $this->benchmark->mark('find_children_start');

    if(!class_exists('Account_model'))
        $this->load->model('Account_model');

    $children = $this->Account_model->get_children($parent_id);

    /** Recursively Loop over the results to build the site tree **/
    foreach($children as $key => $child)
    {
        $childs = $this->find_children($child['id']);

        if (count($childs) > 0)
            $children[$key]['children'] = $childs;
    }

    return $children;

    $this->benchmark->mark('find_children_end');
 }

 /** end find_children **/

如您所见,它非常基础,只是为了演示这个想法

关于php - 带有来自 mysql 的数组的动态菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6315917/

相关文章:

MySQL 如果查询没有返回任何结果,则尝试第二个查询

MySQL 带有额外子句的左连接返回不匹配的行

php - 创建 SQL 语句通过比较一行中的两个字段来查询 MySQL 数据库

php - 无法在 CakePHP 中编辑关于 hasOne 关系的其他表信息

php - 将字符串传递到具有类型提示的方法时出错

php - 找不到稳定性稳定的包/laravel/laravel

java - 我如何使用这个 sql 查询来 hibernate 条件函数

MySQL:如何组合从另一个表多次引用的列并分配适当的别名?

javascript - 我的表单提交和 jquery 问题

php - Paypal 数字商品 : How to get item number after purchase made?