Forum Moderators: coopster

Message Too Old, No Replies

PHP/SQL Menu Current page

         

sanguinor

11:11 am on Oct 3, 2011 (gmt 0)

10+ Year Member



Hi there, I've been trying to get this section of code to work correctly and apply a change to the list class dependent on what article is currently showing. IE if the current URL is index.php?page=article&article=departments then it should change the class of that list creation and highlight that option as according to my CSS.

Now am I doing this right or am I barking up the wrong tree?

<?php
$query = "SELECT * FROM menu";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
if (isset($_GET['article'])){
$article=$_GET['article'];
} else {
$article="none";
}
$current = array($row['alias']);
if (in_array($article, $current))
$current_page="current";
else $current_page = "none";
$query = "SELECT * FROM menu WHERE parent_id = 0";
$result = mysql_query($query) or die(mysql_error());
echo '<div id="links">';
echo '<ul>';
while($row = mysql_fetch_array($result)){
echo '<li class="' . $current_page . '"> <a href="' . $row['link_url'] . '">' . $row['label'] . "</a></li>";
}
echo '</ul>';
echo '</div>';
?>


My SQL table looks like this:
ID - 6
label - Departments
alias - departments
link_url - index.php?page=article&article=departments
parent_id - 0

Any thoughts on this would be appreciated.

enigma1

1:01 pm on Oct 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you could do just one query (the 2nd one) and inside the loop you could check for an article match.


$article = isset($_GET['article'])?$_GET['article']:'none';

while($row = mysql_fetch_array($result)){
$css_class = $row['article'] == $article?'current':'none';
echo '<li class="' . $css_class . '"> <a href="' . $row['link_url'] . '">' . $row['label'] . "</a></li>";
// other code

sanguinor

9:35 am on Oct 4, 2011 (gmt 0)

10+ Year Member



That's excellent thank you, done the job brilliantly. Unfortunately I now can't get my list to display inline... closest I have got is 'stepped' so they're next to each other but drop about 5 pixels every links.

---

Update, realised I'd left a '}' in the wrong place, it's no longer stepped and screwing up the rest of the page but it is now refusing to show inline. Joy of joy. I will plough onwards!

enigma1

3:37 pm on Oct 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know the html/css code you have in place, when you say inline you mean to display adjacent links instead of one per row? If so specify a width on the outer container and then use a percentage of how much each li should take along with a floater. I would prefer to use divs throughout instead of the browser incompatible ul/li combos. Here is an example with 2 entries per row.

$article = isset($_GET['article'])?$_GET['article']:'none';
echo '<div class="outer" style="width:400px;">';
while($row = mysql_fetch_array($result)){
$css_class = $row['article'] == $article?'current':'none';
echo '<div class="' . $css_class . '" style="float: left; width="50%;"><a href="' . $row['link_url'] . '">' . $row['label'] . "</a></div>";
// other code
.....
}
echo '</div>';

Make sure padding/margin is 0 for the inner divs as for each row they will take up the full width.

sanguinor

8:59 am on Oct 11, 2011 (gmt 0)

10+ Year Member



I had another go at it and managed to get it working with the following:

$article = isset($_GET['article'])?$_GET['article']:'none';
echo '<div id="nav_bar">';
echo '<div id="nav_links">';
while($row = mysql_fetch_array($result)){
$current_page = $row['alias'] == $article?'current':'none';
echo '<div id="nav_links_box" class="' . $current_page . '"><a href="' . $row['link_url'] . '">' . $row['label'] . "</a></div>";
}
echo '</div>';
echo '</div>';

With 'float:left;' applied to #nav_links_box.

Cheers for the help mate :)