TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-29-2008, 10:59 PM   #1 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default @included navigation... Setting one active?

How would you set one element of a list to active depending on the navigation?

so the navigation is:
<ul>
<li id="active"><a href="index.htm">Index</a></li>
<li><a href="whatsnew.htm">Whats New</a></li>
<li><a href="about.htm">About</a></li>
<li><a href="why.htm">Why</a></li>
<li><a href="hours.htm">Hours</a></li>
<li><a href="services.htm">Services</a></li>
<li><a href="contact.htm">Contact Us</a></li>
<li><a href="newsletter.htm">Latest News</a></li>
</ul>

Well if I was on whatsnew.htm, how would I make it active?
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 02-29-2008, 11:06 PM   #2 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Assuming you are using PHP for this, I would do something like:

PHP Code:
<ul>
<li <?php echo ($active == 'index' 'id="active"' ''); ?>><><a href="index.htm">Index</a></li>
<li <?php echo ($active == 'whats-new' 'id="active"' ''); ?>><a href="whatsnew.htm">Whats New</a></li>
<li <?php echo ($active == 'about' 'id="active"' ''); ?>><><a href="about.htm">About</a></li>
<li <?php echo ($active == 'why' 'id="active"' ''); ?>><><a href="why.htm">Why</a></li>
<li <?php echo ($active == 'hours' 'id="active"' ''); ?>><><a href="hours.htm">Hours</a></li>
<li <?php echo ($active == 'services' 'id="active"' ''); ?>><><a href="services.htm">Services</a></li>
<li <?php echo ($active == 'contact-us' 'id="active"' ''); ?>><><a href="contact.htm">Contact Us</a></li>
<li <?php echo ($active == 'latest-news' 'id="active"' ''); ?>><><a href="newsletter.htm">Latest News</a></li>
</ul>
Then just set the $active variable at the top of each page.

If you're not using PHP to do this, then I'm afraid I have no idea - could probably do something fancy in javascript to do it.

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
The Following User Says Thank You to Alan @ CIT For This Useful Post:
Aaron (02-29-2008)
Old 02-29-2008, 11:54 PM   #3 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

You gotta explain that code to me... I didn't even know echo... had parentheses.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 03-01-2008, 12:03 AM   #4 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

It's a condensed version of an if() statement

PHP Code:
$myVar 'blah';

// Writing:
echo ($myVar == 'blah' 'Yes!' 'No!');

// Is exactly the same as writing:
if ($myVar == 'blah'
{
    echo 
'Yes!';
}
else
{
    echo 
'No!';

Basicly, PHP will see it as "if the variable $active equals 'whatever' then echo 'id="active"', otherwise echo nothing".

So in theory, it will only echo 'id="active"' for the active page, the other checks will echo nothing.

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 03-01-2008, 02:52 AM   #5 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

Thanks, this code is amazing, and it solves a problem I have had for a while.
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 03-01-2008, 03:23 AM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Slightly unrelated, but I cannot wait for PHP 5.3's adaptation of the ternary operator. Currently you have to do like the following:

php Code:
$myVar = isset($_GET['myVar']) ? $_GET['myVar'] : 'Unset';

Whereas in PHP 5.3 you will be able to do:

php Code:
$myVar = isset($_GET['myVar']) ?: 'Unset';

I don't know why because it's so simple, but I am really looking forward to that! Maybe more so than namespaces !
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 03-02-2008, 01:07 AM   #7 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
php Code:
$myVar = isset($_GET['myVar']) ?: 'Unset';
Correct me if I'm wrong, but your snippet (if $_GET['myVar'] does exist) would assign TRUE to $myVar. My understanding would be that the shortcut you provided would basically be (in more normal use): $myVar = isset($_GET['myVar']) ? isset($_GET['myVar']) : 'Unset';

Maybe I've been reading the mailing list messages incorrectly (or the wrong list!).
Salathe is offline  
Reply With Quote
Old 03-01-2008, 09:53 AM   #8 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

I saw a line about the ?: operator on the 5.3 changelog but never got around to invstigating further. Looks very handy and the less typing I have to do, the happier I am

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 03-01-2008, 02:11 PM   #9 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

Is there any way to set $active to the page? like with that funny server array?
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 03-01-2008, 02:27 PM   #10 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

If by the page you mean something like navigating to index.php?page=doodles then it's a simple matter of setting active to the $_GET['page'] variable.
TlcAndres is offline  
Reply With Quote
Old 03-01-2008, 03:31 PM   #11 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default

I was looking for
PHP Code:
$active "$_SERVER['PHP_SELF']" 
BUUUT, I still need to figure out how to change
/<snip>/<snip>/httpdocs/index.php
into index.php
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 03-01-2008, 07:21 PM   #12 (permalink)
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

Use the basename function, it strips everything but the filename.
PHP Code:
$path "/home/httpd/html/index.php";
echo 
basename($path); // will show just "index.php"; 
Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote
The Following User Says Thank You to DeMo For This Useful Post:
Aaron (03-02-2008)
Old 03-02-2008, 01:28 AM   #13 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

They may use that approach:

php Code:
$myVar = $_GET['myVar'] ?: 'Unset';

But that raises the question of whether or not PHP should throw an undefined error in that instance, as we're not safely asking PHP if it is set. I would assume it would throw an error because I would expect it to act exactly like it would in an if statement:

php Code:
if($_GET['bleh'])
{
   
}

If PHP decide not to throw an undefined error in the ternary, then surely questions arise as to them not being constant across the board. Although an isset would, in theory, return true in that instance, I see PHP as actually using that approach because there's no first argument any more to actually specify the value, both checking and assigning are done at once.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:15 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design