-2

I'm not getting it done, I need to put a class on the output of the function.

Can you help, when the menu item is active there has to be put a class to the link.

This is the code:

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>";

         }  
     }
ckpepper02
  • 3,207
  • 5
  • 27
  • 43

3 Answers3

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>";

    }   
}

Also your

beiller
  • 3,075
  • 1
  • 10
  • 17
  • Hi Beiller, thanks, but this one is not working for me. It ads nothing to the active link. – user2001054 May 30 '13 at 19:03
  • $row['active'] === true -- that needs to be swapped with however you determine if a link is 'active' – beiller May 30 '13 at 19:04
  • thats the issue where i'm trying to getting a sollution for... How can i determine when a link is active – user2001054 May 30 '13 at 19:05
  • Its impossible for me to answer that without knowing more. You could say something like $thispagename == $row['page_name'] – beiller May 30 '13 at 19:07
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>";
Community
  • 1
  • 1
Charles Forest
  • 1,014
  • 1
  • 10
  • 29
0

Thanks beiller for the help..

This is the function now:

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>";
}}

This is the call:

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

And mysql has the following tables:

  • page_id
  • page_name
  • link_name
  • site_id
  • order_id
  • page_menu
  • page_inmenu
  • page_leftmenu
  • page_text
  • page_active
Charles Forest
  • 1,014
  • 1
  • 10
  • 29