1

这是 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>
4

1 回答 1

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 的第一个子伪元素

我看你用while的很多。我不怪你,这就是 90% 的 PHP 指南(以及 PHP.net 上的示例代码)的工作方式。但是,这并不理想,因为:

  1. 它依赖于难以理解且笨拙的Internal Row Pointer,可以说是最不直观的数组导航方法,以及
  2. 结果你不得不写很多可笑的臃肿之类的$MainSubNav_items=$cliao->fetchResult($SubNav_result)

让您的生活更轻松。首先将数据转换为数组,然后使用 或 访问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 :)
}
于 2012-11-13T20:13:12.783 回答