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 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:
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.phpSo you could do some more checking likeif($_SERVER[‘PHP_SELF’] == ‘/pageone.php’){ echo ‘bold’; }
This is quite an ugly way of doing it, though it may work for your purposes.