2011-06-11 3 views
1

아무도 동적으로 다시 작성하는 방법을 도울 수 있습니까 ??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" 
          ) 
        ); 

        } 

      } 
+0

"역동적 인"의미를 설명해 주시겠습니까? 그리고 * 코드를 다시 포맷하십시오. '{}'버튼을 사용하여 일부를 포맷하거나 이것을 읽을 수 있습니다 : http://stackoverflow.com/editing-help. 불필요한 추가 개행과 모두 제거하십시오. 질문 아래에서 어떻게 보는지 알 수 있습니다. – Nanne

+0

데이터베이스의 모든 메뉴 항목을 표시해야합니다. 나는 이것을 시도하고 있지만 fail (http://pastebin.com/Z7fRftiJ), while out of statment는 데이터베이스로부터의 마지막 resoul을 echo합니다. – Stefan

답변

1

HI가이 질문에 대한 정확한 답변을하지 않을 수 있지만, 비슷한 도움이 될 수 있기 전에 비슷한 일을했습니다! 기본적으로 초기 부모 ID를 기반으로 사이트 트리의 구축에 자사의 recurssive 루프는 어쩌면 당신은 살펴보고 그냥 아이디어를 입증 할 그것의 아주 기본적인 알 수 있듯이 그것은 조금

/** 
* 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 **/ 

을 수정할 수 있습니다