更好的动态导航菜单

时间:2012-03-08 20:48:38

标签: php algorithm navigation

我现在有这种方法:

    ctop = $cnew = $cmine = '';
    if($actual == 'top') $ctop = 'class="active"';
    if($actual == 'last') $new = 'class="active"';
    if($actual == 'mine') $cmine = 'class="active"';
    $html = '<aside class="panel_derecho">
                <div class="tabs"><h4>$donde</h4>
                    <ul>';
                                $js = "refrescar_submenu('last_$donde')";
                                $js_t = "refrescar_submenu('top_$donde')";
                                $js_r = "refrescar_submenu('mine_$donde')";
                        $html .= '<li '.$ctop.'><a href="#sub_menu" onclick="'.$js_t.'">top</a></li>';
                        $html .= '<li '.$cnew.'><a href="#sub_menu" onclick="'.$js.'">ultimos</a></li>';
                        $html .= '<li '.$mine.'><a href="#sub_menu" onclick="'.$js_r.'">like</a></li>';
   $html .= '       </ul>
            </aside>';
    return $html;

按预期工作:

它生成一个列表,其中包含所需的副本,所需的javascript函数参数和活动类(对于想要的副本)

但我觉得它可能不那么重复;我已经可以看到添加/编辑/删除copys,params,元素等会很昂贵。我只是不知道在哪里开始..

如果有帮助:

  1. $donde代表数据类型(文章,歌曲,视频......)
  2. $actual代表一个属性(新文章,热门文章, 我喜欢的文章)

2 个答案:

答案 0 :(得分:2)

// Menu: link => text
$menu = array(
     "top" => "top",
     "last" => "ultimos",
     "mine" => "like"
);

$html = '<aside class="panel_derecho">
            <div class="tabs"><h4>$donde</h4>
                <ul>';
foreach ($menu as $link => $text)
{
    $html .= '<li '.( $link==$actual ? 'class="active"' : '').'><a href="#sub_menu" onclick="refrescar_submenu(\''.$link.'_'.$donde.'\')">'.$text.'</a></li>';
}
$html .= '       </ul>
        </aside>';
return $html;

答案 1 :(得分:2)

    $attributes = array("top" => "top", "last" => "ultimos", "mine" => "like");
    $html = "<aside class=\"panel_derecho\">
    <div class=\"tabs\"><h4>{$donde}</h4>
        <ul>";
    foreach ($attributes as $key=>$value)
        $html .= "          <li ". ($actual == $key ? "class=\"active\"" : "") ."><a href=\"#sub_menu\" onclick=\"refrescar_submenu('{$key}_{$donde}'\">{$value}</a></li>";
   $html .= "       </ul>
   </div>
</aside>";

    return $html;