[thelist] best way to do dynamic tabs in PHP

Aleem Bawany aleem.bawany at utoronto.ca
Tue Jan 21 21:57:01 CST 2003


> ----------------------------------------
> // get page name, assume 'parties.php'
> $curpage = basename($_SERVER['SCRIPT_NAME']);
> // test each tab to see if it is the right page
> if($curpage == "parties.php")
> {
>   // show 'selected' parties tab with no rollover state }
> else
> {
>   // show regular parties tab with rollover state
> }
> ---------------------------------------
>
> 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

I don't see how it's that much code. You are using a while
loop around those if statements aren't you? That's
one if-else and a while/foreach loop - not much code. You
would probably end up with something like this:

$pages = array("one.php", "two.php", ..., "six.php");
foreach ($pages as $page) {
	if ($page != $currpage)
		// code for regular link
	else
		// code for current link
}

If you're looking for shorter code you could try something
like this:

echo (($currpage=="one.php")? "curr" : "regular");


this will print "curr" or "regular" depending on the link.
There is another approach, something which perl mongers use
often, and maybe you could use it in some way:
<code>

if ($curr == "one.php" || print("normal page")) {
	echo "current page";
}

</code>

if the first condition is true, the second condition isn't checked
because || is satisfied and current page gets written, otherwise
normal page gets printed. IIRC, you need to use print because it's
a function call (unlike echo).


aleem

[ http://members.evolt.org/aleem/ ]




More information about the thelist mailing list