PHP4 menu script problems..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Esch
Forum Newbie
Posts: 3
Joined: Mon Apr 28, 2003 5:28 am

PHP4 menu script problems..

Post by Esch »

Ok, first off I know very very little about PHP, I'm still in the learning stages. I'm currently building a site that is using a CSS navigation menu, but I want it to be dynamic to whatever page you're on. I'm also using a template system for the site, so the area that the below script is in is in the header include file.

I had a friend who helped me out a little bit but he's left town on vacation and didn't tell me till just now, so I'm in a bind because I needed this site done two weeks ago.

Here's what I'm trying to do: You load up the page and it goes through the following code to post the navigation links. It will set a $whatpage variable so it can check that and if it's on the page that the variable is set too, that navigation link will be turned off and highlighted through CSS.

The $pages variable is the info that gets plugged into the end of the URL to set the $whatpage variable. I have 2 problems with the current script.

1) Instead of listing the actual names from the $navnames variable, it's simply putting "Array" in every link name.

2) The $whatpage variable isn't getting reset when you click on a link, so the same link button remains highlighted no matter where you go in the site. I was under the assumption that by putting it at the end of the URL for whatever page you're on. "IE: blah.com?whatpage=thepageyoureon" would reset the $whatpage variable each time, am I wrong on that? I think the problem with this is that because this script is in the header file, it keeps resetting the variable to "news", so it's ignoring the other info. Should I include the variable reset in the actual page where the content is instead?

Anyway, if anyone can help me out with this, I would REALLY appreciate it. I need to get this site up and running but this is the main thing holding me back.

Thanks for your help!!!!
Esch




<?php
$pages = array("news", "aboutthestaff", "directorratings", "forum", "contests", "submissions", "links", "contact");
$navnames = array("News", "About The Staff", "Director Ratings", "Forum", "Contests", "Submissions", "Links", "Contact");
$whatpage = "news";
$display = "";

foreach ($pages as $page){
if ($whatpage != $page)
$display .= "<div id='leftnav'><a href='{$page}.php?whatpage={$page}'>{$navnames}</a></div>\n";
else
$display .= "<div id='leftnavon'>{$navnames}</div>\n";
}


if ($display != "")
echo ($display);
else
echo ("Nothing to display.");

?>
User avatar
Guy
Forum Commoner
Posts: 53
Joined: Sun Jan 12, 2003 3:34 am

try this instead of the foreach

Post by Guy »

Code: Select all

<?php
for($i=0;$i<count($pages);$i++)
{
if ($whatpage != $pages[$i]) {
$display .= "<div id='leftnav'><a href='{$pages[$i]}.php?whatpage={$pages[$i]}'>{$navnames[$i]}</a></div>\n"; 
}else {
$display .= "<div id='leftnavon'>{$navnames[$i]}</div>\n"; 
} 	
?>
you need an index for the $navnames array.
Guy
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Try this:

Code: Select all

<?php

// set-up one array with all page names and headings connected
// so that it's easier to manage
$pageinfo = array(
	'news' => 'News',
	'aboutthestaff' => 'About The Staff',
	'directorratings' => 'Director Ratings',
	'forum' => 'Forum',
	'contests' => 'Contests',
	'submissions' => 'Submissions',
	'links' => 'Links',
	'contact' => 'Contact',
);

// set whatpage to news if it is not set or if the
// value given is not one of the pages in the $pageinfo
// array, otherwise set it to what's in the query string
if (!empty($_GET['whatpage']) && array_key_exists(trim($_GET['whatpage']), $pageinfo)) {
	$whatpage = trim($_GET['whatpage']);
} else {
	$whatpage = 'news'; 
}


foreach ($pageinfo as $pagename => $pageheading){ 
	if ($whatpage != $pagename) {
		// you need to change something.php to whatever the
		// main page should be called
		$display[] = '<div id="leftnav"><a href="something.php?whatpage='.$pagename.'">'.$pageheading.'</a></div>'; 
	} else {
		$display[] = '<div id="leftnavon">'.$pageheading.'</div>'; 
	}
} 

if (!empty($display)) {
	// implode the $display array so that it can be displayed (each line
	// separated by a new line \n)
	echo implode("\n", $display); 
} else {
	echo 'Nothing to display.'; 
}

?>
Mac
Esch
Forum Newbie
Posts: 3
Joined: Mon Apr 28, 2003 5:28 am

Post by Esch »

Thank you very much for responding to this so fast, I appreciate it! I do have a few questions based off of your reply, however, so please bear with me.
// set whatpage to news if it is not set or if the
// value given is not one of the pages in the $pageinfo
// array, otherwise set it to what's in the query string
if (!empty($_GET['whatpage']) && array_key_exists(trim($_GET['whatpage']), $pageinfo)) {
$whatpage = trim($_GET['whatpage']);
} else {
$whatpage = 'news';
}
So if I'm understanding this right, when you click on any link, the ?whatpage=blahblah will reset the above $whatpage variable and switch out the buttons from being on to being off depending on where you're at on the site. Will having the variable already set to news (as shown above) cause a problem with the other pages, as in it would keep resetting itself back to news because this script is in the header include file? Or will this script be defaulted to the index page (which is news btw) to start and then will be smart enough to change the variable and know what page it's on on it's own?


foreach ($pageinfo as $pagename => $pageheading){
if ($whatpage != $pagename) {
// you need to change something.php to whatever the
// main page should be called
$display[] = '<div id="leftnav"><a href="something.php?whatpage='.$pagename.'">'.$pageheading.'</a></div>';
} else {
$display[] = '<div id="leftnavon">'.$pageheading.'</div>';
}
Same question as above really. If I set the something.php to whatever the main page is called (in this case, index.php), will it be smart enough to switch out that name depending on what the link is?

I hope what I'm asking is clear, again I'm a total newbie so I'm just trying to work it all out so I understand what's going on.

Thanks again!!!

Esch
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Esch wrote:Thank you very much for responding to this so fast, I appreciate it! I do have a few questions based off of your reply, however, so please bear with me.
// set whatpage to news if it is not set or if the
// value given is not one of the pages in the $pageinfo
// array, otherwise set it to what's in the query string
if (!empty($_GET['whatpage']) && array_key_exists(trim($_GET['whatpage']), $pageinfo)) {
$whatpage = trim($_GET['whatpage']);
} else {
$whatpage = 'news';
}
So if I'm understanding this right, when you click on any link, the ?whatpage=blahblah will reset the above $whatpage variable and switch out the buttons from being on to being off depending on where you're at on the site. Will having the variable already set to news (as shown above) cause a problem with the other pages, as in it would keep resetting itself back to news because this script is in the header include file? Or will this script be defaulted to the index page (which is news btw) to start and then will be smart enough to change the variable and know what page it's on on it's own?
The $_GET['whatpage'] bit refers to the information being sent in the URL - so if that's not set (first time someone comes to the site) or is wrong (refering to a page that's not set in your $pageinfo array) then it defaults to news.php. So yes, it will know when links have been clicked and which ones. You can then find out what page you're on by checking what's in $whatpage.
Esch wrote:
foreach ($pageinfo as $pagename => $pageheading){
if ($whatpage != $pagename) {
// you need to change something.php to whatever the
// main page should be called
$display[] = '<div id="leftnav"><a href="something.php?whatpage='.$pagename.'">'.$pageheading.'</a></div>';
} else {
$display[] = '<div id="leftnavon">'.$pageheading.'</div>';
}
Same question as above really. If I set the something.php to whatever the main page is called (in this case, index.php), will it be smart enough to switch out that name depending on what the link is?

I hope what I'm asking is clear, again I'm a total newbie so I'm just trying to work it all out so I understand what's going on.
This will depend on how the rest of the site is setup. I assume that you have index.php which has most of the code in it and once $whatpage is set there's a bunch of stuff that goes on and somewhere in there you have an include() statement, something like:

Code: Select all

include $whatpage.'.php';
if so, that's where the script determines which page content to show - in that it includes all the code from news.php by default or forum.php if the Forum link was clicked. As far as the user knows though, they're still on index.php.
Esch wrote:Thanks again!!!
S'alright. Hope this helps.

Mac
Esch
Forum Newbie
Posts: 3
Joined: Mon Apr 28, 2003 5:28 am

Post by Esch »

I just got home and plugged in the stuff you showed me and the site is now working like a charm! I had to make one slight change to the code with the page that gets loaded up, calling the same array info that goes into the ?whatpage=blah part of the URL because it kept loading the same file no matter what page I was on. But that problem is now fixed and it's working great.

Thanks for all your help with this, you really got me out of a jam!

Esch
Post Reply