Author Topic: Active state for dynamic created subnav  (Read 350 times)

0 Members and 1 Guest are viewing this topic.

Offline zebedeeeTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Active state for dynamic created subnav
« on: July 28, 2010, 10:00:37 AM »
Hi all, great forum so far. Am new so hope I find my way around ok.

I have been working on a site that uses mysql to store the info and then php to talk to mysql. Basically the site has a custom build CMS (I'm learning).

I have created a dynamic subnav in my page where entries can be added or removed in the admin area for the site.

Here is the code that pull the info etc. How can I set an active state for nav? Each sub nav link dynamically created the href.

I need to find if the url is the same as the href for each subnav link. If it is then add a class called .active to that li item only.

Hope that makes sense? any help would be great thank you,

Code: [Select]
$subnav = new DataConn; //connection to database with my classes file
$subnav->query("SELECT * FROM stock_categories ORDER BY category_order");

$currCatID = 0;
$currCatTitle = '';
$i = 1;
?>

<ul class="subnav">
<?php

while($rs_subnav mysql_fetch_array($subnav->result))
{
if ($i == 1)
{
$currCatID $rs_subnav['category_id'];
$currCatTitle $rs_subnav['title'];
}
?>

<li><a href="stock-list.php?cat=<?php echo $rs_subnav['category_id']; ?>"><?php echo $rs_subnav['title']; ?></a></li>
    <?php
$i++;
}
?>

</ul>

Offline zebedeeeTopic starter

  • Irregular
  • Posts: 2
    • View Profile
Re: Active state for dynamic created subnav
« Reply #1 on: July 28, 2010, 10:19:29 AM »
Hi all, great forum so far. Am new so hope I find my way around ok.

I have been working on a site that uses mysql to store the info and then php to talk to mysql. Basically the site has a custom build CMS (I'm learning).

I have created a dynamic subnav in my page where entries can be added or removed in the admin area for the site.

Here is the code that pull the info etc. How can I set an active state for nav? Each sub nav link dynamically created the href.

I need to find if the url is the same as the href for each subnav link. If it is then add a class called .active to that li item only.

Hope that makes sense? any help would be great thank you,

Code: [Select]
$subnav = new DataConn; //connection to database with my classes file
$subnav->query("SELECT * FROM stock_categories ORDER BY category_order");

$currCatID = 0;
$currCatTitle = '';
$i = 1;
?>

<ul class="subnav">
<?php

while($rs_subnav mysql_fetch_array($subnav->result))
{
if ($i == 1)
{
$currCatID $rs_subnav['category_id'];
$currCatTitle $rs_subnav['title'];
}
?>

<li><a href="stock-list.php?cat=<?php echo $rs_subnav['category_id']; ?>"><?php echo $rs_subnav['title']; ?></a></li>
    <?php
$i++;
}
?>

</ul>
Hi all, great forum so far. Am new so hope I find my way around ok.

I have been working on a site that uses mysql to store the info and then php to talk to mysql. Basically the site has a custom build CMS (I'm learning).

I have created a dynamic subnav in my page where entries can be added or removed in the admin area for the site.

Here is the code that pull the info etc. How can I set an active state for nav? Each sub nav link dynamically created the href.

I need to find if the url is the same as the href for each subnav link. If it is then add a class called .active to that li item only.

Hope that makes sense? any help would be great thank you,

Code: [Select]
$subnav = new DataConn; //connection to database with my classes file
$subnav->query("SELECT * FROM stock_categories ORDER BY category_order");

$currCatID = 0;
$currCatTitle = '';
$i = 1;
?>

<ul class="subnav">
<?php

while($rs_subnav mysql_fetch_array($subnav->result))
{
if ($i == 1)
{
$currCatID $rs_subnav['category_id'];
$currCatTitle $rs_subnav['title'];
}
?>

<li><a href="stock-list.php?cat=<?php echo $rs_subnav['category_id']; ?>"><?php echo $rs_subnav['title']; ?></a></li>
    <?php
$i++;
}
?>

</ul>

Which creates this subnav:-
<ul class="subnav">
      <li><a href="stock-list.php?cat=1">Cabins</a></li>
       <li><a href="stock-list.php?cat=2">Self Storage</a></li>
       <li><a href="stock-list.php?cat=3">Vehicles</a></li>

       <li><a href="stock-list.php?cat=4">Plant</a></li>
       <li><a href="stock-list.php?cat=5">Logistics</a></li>
       <li><a href="stock-list.php?cat=6">Out of the ordinary</a></li>
    </ul>