PDA

View Full Version : If Statement


tbqfh
06-17-2007, 09:56 PM
Hi Guys

I'm a lurker normally but, I actually need some help and I've tried all the normal avenues, Friends etc and no ones really been able to nail it for me

I know your all going to laugh at this because its probably really easy thing to do for most of you.

But what I need is the following code working so the current state of the button changes when your on a particular page. Now I have done this some years back and now I cant make it work..*bangs head on wall*

I cant seem to use a normal CSS statment as there is no actual seperate page bodies to add ids to.

<ul><li id="current"><a href="/" title="Home"><span>Home&nbsp;&nbsp;</span></a></li>
<li><a href="/vibrators-c-177.html" title="Vibrators"><span>Vibrators&nbsp;&nbsp;</span></a></li>
<li><a href="/anal-toys-c-118.html" title="Anal Toys"><span>Anal Toys&nbsp;&nbsp;</span></a></li>
<li><a href="/male-toys-c-190.html" title="Male Toys"><span>Male Toys&nbsp;&nbsp;</span></a></li>
<li><a href="/lubes-oils-c-181.html" title="Lubes<"><span>Lubes&nbsp;&nbsp;</span></a></li>
<li><a href="/clothes-c-16.html" title="Clothing"><span>Clothing&nbsp;&nbsp;</span></a></li>
<li><a href="/sexual-health-c-114.html" title="Sexual Health"><span>Sexual Health&nbsp;&nbsp;</span></a></li>
<li><a href="/adult-dvds-c-225.html" title="Adult DVDs"><span>Adult DVDs&nbsp;&nbsp;</span></a></li>
<li><a href="/books-mags-c-651.html" title="Books and Magazines"><span>Books & Mags&nbsp;&nbsp;</span></a></li>
<li><a href="/gifts-c-169.html" title="Gifts"><span>Gifts&nbsp;&nbsp;</span></a></li>
<li><a href="/contact_us.php" title="Contact Us"><span>Contact Us&nbsp;&nbsp;</span></a></li>
</ul>

If someone could just write the first statment i could then do the rest

Thanks in advance guys...

tbqfh

Fou-Lu
06-18-2007, 12:45 PM
Errr... What button are you referring to, I do not see any code to construct a button.

tbqfh
06-18-2007, 01:17 PM
Hi Sorry

The button is a css one you can see it

I just need it so the id changes to current depending on what ever URL you are on

Thanks again

Fou-Lu
06-18-2007, 03:49 PM
Are you referring to the top tabs, and simply changing it so the colour of the current page appears as the one selected?
The css is of course an easy one to do, just have two classes for each one, ie: selectedTab & tab for instance.
Now the trick is getting the currently selected id.
The links should be dynamically generated, and best put into an array:

$links = array(
'/' => 'Home',
'/link2' => 'Link 2',
...
);

$strLinks = '';
if (count($links) > 0)
{
$strLinks .= "<ul>\n";
for ($i = 0; $i < count($links); $i++)
{
$isCurrent = (strrpos($_SERVER['SCRIPT_FILENAME'], $links[$i][0]) !== false) ? ' id = "current"' : '';
$strLinks .= '<li' . $isCurrent . '><a href="' . $links[$i][0] . '" title="' . $links[$i][1] . '"><span>' . $links[$i][1] . "</span></a></li>\n";
}
$strLinks .= "</ul>\n";
}

Or something of the sorts. That should check against the current filename and compare it to the one given in the link itself (assuming its in the array). Also, I removed the non breaking spaces from your span, you should instead position it with css as well (using padding or margins, in this case, padding).
Hope that helps, post back if this isn't quite what you were looking for.

tbqfh
06-18-2007, 04:42 PM
Are you referring to the top tabs, and simply changing it so the colour of the current page appears as the one selected?
The css is of course an easy one to do, just have two classes for each one, ie: selectedTab & tab for instance.
Now the trick is getting the currently selected id.
The links should be dynamically generated, and best put into an array:

$links = array(
'/' => 'Home',
'/link2' => 'Link 2',
...
);

$strLinks = '';
if (count($links) > 0)
{
$strLinks .= "<ul>\n";
for ($i = 0; $i < count($links); $i++)
{
$isCurrent = (strrpos($_SERVER['SCRIPT_FILENAME'], $links[$i][0]) !== false) ? ' id = "current"' : '';
$strLinks .= '<li' . $isCurrent . '><a href="' . $links[$i][0] . '" title="' . $links[$i][1] . '"><span>' . $links[$i][1] . "</span></a></li>\n";
}
$strLinks .= "</ul>\n";
}

Or something of the sorts. That should check against the current filename and compare it to the one given in the link itself (assuming its in the array). Also, I removed the non breaking spaces from your span, you should instead position it with css as well (using padding or margins, in this case, padding).
Hope that helps, post back if this isn't quite what you were looking for.

Hi and thanks

But I think I might be out of my depth on this one, I thought it was easier to do. I've done this so far and dont really know whats next :confused:

<?php
$links = array(
'/' => 'Home',
'/vibrators-c-177.html' => 'Vibrators',
'/anal-toys-c-118.html' => 'Anal Toys',
'/male-toys-c-190.html' => 'Male Toys',
'/vibrators-c-177.html' => 'Female Toys',
'/lubes-oils-c-181.html' => 'Lubes & Oils',
'/clothes-c-16.html' => 'Clothes',
'/sexual-health-c-114.html' => 'Sexual Health',
'/adult-dvds-c-225.html' => 'Adult DVDs',
'/books-mags-c-651.html' => 'Books and Magazines',
'/contact_us.php' => 'Contact Us',

);

$strLinks = '';
if (count($links) > 0)
{
$strLinks .= "<ul>\n";
for ($i = 0; $i < count($links); $i++)
{
$isCurrent = (strrpos($_SERVER['SCRIPT_FILENAME'], $links[$i][0]) !== false) ? ' id = "current"' : '';
$strLinks .= '<li' . $isCurrent . '><a href="' . $links[$i][0] . '" title="' . $links[$i][1] . '"><span>' . $links[$i][1] . "</span></a></li>\n";
}
$strLinks .= "</ul>\n";
}
?>

Sorry for being a complete nube on this

You can see ive added it to the file already

Fou-Lu
06-18-2007, 07:32 PM
Sry, my cache is deleted whenever I close my browser so I don't have the url anymore.
However, if you are using that code to generate your links, with the exception of the home (sorry my bad on that one, it needs to be index.php), the only other problems I can see is that the pages are used with .html extensions. This code needs to be parsed and included into whatever file you plan to use, and chances are you need a .php extensions to provide the dynamic content.
Otherwise, if the parsing is ok, your next step is to set up a css style for the id:

<style type="text/css">
ul li { background-color: black; color: white; }
ul li#current {background-color: white; color: black;}
</style>

for a simple example.