Page 1 of 1

including a menu file, basic array question

Posted: Wed Jun 02, 2004 5:25 am
by batfastad
OK here's what I want to achieve and I'm pretty knew to PHP so I don't really know what to look out for in the documentation.

I'd like to have one file, containing the text and the links for my website.

I'd like to have a php file called menu.php, which features an array, with the link text, and the link href to all the pages on my site.
I'd need it pretty easy to edit, just add a new item to the array or something.
Then on my main page, I simply include that menu.php file, with the links already built.

However, in that menu.php file, I will need to do an if statement, to get the value of the current file.

For example if the script is front.php then format each link/item in the array like this...

Code: Select all

<tr align="left" valign="middle"><td>
<a href="$linkhref">$linktext</a></td><td><img src="bullet.gif" alt="" border="0" /></td></tr>
but if the script is anything else, then format each link/item in the array like this...

Code: Select all

<a href="$linkhref">$linktext</a><br />
<br />
Where $linkhref is the href property of that item in the array, and $linktext is the text for the link.

Then from my main page, I simply include menu.php - and hopefully bingo! A site that's really easy to update!

I can do that if statement no problem, but I'm unsure how to actually setup the array, and then get the values out of it and into variables.

In the documentation I found some interesting stuff, functions like foreach and some other things that sound like they would be useful for this.
Unfortunately my PHP knowledge is too limited to be able to use this information.

If someone does know what the heck I'm on about, please explain your code as I'd like to actually understand how it works - and learn some of the syntax structures, rather than just getting it working and forgetting about it.
PHP seems like a complete solution for server-side programming to me. And I think it's essential that I learn it - so an help on this would be greatly appreciated.

Thanks in advance

Posted: Wed Jun 02, 2004 5:59 am
by JAM
Hopefully this will give you more ideas:

Code: Select all

// example links
$links['Link One'] = 'http://www.example.com/';
$links['Contact'] = 'contact.php';
$links['A cow'] = '/animals/muuh.html';

// example : looping each 'row' in the $links array...
foreach ($links as $name => $value) {
  echo '<a href="'.$value.'">'.$name.'</a><br />';
}
Replace the echo part in the foreach loop, with an if-then-else giving you the desired effect based on the 'front.php'-issue you mention.
A tip there is to get the URI of the page being shown, use basename() on that to the the filename and...

I tried not to spoil all the fun for you, but to mention some interesting approaches to look into. ;)
Good luck.

Posted: Wed Jun 02, 2004 6:08 am
by markl999
Here's another way to do it, also similar to JAM's method.

Code: Select all

<?php
//example menu.php
//menu array in linktext => linkhref format
$menu = array(
	'home' 		=> '/',
	'links' 	=> '/links/',
	'contact' 	=> '/contact/',
	'pictures' 	=> '/pictures/'
);
//as the a href is the same for both types of menu
//(front and the default one) then we can build up an array
//of html href links
foreach($menu as $linktext=>$linkhref){
	//create the html a href and store it in an array
	$htmlmenu[] = '<a href="'.$linkhref.'">'.$linktext.'</a>';
}
//now determine which page we are on
//and require the relevant menu file
//i've put the actual html for the menus in 2 different files
//front_menu.php and default_menu.php to separate the logic
//(business logic from presentation logic)
//basically keeping the code and html separate so changes are easier
switch(basename($_SERVER['PHP_SELF'])){
	case 'front.php':
		require_once 'front_menu.php';
		break;
	default:
		require_once 'default_menu.php';
		break;
}
?>

<?php
//example front_menu.php
//loop over the html hrefs and display them
foreach($htmlmenu as $menuitem){
	echo <<< EOD
	<tr align="left" valign="middle">
		<td>{$menuitem}</td>
		<td><img src="bullet.gif" alt="" border="0" /></td>
	</tr>
EOD;
}
?>

<?php
//example default_menu.php
//loop over the html hrefs and display them
foreach($htmlmenu as $menuitem){
	echo <<< EOD
		{$menuitem}<br /><br />
EOD;
}
?>

Posted: Wed Jun 02, 2004 11:04 am
by batfastad
JAM,
How come your example starts differently to markl999's? Is it still called an array? Are there different formats?

Also on the penultimate line of your code, why is there a periods . either side of $value and $name. And in a few cases in markl999's code too.
Why do they need to be there? What do they do?

Markl999,
What does <<< EOD do?
Also why is there curly brackets round the variables in the last couple of lines of your code?

Those are just a few basic syntax questions that really does my head in about PHP so far. All the different punctuation used for different things on different occasions. And those sort of things I've not been able to find much information about in the documentation - searching for {$menuitem} in the docs for example won't get me anywhere.
So if you could iron out a few of those questions for me then I'll get a bit more understanding of what's going on.

Thanks for the extra comments in the code guys - very useful indeed. I certainly don't want to be seen as someone who asks a question to get some free code out of people!

Posted: Wed Jun 02, 2004 11:30 am
by markl999
Both myself and JAM used arrays, we just used different methods of creating them, but they are essentially the same.

The periods are used to append (concatenate) 'bits' together.
For example, you can do:
$string = "Hello $username";
or
$string = 'Hello '.$username;
The main thing to notice is the quotes. The first uses double quotes, the latter uses single quotes. PHP variables don't get evaluated (interpolated) inside single quotes, so echo 'Hello $foo'; will literally output Hello $foo and not use the value of $foo. So it's just a matter of preference/style which one you use, variables inside double quotes, or single quotes for plain old text and use the period to concatenate variables.

The echo <<< EOD stuff is called heredoc syntax, you can read about it here.
It's just a way to echo chunks of text without having to echo individual lines and escape quotes etc, i also think it looks 'neater' ;)
I just used {$menuitem} for clarity as it help identify variables, it's also required sometimes, imagine onetwo$foothree where $foo is the variable, PHP will think $foothree is the var, so onetwo{$foo}three helps clear that up.

That's a simplified explanation of your questions but i hope it makes sense :o

Posted: Wed Jun 02, 2004 2:10 pm
by batfastad
Oh right, so in theory any variable could have {} surrounding them. Certainly makes them easier to spot.

Thanks for all your help. I think i've got enough to get it going - and some interesting stuff to look at in the docs.

Thanks again guys!