[thelist] best way to do dynamic tabs in PHP

Mike Migurski mike at saturn5.com
Tue Jan 21 21:18:00 CST 2003

>I have a site with your typical tabbed navigation, with rollovers. Each
>tab is an image with a link of course, just like this:
>And I would do this for each of the six images. This seems like lots of
>code, and I'm thinking there is a better way to do this, but I'm not sure
>what it is. I do NOT want to do something like numbering my images
>instead of naming them and using an array, but I'm sure thats not the
>only way.

You can use arrays without having to number your images. Try PHP's foreach
construct, like so:

[clip 'n save]------8<-------------------------------

$all_pages = array('eenie', 'meenie', 'miney', 'moe');
$current_page = 'miney';

foreach($all_pages as $a_page) {

	printf('<a href="%s.php"><img src="%s_%s.jpg"></a>',
		(($a_page === $current_page) ? 'on' : 'off')));



That's drastically simplified, but it's the most compact-yet-legible way I
know of to get what you are trying to achieve. I used printf() because in
situations like this one, it can help make echo statements with lots of
inserted variables a little easier on the eyes.



see first comment for example of ternary operator, which doesn't seem to
be properly documented in PHP:

michal migurski- contact info and pgp key:

                "Freedom! Horrible, horrible freedom!"

More information about the thelist mailing list