动态菜单= Php数据库驱动

时间:2012-11-13 20:06:06

标签: php menu database-driven

这是一个定制&数据库驱动的PHP5 5.3.10无序列表脚本 如您所见,此方法来自程序背景。

大约1岁,今天我想再写一次,这个脚本的数据只来自1个SQL表;由dao类和多个公共函数触发。

这段代码按预期工作,但对我来说看起来有点难看,你怎么想?如果你再次写这篇文章,你会从哪里开始呢?

基本上脚本会产生这个html:

<div class="menuInside" id="menu">
    <div>
        <ul class="">
            <li class="first"><a onclick="this.blur()" id="bienvenue" href="/bienvenue.html"><img width="109" height="19" alt="Bienvenue" src="/images/menu/bienvenue.png"></a></li>
            <li class="text"><a href="/bienvenue.html">Nous sommes heureux de vous accueillir sur notre site Internet.</a></li>
        </ul>
    </div>
    <div>
        <ul class="insidemiddle ulactive">
            <li class="second"><a onclick="this.blur()" id="marques" href="/lingerie-feminine.html"><img width="102" height="23" alt="Marques" src="/images/menu/marques.png"></a></li>
            <li class="elements"><a href="/barbara.html">Barbara</a></li>
            <li class="elements"><a href="/calida.html">Calida</a></li>
            <li class="elements"><a href="/canat.html">Canat</a></li>
            <li class="elements"><a href="/chantelle.html">Chantelle</a><img width="9" height="9" class="last" alt="" src="/images/menu/menu_item.png"></li>
            <li class="elements"><a href="/charmline-lidea.html">Charmline &amp; Lidea</a></li>
            <li class="elements"><a href="/eminence.html">Eminence</a><img width="9" height="9" class="last" alt="" src="/images/menu/menu_item.png"></li>
            <li class="elements"><a href="/empreinte.html">Empreinte</a></li>
            <li class="elementsOne"><a href="/lisanza.html">Lisanza</a></li>
            <li class="elements"><a href="/lise-charmel.html">Lise Charmel</a><img width="9" height="9" class="last" alt="" src="/images/menu/menu_item.png"></li>
            <li class="elements"><a href="/lou.html">Lou</a></li>
            <li class="elementsThree"><a href="/marjolaine.html">Marjolaine</a></li>
            <li class="elements"><a href="/pain-de-sucre.html">Pain de Sucre</a><img width="9" height="9" class="last" alt="" src="/images/menu/menu_item.png"></li>
            <li class="elements"><a href="/rcrescentini.html">R. Crescentini</a></li>
            <li class="elements"><a class="active" href="/save-the-queen.html">Save The Queen</a><img width="9" height="9" class="last" alt="" src="/images/menu/menu_item.png"></li>
            <li class="elements"><a href="/secret-eva.html">Secret d'Eva</a></li>
            <li class="elements"><a href="/trasparenze.html">Trasparenze</a></li>
            <li class="elements"><a href="/triumph.html">Triumph</a><img width="9" height="9" class="last" alt="" src="/images/menu/menu_item.png"></li>
            <li class="elements"><a href="/twin-set.html">Twin Set</a></li>
        </ul>
    </div>
    <div>
        <ul class="insidemiddle">
            <li class="first"><a onclick="this.blur()" id="lingerie" href="/lingerie-feminine.html"><img width="88" height="21" alt="Lingerie" src="/images/menu/lingerie.png"></a></li>
            <li class="elementsOne"><a href="/lingerie-feminine.html">Féminin</a></li>
            <li class="elements"><a href="/lingerie-masculine.html">Masculin</a><img width="9" height="9" class="last" alt="" src="/images/menu/menu_item.png"></li>
            <li class="elementsTwo"><a href="/lingerie-de-nuit.html">Linge de Nuit</a></li>
            <li class="elements"><a href="/articles-balneaires.html">Articles Balnéaires</a><img width="9" height="9" class="last" alt="" src="/images/menu/menu_item.png"></li>
            <li class="elementsNine"><a href="/collants-bas-mi_bas-chaussettes.html">Collants, Bas, Mi-bas, Chaussettes...</a><img width="9" height="9" class="last" alt="" src="/images/menu/menu_item.png"></li>
            <li class="elementsTen"><a href="/laine-et-soie.html">Laine et Soie</a><img width="9" height="9" class="last" alt="" src="/images/menu/menu_item.png"></li>
        </ul>
    </div>
    <div>
        <ul class="">
            <li class="first"><a onclick="this.blur()" id="contact" href="/contact.html"><img width="106" height="19" alt="Contact" src="/images/menu/contact.png"></a></li>
            <li class="text"><a href="/contact.html">Si vous souhaitez nous faire part de vos impressions ou pour tout autre commentaire...<br>
                <br>
                Nos horaires d'ouvertures</a></li>
        </ul>
    </div>
</div>

你想指出我正确的方向吗?

这是脚本:

<?php
$id=isset($_GET['id'])?$_GET['id']:1;
($id!=1)?$classNav='menuInside':$classNav='menu';
?>

<div id="menu" class="<?=$classNav?>"><?php $cliao=new cliao();
    $ActiveNav_result=$cliao->SubNav(array(" display_block"=>'=1'," id_marque"=>'='.$id),"id_menu",1);
    $ActiveNav_item=$cliao->fetchResult($ActiveNav_result);
    $Nav_result=$cliao->Nav(array(" display_block"=>'=1'),"id_menu");
    while($MainNav_items=$cliao->fetchResult($Nav_result)){$SubNav_result=$cliao->SubNav(array(" display_block"=>"=1"," id_menu"=>'='.$MainNav_items->id_menu),"id_menu",1);
        if($classNav!="menu"){($MainNav_items->Nav_type==1)?$class='insidemiddle':$class='';
            ($ActiveNav_item->id_Nav==$MainNav_items->id_menu)?$class.=' ulactive':$class.='';
            echo '<div><ul class="'. $class .'">'."\n";
        }else{
            ($MainNav_items->Nav_type==1)?$class='ulactive middle':$class='ulactive';
                echo '<div><ul class="'.$class.'">'."\n";
        }
    while($MainSubNav_items=$cliao->fetchResult($SubNav_result)){
        ($MainNav_items->class_block==2)?$firstclass='class="first"': $firstclass='class="second"';
        echo ($MainNav_items->display_block==1)?'<li '.$firstclass.'><a href="'.$MainNav_items->href_link.'" id="'.$MainNav_items->name.'" onclick="this.blur()"><img src="/images/menu/'.$MainNav_items->name.'.png" alt="'.ucfirst($MainNav_items->name).'" width="'.$MainNav_items->image_width.'" height="'.$MainNav_items->image_height.'" /></a></li>'."\n":'';
        }
        $SubNav_result=$cliao->SubNav(array(" display_block"=>"=1"," id_menu"=>'='.$MainNav_items->id_Nav),"id_menu");
    while($MainSubNav_items=$cliao->fetchResult($SubNav_result)){($MainSubNav_items->Nav_last=="yes")?$last='<img src="/images/menu/menu_item.png" alt="" width="9" height="9" class="last" />':$last='';
        ($MainSubNav_items->id_marque==$id)?$activeclass='class="active"':$activeclass='';
?>
        <li class="<?=$MainSubNav_items->Nav_class?>"><a href="<?=$MainSubNav_items->href_link?>" <?=$activeclass?>><?=$MainSubNav_items->text?></a><?=$last?></li><?="\n"?><?php }echo '</ul></div>'."\n";
    }
?>
</div>

1 个答案:

答案 0 :(得分:0)

我始终确保尽可能将数据库内容绑定到生成的HTML中。例如,而不是:

if($classNav!="menu"){($MainNav_items->Nav_type==1)?$class='insidemiddle':$class='';
        ($ActiveNav_item->id_Nav==$MainNav_items->id_menu)?$class.=' ulactive':$class.='';
        echo '<div><ul class="'. $class .'">'."\n";
    }else{
        ($MainNav_items->Nav_type==1)?$class='ulactive middle':$class='ulactive';
            echo '<div><ul class="'.$class.'">'."\n";
    }

我将所需的最终$ClassNav存储在数据库中并按如下方式编写:

echo '<ul class='$ClassNav'>\n';

我也会跳过凌乱的class='first'废话并尽可能使用CSS first child pseudoelements

我发现你经常使用while。我不怪你,这就是90%的PHP指南(以及PHP.net上的示例代码)做的事情。但是,它并不理想,因为:

  1. 它依赖于难以理解和笨拙的Internal Row Pointer,可以说是导航数组最不直观的方法,
  2. 因此你必须写出很多荒谬的臃肿,如$MainSubNav_items=$cliao->fetchResult($SubNav_result)
  3. 让您的生活更轻松。首先将数据转换为数组,然后使用foreach ($data as $record) { $out .= $record['fieldname']; }for ($i = 0; $i < sizeOf($record); $i++) { $out .= $record[$i]['fieldname']; }访问它。

    在该比较中,foreach更简洁,而for则为您提供了更多流量控制权限。

    最后,考虑将其重写为递归函数:通过列表呈现的函数,以及当遇到子菜单时,调用自身以生成下一级别。更容易,更强大。 :)

    举个例子,这是我在我的(内部)面向对象工具包中使用的菜单生成代码:

    public function Render($Menu = false){ 
        $Menu = (is_array($Menu) ? $Menu : $this->MainMenu); // If no menu array is passed, pull the main menu object from the class constructor
        $Out = "<ul class='SuuiMenu'>"; //building an $Out string instead of echoing is preferable as the resulting function can be used in more contexts
    
        foreach ($Menu as $Entry){
            if ($Entry['Link'] !== '#'){ //I use a link value of # for submenu headers
                $Entry['Link'] = base_url().$Entry['Link'];
            }
            $Out .= "
                <li>
                    <a href='{$Entry['Link']}'>{$Entry['Title']}</a>";
                if (isset($Entry['Children'])){ //if this menu entry has 'Children' in its array, this method calls itself with that child's array as an argument and adds the whole thing to $Out
                    $Out .= $this->Render($Entry['Children'], false);
                }   
            $Out .="</li>
            ";
        }
    
        $Out .= "</ul>";
    
        return $Out; //to be echoed, modified further in PHP-DOM, cached, etc :)
    }