Closed dbashby closed 8 years ago
maybe the $padeid
is null, try to echo out that value to check that it works.
I tried <?php echo $nav['id'] . ' ' . $pageid . ' ';?>
and in return I got
`
2 home
<li><a href="?page=about">About</a></li>
3 home
<li><a href="?page=faq">FAQ</a></li>
4 home
<li><a href="?page=contact">Contact</a></li>
</ul>
` As shown above it is only picking 1 $pageid at a time, so if home selected all $pageid's are home etc.
So $nav['id'] is picking up the number, as expected but $pageid is picking up the label so I have tried
<li<?php if($pageid == $nav['id'] || $pageid == $nav['label']) { echo ' class="active"'; } ?>><a href="?page=<?php echo $nav['slug']; ?>"><?php echo $nav['label']; ?></a></li>
Thinking that either way it would be picked up but no, I am no further forward.
Thanks for any help or pointers.
So I have now got the active class working with 1 change
`#Main nav function function nav_main($pdo, $pageid, $nav){ $stmt = $pdo->query("SELECT * FROM posts"); $stmt->bindParam(1,$pageid,PDO::PARAM_INT); $stmt->execute(); foreach($stmt as $nav){ ?>
<?php //echo $nav['id'] . ' ' . $pageid; ?>
<li<?php if($pageid == $nav['id']) { echo ' class="active"'; } ?>><a href="?page=<?php echo $nav['id']; ?>"><?php echo $nav['label']; ?></a></li>
<?php }
}`
In the
I have had to change the<?php echo $nav['slug'];
to
<?php echo $nav['id'];
And everything works however this now means that I no longer have the slug coming out in the address bar but I do get the following on view source
`
<li class="active"><a href="?page=2">About</a></li>
<li><a href="?page=3">FAQ</a></li>
<li><a href="?page=4">Contact</a></li>
`
So now I need to find out why I cannot use the slug rather than the id.
When trying to echo
<?php echo 'nav id: ' . $nav['id'] . '<br>' . 'pageid: ' . $pageid . '<br>' . '$nav slug: ' . $nav['slug']; ?>
I get
nav id: 1 $pageid: home $nav slug: home
so the pageid is picking up the label rather than the id, however the $pageid is always the selected page and none of the other options show.
Working
<li<?php if($pageid == $nav['slug']) { echo ' class="active"'; } ?>><a href="?page=<?php echo $nav['slug']; ?>"><?php echo $nav['label']; ?></a></li>
As page id is taking the label, on selection will now equal the slug.
Prior to video 13 having static links the active state worked. With the help of Creptor on some previous code due to trying to convert all the MySQLi to PDO I have managed to get the navigation up on the site, on click does what is expected and fetches the results however the active class is not working anymore. I can only put this down to my PDO
HTML & PHP Code - navigation.php `
``` query("SELECT * FROM posts"); $stmt->bindParam(1,$id,PDO::PARAM_INT); $stmt->execute(); foreach($stmt as $nav){ ?>- >
`