PHP Menu - page conditional formating
I have a PHP menu which I include simply via
<?php include_once("includes/blog.php") ?>
I'm trying to figure out how to reformat the link text in the menu if the linked page is being displayed in the browser - e.g. (simple example)
PHP inserts the following links:
One Two Three Four
If 开发者_JAVA技巧Four is clicked and the page loaded, I would like it to appear as
One Two Three Four
Is this possible (I have searched my apologies if I've missed something).
You can do this several ways, $_GET vars, $_SESSION data, reading the current page url,
Here is one way to do it....
Make your links so that:
//On your actual page
<style>
.bold{ font-weight:bold; }
</style>
//In includes/blog.php
<?php
echo '<a href="pageone.php?id=1" class="'.(($_GET['id'] == "1") ? 'bold' : '').'">One</a>';
echo '<a href="pagetwo.php?id=2" class="'.(($_GET['id'] == "2") ? 'bold' : '').'">Two</a>';
?>
You'll need to format that to whatever you have, but hopefully you get the idea.
This part class="'.(($_GET['id'] == "1") ? 'bold' : '').'"
Means, If $_GET['id'] equals 1, then echo 'bold'. So if our link that we clicked was pageone.php?id=1, we know we would have a $_GET variable named 'id' that was equal to 1. So our class would look like: class="bold"
. If our page has the .bold{ font-weight:bold; }
on it, then our selected link will be bold.
If you aren't using any framework that keeps track of your pages, then you can use $_SERVER['PHP_SELF'] to do matching against your link.
See the manual: http://www.php.net/manual/en/reserved.variables.server.php
If someone typed in http://www.example.com/pageone.php
$_SERVER['PHP_SELF'] would contain /pageone.php
So you could do some more checking like
if($_SERVER['PHP_SELF'] == '/pageone.php'){ echo 'bold'; }
This is quite an ugly way of doing it, though it may work for your purposes.
精彩评论