Hi folks,

I've used a while loop to call about 5 category names from a database to make a navigation. I didn't want to hard code the categories because I want to be able to change them using Content Management and have the nav update from the database.

<a href="category.php?cat=<?php echo $cid ?>"><?php echo stripslashes($cname) ?>

Clicking one of the links will bring up the Category page selected from the menu.

Question is, how can I make this nav highlight to the category that was chosen on the category.php page?

I would appreciate any help or ideas to work this out.
-Besherek

Recommended Answers

All 6 Replies

Just to add. I'm using CSS to style the nav using Divs and Spans. It's just a matter of calling the selected category from the loop to highlight it as selected.

i.e. on the page category.php?cat=3 . How can I highlight cat 3 in the nav to show that it's the page being viewed.

Just to add. I'm using CSS to style the nav using Divs and Spans. It's just a matter of calling the selected category from the loop to highlight it as selected.

i.e. on the page category.php?cat=3 . How can I highlight cat 3 in the nav to show that it's the page being viewed.

You can do it in PHP or you can do it with CSS only.
PHP - http://www.alistapart.com/articles/keepingcurrent/#
CSS - http://hicksdesign.co.uk/journal/highlighting-current-page-with-css

Hi, thanks for that info, but this isn't what I'm looking for.

What I have is a sub navigation selecting the cat id and cat name from a database using mysql_fetch_array().

<?php
  // set up SQL statement to output data
  $queryshowroomlink = 'SELECT * FROM category';

  // execute the SQL
  $resultshowroomlink = mysql_query($queryshowroomlink);

  // display the result to the browser
  if (!$resultshowroomlink) {
    exit('Error retrieving Featured Site from database');
  }
  // fetch the next row of results set until there are no more left
  while ($showroomlink = mysql_fetch_array($resultshowroomlink)) {
    // store results in variables
    $cid = $showroomlink['cid'];
    $cname = $showroomlink['cname'];
?>
              <div class="link">
                <span class="item" id="main"><a href="category.php?cat=<?php echo $cid ?>"><?php echo stripslashes($cname) ?> &nbsp; &#8226;</a></span>
              </div>
<?php
  // end loop
  }
?>

In this state, with the CSS I have, it merely has a hover affect where the text changes color, etc.

However, I need to have this nav know exactly which category the page is currently viewing so I can highlight the item in the subnav so the user knows by looking at the subnav, which page the are on.

I have breadcrumbs in the title using PHP, no problem there, I'm just having difficulty highlighting one row of data in the nav from the array.

What I'm basically asking is there an if statement maybe within the loop I need to build to say, this row is the selected category, so highlight it.

I need to have this nav know exactly which category the page is currently viewing so I can highlight the item in the subnav so the user knows by looking at the subnav, which page the are on.

Let me see if I understand.
The cat number will be held in the URL, right? - $_GET
And you want the item where $cid == $_GET to be highlighted?

So is this vaguely what you mean?
(I put the <div> into an echo; makes it easier to add in variables)

// fetch the next row of results set until there are no more left
  while ($showroomlink = mysql_fetch_array($resultshowroomlink)) {
    // store results in variables
    $cid = $showroomlink['cid'];
    $cname = $showroomlink['cname'];
    echo "              <div class=\"link\"";
    if ($cid == $_GET['cat']) echo " style=\"####INSERT HIGHLIGHT STYLE####\"";
    echo ">";
    echo "                <span class=\"item\" id=\"main\"><a href=\"category.php?cat=$cid\">".stripslashes($cname)."&nbsp; •</a></span>\n";
    echo "              </div>\n";
  // end loop
  }

Let me see if I understand.
The cat number will be held in the URL, right? - $_GET
And you want the item where $cid == $_GET to be highlighted?

So is this vaguely what you mean?
(I put the <div> into an echo; makes it easier to add in variables)

// fetch the next row of results set until there are no more left
  while ($showroomlink = mysql_fetch_array($resultshowroomlink)) {
    // store results in variables
    $cid = $showroomlink['cid'];
    $cname = $showroomlink['cname'];
    echo "              <div class=\"link\"";
    if ($cid == $_GET['cat']) echo " style=\"####INSERT HIGHLIGHT STYLE####\"";
    echo ">";
    echo "                <span class=\"item\" id=\"main\"><a href=\"category.php?cat=$cid\">".stripslashes($cname)."&nbsp; •</a></span>\n";
    echo "              </div>\n";
  // end loop
  }

Let me check it out. Thanks

Awesome stuff, Thanks for you help jedi_ralf, this works perfectly. Nice and easy, didn't think it would be that simple.

Cheers
-Besherek

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.