-2

我没有完成它,我需要在函数的输出上放置一个类。

你能帮忙吗,当菜单项active存在时,必须为链接添加一个类。

这是代码:

function loadMenu()
      {
     $result = mysql_query(" SELECT * FROM  cms_page WHERE  site_id =2  AND  page_inmenu =1 AND  page_active =1");
    if ($DEBUG)
         echo "<pre>$result</pre>";
    while($row = mysql_fetch_array($result))
    {   $last_parent = $row['page_name']; 
        echo "<a href={$row['link_name']}>{$row['page_name']}</a>";

         }  
     }
4

3 回答 3

2
function loadMenu()
{
    $result = mysql_query(" SELECT * FROM  cms_page WHERE  site_id =2  AND  page_inmenu =1 AND  page_active =1");
    if ($DEBUG)
        echo "<pre>$result</pre>";
    while($row = mysql_fetch_array($result))
    {
        $last_parent = $row['page_name']; 
        $class = $row['active'] === true ? ' class="active"' : '';
        echo "<a href=\"{$row['link_name']}\"{$class}>{$row['page_name']}</a>";

    }   
}

还有你的

于 2013-05-30T18:59:02.087 回答
0

I'm not 100% sure about what's your issue.

So, if you aren't displaying anything, probably a MySQL issue.

1- Reference for Mysql (Look at OZ_'s answer) : How to put mysql inside a php function?

2- As mentionned by Beiller, you'll need a way to see if the link is active.

Solution A : Might need some adjustment according $row['link_name'] value.

    $class = ($row['link_name'] == $_SERVER['REQUEST_URI'].)?' class="active" ' : '';

Solution B : Simply stick to CSS - http://www.echoecho.com/csslinks.htm

    .mylink:active{ background-color:pink; }

3- Href needs quotes on around the URL. also, I'm not a big fan of {} inside strings.

    echo "<a href=\"". $row['link_name'] ."\"". $class .">". $row['page_name'] ."</a>";
于 2013-05-30T19:11:48.367 回答
0

感谢贝勒的帮助..

这是现在的功能:

function loadMenu(){
     $result = mysql_query(" SELECT * FROM  cms_page WHERE  site_id =2  AND  page_inmenu =1 AND  page_active =1");
    if ($DEBUG)
         echo "<pre>$result</pre>";
    while($row = mysql_fetch_array($result))
    {   $last_parent = $row['page_name']; 
        echo "<a href={$row['link_name']}>{$row['page_name']}</a>";
}}

这是电话:

<div id="header_menu">
           <? loadMenu (); ?>
</div>

mysql有以下表格:

  • page_id
  • page_name
  • 链接名称
  • site_id
  • order_id
  • page_menu
  • page_inmenu
  • page_leftmenu
  • page_text
  • page_active
于 2013-05-30T19:13:21.327 回答