homepage Welcome to WebmasterWorld Guest from 54.196.194.204
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
dynamic menu
Gilead




msg:4407337
 4:52 pm on Jan 16, 2012 (gmt 0)

I've been wondering about this for a long while, while I think I have a handle on how to do this, I'd like to run it by you guys to see if there is a better way to do it.

Two parts:
php section
$page=$_SERVER['REQUEST_URI'];
if ($page="index.php") {
page1="#";
image1="home3.png";
page2="profile.php";
image2="profile.png";
page3="nextpage.php";
image3="nextpage.png";
}
html section
Then echo the variables out in the html tags
<a href="<?php echo $page1; ?>" title="Home"><img src="<?php echo $image1; ?>" height="25" alt="" title="" border="0" /></a>

Then the menu can be dynamic, still have the mouseovers/dropdowns but able to be included by itself.
Thanks!

 

rocknbil




msg:4407435
 8:46 pm on Jan 16, 2012 (gmt 0)

Not really sure what the extra values are for, but I'd use an array of arrays defined in one location.

<?php
$page=$_SERVER['REQUEST_URI'];

$pages = array (
'index.php' => array('#','home3.png','profile.php','profile.png','nextpage.php','nexpage.png'),
'about.php' => array('index.php','home2.png','what-we-do-php.php','what-we-do.png','nextpage.php','nexpage.png'),
}
?>


<ul id="navigation">
<li><a href="<?php echo $pages[$page][0]; ?>" title="Home">
<img src="<?php echo $pages[$page][1]; ?>" height="25" alt="" title="" border="0" /></a></li>
<li><a href="<?php echo $pages['about.php'][0]; ?>" title="Home">
<img src="<?php echo $pages['about.php'][1]; ?>" height="25" alt="" title="" border="0" /></a></li>
</ul>

Gilead




msg:4407448
 9:05 pm on Jan 16, 2012 (gmt 0)

You need one var for the new location of the link and one for the image used. If you are on that page, have it display the third image(indicating where you are) along with the link be # so you don't go anywhere.

Arrays are generally static, what is echoed will change depending on what page you are on. The key being if you are on the index page, you don't want to go to the index page. (recursion) and you want to show the image indicating where you are instead of the normal image in the navigation menu.

Does that make more sense?

rocknbil




msg:4407779
 4:55 pm on Jan 17, 2012 (gmt 0)

They're static, yes, but then so are hard coded if/then/switch blocks. With an array, you can change it easily in one place.

you want to show the image indicating where you are instead of the normal image in the navigation menu.


Still not getting it all, but if you standardize naming of files/etc., and make sure there is a file for every page, you can do something like this. I'm using REQUEST_FILENAME but you can use other environment variables, whatever works.


// Presuming there will always be an index.png for index.php,
// about.png for about.php, etc.

<img src="/images/navigation/"<?php explode('.',$_SERVER['REQUEST_FILENAME']) . '.png'; ?>" alt="Where I am">

You can use the page title or other variables present to output the alt image, etc.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved