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 07-06-2011, 01:24 PM   #1 (permalink)
The Acquainted
 
KingOfTheSouth's Avatar
 
Join Date: Oct 2008
Location: Cincinnati
Posts: 151
Thanks: 14
KingOfTheSouth is on a distinguished road
Default php tab select

Hey everyone, I have come to something I am working on for my website template. I have a top navigation that are tabs and when you click a tab to the next page it pops out the next tab as the selected tab. Very basic with HTML this is my sample code for it.

Code:
      <ul>
        <!-- **** INSERT NAVIGATION ITEMS HERE (use id="selected" to identify the page you're on **** -->
        <li><a id="selected" href="index.php">home</a></li>
        <li><a href="about.php">about</a></li>
        <li><a href="portfolio.php">portfolio</a></li>
        <li><a href="services.php">services</a></li>
        <li><a href="contact.php">contact</a></li>
        <li><a target="_blank" href="/blog">blog</a></li>
      </ul>
Quote:
Note: The files do have a .php file extension but I have very little php in the files the site in mostly HTML.
So now that I explained that part I am looking for a solution to do it in PHP or JavaScript if possible. If anyone can help me with a easy solution it would be much appreciated.
KingOfTheSouth is offline  
Reply With Quote
Old 07-06-2011, 02:28 PM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 336
Thanks: 8
tony is on a distinguished road
Default

so for example if I click portfolio. it would take me to porfolio.php but the portfolio tab in the nav would have the selected id? I might have understood wrong, but that is always a high possibility this early.

to keep it as you have it now. I would just use something like:

PHP Code:
<?php
$page 
basename(__FILE__);
$sel 'id="selected"';
?>
<ul>
  <!-- **** INSERT NAVIGATION ITEMS HERE (use id="selected" to identify the page you're on **** -->
  <li><a <?php if($page == 'index.php'){ echo $sel; } ?> href="index.php">home</a></li>
  <li><a <?php if($page == 'about.php'){ echo $sel; } ?>  href="about.php">about</a></li>
  <li><a <?php if($page == 'portfolio.php'){ echo $sel; } ?>  href="portfolio.php">portfolio</a></li>
  <li><a <?php if($page == 'services.php'){ echo $sel; } ?>  href="services.php">services</a></li>
  <li><a <?php if($page == 'contact.php'){ echo $sel; } ?>  href="contact.php">contact</a></li>
  <li><a target="_blank" href="/blog">blog</a></li>
</ul>
a bit repetitive, but it works
tony is offline  
Reply With Quote
Old 07-08-2011, 07:58 AM   #3 (permalink)
The Wanderer
 
Join Date: May 2010
Posts: 19
Thanks: 1
core1024 is on a distinguished road
Default

Here is my js solution.

HTML Code:
    <ul id="tabs">
      <!-- **** INSERT NAVIGATION ITEMS HERE (use id="selected" to identify the page you're on **** -->
      <li><a href="index.php">home</a></li>
      <li><a href="about.php">about</a></li>
      <li><a href="portfolio.php">portfolio</a></li>
      <li><a href="services.php">services</a></li>
      <li><a href="contact.php">contact</a></li>
      <li><a target="_blank" href="/blog">blog</a></li>
    </ul>
    <script type="text/javascript">
      window.onload=function () {
        function tabSelector (tabs) {
          function basename (location) {
            return location.split('/').pop().split('?').shift().split('#').shift();
          }
          var current = basename(window.location.href);
          var ti;
          for (ti in tabs) {
            if (typeof(tabs[ti]) === 'object')
              tabs[ti].id = (basename(tabs[ti].href) == current) ? 'selected' : null;
            }
          }
          tabSelector (document.getElementById('tabs').getElementsByTagName('a'));
        }
    </script>
core1024 is offline  
Reply With Quote
Old 11-28-2011, 11:28 AM   #4 (permalink)
The Visitor
 
Parkinson4's Avatar
 
Join Date: Nov 2011
Posts: 3
Thanks: 0
Parkinson4 is on a distinguished road
Default

It's really not all that hard (at least compared to some things). There are a number of ways. One easy(?) way is to define a function that accepts an argument to indicate which one is selected:
PHP Code:
function tabs($active)
{
$tabs = array(
'Home' => '/index.php',
'About Us' => '/about/index.php',
'Contact Us' => '/contact/index.php'
);
echo "<ul id='tabs'>";
foreach($tabs as $label => $path)
{
if($label == $active)
{
echo "<li class='active'>$label</li>\n";
}
else
{
echo "<li><a href='$path'>$label</a></li>\n";
}
}
echo "</ul>\n";
}
Then in your pages:
PHP Code:
<html>...<body>
<?php
include $_SERVER['DOCUMENT_ROOT'].'/includes/functions.php';
tabs('Home');
?>
rest of page...
Parkinson4 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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't get PHP 5. to work? Newbie windows PHP guy DotNetTim Absolute Beginners 11 02-01-2013 11:02 AM
10 PHP Myths Dispelled Wildhoney General 9 06-15-2009 06:55 AM
PHP Compressor Kalle Script Giveaway 8 05-28-2008 12:14 AM
what are all the subjects in php? sarmenhb General 7 01-21-2008 05:41 PM


All times are GMT. The time now is 12:59 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