Current location: Hot Scripts Forums » Programming Languages » PHP » Using an array for navigation


Using an array for navigation

Reply
  #1 (permalink)  
Old 06-04-04, 01:38 PM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
Using an array for navigation

I am using css to create navigation buttons an php for the " include function ". I want the "selected page" button to apear different to the others using a <div>.
This was getting very complicated as I needed a different file to include for every page and if I added yet another page I would have lots of files to alter.

Example

PHP Code:

<div class='button'>

  <
a href='main.php'>Home</a>
  <
a href='main.php?inc=info'>Information</a>
  <
a href='main.php?inc=photo'>Photos</a>
  <
a href='main.php?inc=video'>Video</a>
  <
div class='seleted'>Contact</div>
</
div
I then thought about using an array method and have come up with this very rough method.

PHP Code:

$main_array = array ( "Home" => "home""Information" => "info""Contact" => "cont""Photos" => "photo""Videos" => "vid" );


print(
"<table><tr>");
foreach(
$main_array as $main => $link)
print 
"<td><a href = main.php?inc=".$link.">".$main "</a>";
print(
"</tr></table>"); 
But I am back to the same problem with the selected page !!

Can anyone help to get either method working please ( I have $page = home; etc. on every included page so the code knows which page is displayed. )

Anthony
Reply With Quote
  #2 (permalink)  
Old 06-05-04, 12:57 PM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
I still can not get this to work.

I have tried
PHP Code:

print("<h2>Test 2</h1><br><table border=2><tr>");


foreach(
$main_array as $mname => $days)
    
if ( 
$inc == $page ){ print "<td><p>".$mname."</p></td>"; }

else { print 
"<td><a href = main.php?inc=".$days.">".$mname "</a>"; }

print(
"</tr></table><hr>"); 
All this dose is print every link the same using either the if or else depending on the $inc value.

I am then trying
PHP Code:

function selected() {

 if ( 
$mname[i] == $page ){$mname[i] == 'Wahoo';}
}

for(
$i=0$i<count($main_array);$i++){

selected();
   
print 
"<td><p>".$mname."</p></td>"; } 
But I have no idea where to call the funtion and it dose not seem to have any effect.

$inc is passed in the url and $page is on the included page.

Can someone point me in the right dirrection ?

Anthony
Reply With Quote
  #3 (permalink)  
Old 06-05-04, 06:00 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hello there,

You should check whether $_GET['inc'] is the same as whatever the $link you are looping through with.

From the first post you posted, the foreach loop can be rewritten as follows:

PHP Code:

<?php


foreach($main_array as $main => $link) {
    
// All links other than "home" have $_GET['inc'].
    
if ($_GET['inc'] == $link || (empty($_GET['inc']) && $link == 'home')) {
        
// Matched. this is the "selected" one.
        
echo '<td><div class="selected">'.$main.'</div>';
    }
    else {
        
// No match. Link.
        // Note that the IIF expression makes sure not to echo "?inc=" for "home".
        
echo '<td><a href="main.php'.($link != 'home' '?inc='.$link '').'">'.$main.'</a>';
    }
}

?>
Please read the comments for details. HTH.
__________________
Blavv =|
Reply With Quote
  #4 (permalink)  
Old 06-06-04, 07:04 AM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the help blaw; it seems to be working great now; I thought there must be a way of doing it but I had no idea how !

Below is a link to the output - its not working right with my css at the moment & so I will look into that. Site is realy rough as I am still deciding what to do.

Anthony

http://www.rubble.info/tests/new/main.php - As blaw's help

http://www.rubble.info/tests/new/main2.php - The look I am after

P.S. ( Edit ) Layout now sorted; I did not need the <td> in the code as that is sorted in the css.

Last edited by Bonzo; 06-06-04 at 07:11 AM.
Reply With Quote
  #5 (permalink)  
Old 06-06-04, 01:29 PM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
My site was getting to be a real mess and so I am starting from scratch; I have learnt a couple of new things to improve it over the last year !

If you want to see the method I was talking about in this thread; I am going to use it on www.rubble.info but wait for a day or so !!!

Anthony
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multidimensional Array Sorting Kage PHP 6 12-12-05 04:19 PM
file into an array, then appending array only if no duplicates djpennin Perl 2 05-17-04 01:04 PM
How do I write an array to a file? bark PHP 2 03-30-04 12:28 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM
asp: values in array not in order?? seala ASP 0 08-16-03 12:06 PM


All times are GMT -5. The time now is 10:37 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.