Jump to content

Active state for dynamic created subnav


zebedeee

Recommended Posts

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,

 

$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>

Link to comment
Share on other sites

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,

 

$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,

 

$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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.