簡體   English   中英

從SQL查詢創建PHP數組

[英]Creating php arrays from sql queries

我試圖創建一個數組數組,以便可以構建一個動態菜單,但是我在代碼中迷路了。 我的輸出如下所示:

$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 
            ) 
    ) 

並且應該建立菜單的兩個功能是:

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

            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>' . "\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 )-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>' . "\n";
                }
            }
            $out .= '   </ul>'."\n";
            return ( $has_subcats ) ? $out : FALSE;
        }

但是菜單拒絕顯示任何子菜單級別-它僅顯示頂層。 有任何想法嗎?

謝謝!

您的代碼幾乎在那里-您可能需要將mysql_fetch_array更改為mysql_fetch_assoc ,並且可以使用諸如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++;
}

您需要將show_condition轉換為適當的類型-如何執行此操作可能取決於IsVisible的列類型。

我看到您的數組的索引從[0]到[399]

數組(

 [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 ) ) 

但您嘗試顯示從[1]到[340]的項目

對於($ i = 1; $ i <= count($ menu); $ i ++)

count($ menu)返回340([0]-> [399])

解決方案:for($ i = 0; $ i <count($ menu); $ i ++)

從0開始直到399(嚴格<340)

我會以另一種方式做到這一點:面向對象。

class Menu {
  private $children = array();
  private $name = '';
  private $link = '';
  private $class = '';
  private $show = TRUE;

  function __construct($name, $class, $link, $show = TRUE, $parent = null) {
    $this->name = $name;
    $this->link = $link;
    $this->class = $class;
    $this->show = $show;
    if(!is_null($parent)) {
        $parent->addChild($this);
    }
  }

  function addChild(Menu $child) {
      $this->children[] = $child;
  }

  // ... remaining getters (and probably setters)
}

然后,您可以構建如下菜單:

$home = new Menu('Home', '875', '//Home');
$about = new Menu('About', '326', '//About');

//...

$planner = new Menu('Planner', '921', '//Planner', true, $home);

$menu = array($home, $about,...);

這只是一個例子。 我知道這意味着您將創建340個變量來保存菜單。 使用其他setter和getter方法,您可以做得更好,這只是一個快速的“草繪”。

您可以按以下方式構建菜單:

function build_menu ( $menu, $showContainer = false)   {
    $out = '';
    if($showContainer) {
        $out = '<div class="container4">' . "\n";
        $out .= '       <div class="menu4">' . "\n";              
    }

    if(!empty($menu)) {
        $out .= '<ul>' . "\n";

        for ($entry in  $menu) {
            if($entry->getShow()) {//are we allowed to see this menu?
                 $out .= '<li class="' . $entry->getClass() . '"><a href="' . $entry->getLink() . '">';
                 $out .= $entry->getText();
                 $out .= '</a>';
                 $out .= "\n" . build_menu($entry->getChildren());
                 $out .= '</li>' . "\n";
             }
        }
        $out .= '</ul>'."\n";
    }
    if($showContainer) {
        $out .= "\n\t" . '</div>';
        $out .= "\n\t" . '</div>';                
    }
    return $out;
}

我沒有測試代碼,但這是其背后的想法。 如果您沒有OOP和php的經驗,請查看官方文檔

還要注意,這需要PHP5。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM