darksoul90
darksoul90

Reputation: 145

how to add a class to one dynamic menu item?

php:

$sql = "SELECT id, menuname
        FROM table
        WHERE status = 'active'
        ORDER BY order ASC";
$result = mysql_query($sql);
$menu = "<ul>\n";
while ($row = mysql_fetch_assoc($result)) {
    $menu.= "<li><a href=\"?id={$row['id']}\">{$row['menuname']}</a></li>";
}
$menu.= "</ul>\n";

html:

<nav>
    <ul>
        <li><a href="?id=1">About</a></li>
        <li><a href="?id=2">News</a></li>
        <li><a href="?id=3">Prices</a></li>
        <li><a href="?id=4">Gallery</a></li>
        <li><a href="?id=5">Contact</a></li>
    </ul>
</nav>

I'd like to add the class="lhidden" to the "News" (not the 2nd menu,because the order can be changed).

<li class="lhidden"><a href="?id=2">News</a></li>

How can i do that? I'm just learning javascript and php,so I have no idea :S

Upvotes: 0

Views: 124

Answers (3)

Mahmoud Mamdouh
Mahmoud Mamdouh

Reputation: 29

   $sql = "SELECT id, menuname
   FROM table
   WHERE status = 'active'
   ORDER BY order ASC";
   $result = mysql_query($sql);
   $menu = "<ul>\n";
   while ($row = mysql_fetch_assoc($result)) {

   if($row['id']==2)
   {  
       $menu.= "<li class=\"lhidden\"><a href=\"?id={$row['id']}\">{$row['menuname']}</a></li>";
   }
   else
   {
       $menu.= "<li><a href=\"?id={$row['id']}\">{$row['menuname']}</a></li>";
   }
   $menu.= "</ul>\n";

Upvotes: 0

Musa
Musa

Reputation: 97717

How about

while ($row = mysql_fetch_assoc($result)) {
    $menu.= "<li><a".($row['menuname']=='News'?' class="lhidden"':'')." href=\"?id={$row['id']}\">{$row['menuname']}</a></li>";
}

Upvotes: 2

jtheman
jtheman

Reputation: 7491

Using jQuery is pretty easy:

$('a[href="?id=2"]').parent().addClass('lhidden');

This would first find the specific anchor, then add your class to its parent.

Documentation for the selector part here: http://api.jquery.com/attribute-equals-selector/

Upvotes: 0

Related Questions