Jump to content


Drop Down menu


5 replies to this topic

#1 Ph0enix

    Young Padawan

  • Members
  • Pip
  • 33 posts

Posted 23 June 2006 - 08:33 AM

Hello, iv recently bought a book to help me with my PHP.
Im trying to learn how to make a drop down menu and in my PHP book i was given this code

<?php
function generate_menu($name, $options, $default="") {
  
  $html = "<SELECT NAME=\"$name\">";
  foreach($options as $value => $label) {
	$html .= "<OPTION ";
	if ($value == $default)
	$html .= "SELECTED ";
	$html .= "VALUE =\"$value\">$label</OPTION>";
	  
}
$html .= "</SELECT>";
return($html);
}

?>

Now when i run this script i have a blank screen.
But when i add to the bottom
echo generate_menu($name, $options, $default="");

I get the Drop down menu produced but i also have an error saying
Warning: Invalid argument supplied for foreach() in C:\wamp\www\Scripts\dd_menu.php on line 5

Could sombody please tell me what is going wrong.
And also could someone explain what the script does becuase im new to PHP So i dont know.

Thanks

#2 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 23 June 2006 - 10:50 AM

$options should point to an array,

Try this:

<?php
function generate_menu($name, $options, $default="") {
  
  $html = "<SELECT NAME=\"$name\">";
  foreach($options as $value => $label) {
	$html .= "<OPTION ";
	if ($value == $default)
	$html .= "SELECTED ";
	$html .= "VALUE =\"$value\">$label</OPTION>";
	  
}
$html .= "</SELECT>";
return($html);
}


$menu = array('bar' => 'baz');
echo generate_menu('the-form-name', $menu); 

?>


What is does this:

Defines a new function calledgenerate_menu with 3 variables: :- $name, $options, and $default which is by default "" (empty).
> New variable $html with select box code in but also with $name in, $name willbe taken from whatever you set it as when you CALL the function
> a loop (look here: http://php.net/foreach) to deal with the array + values
> adds to the variable $html the options with $value and $label (the array key and value)
> IF to check is the default option you set, is the current value going around in the loop - if it is add SELECTED to the option which will make it first selected when you load the page.
> Adds value and label to variable $html
> closes loop
> $adds </select> to close form.
returns $html (the menu).




However, If that code is from a book...get your money back lol!

edit: I cleaned it up slightly for you.

<?php

function generate_menu ($name, $options, $default="") 
{
  
	$html = '<select name="'. $name .'">';
	  
	foreach($options as $value => $label) 
	{
		$html .= "<option ";
		
		$html .= ($value == $default) ? 'selected ' : '';
		
		$html .= 'value ="'. $value .'">'. $label .'</option>';
	  
	}
	
	$html .= '</select>';
	
	return($html);
}


$menu = array('bar' => 'baz');

echo generate_menu('the-form-name', $menu);

?>

Edited by .Matt, 23 June 2006 - 11:01 AM.


#3 Ph0enix

    Young Padawan

  • Members
  • Pip
  • 33 posts

Posted 23 June 2006 - 11:41 AM

Thanks you so much. You really helped. ;)
It works now.

#4 Ph0enix

    Young Padawan

  • Members
  • Pip
  • 33 posts

Posted 23 June 2006 - 11:46 AM

One more question xD
So to add more values into the drop down menu, would i just do this?

$menu = array('bar' => 'baz','max','bob','tim');

Thanks

#5 Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 23 June 2006 - 12:37 PM

kinda, just more like this ;)

$menu = array('option_value' => 'option_label', 'option_value2' => 'option_label2');

Just add
, 'option_value' => 'option_value'
after the last one, and change the values :D

Its easier to see if you change it to this:

$menu = array(
			'option_value' => 'option_label', 
			'option_value2' => 'option_label2',
			'option_value3' => 'option_label3',
			'option_value4' => 'option_label4',
			'option_value5' => 'option_label5',
			);

Just the same, but formatted better ;)

ps: cleaned the code a bit more:

<?php

function generate_menu ($name, $options, $default="")
{
	  $html = '<select name="'. $name .'">';
	
	foreach($options as $value => $label)
	{
		$x = ($value == $default) ? 'selected ' : '';
		$html .= '<option value="'. $value .'">'. $label .'</option>';
	}
	
	$html .= '</select>';
	return($html);
}
/* Ends the function */


$menu = array(
			'option_value' => 'option_label',
			'option_value2' => 'option_label2',
			'option_value3' => 'option_label3',
			'option_value4' => 'option_label4',
			'option_value5' => 'option_label5',
			);
			
/* To add a new menu item add:
@
@  , 'option_value' => 'option_label'
@
@  In the corect place. 
*/

echo generate_menu('the-form-name', $menu);

?>

Its not what i would do exactly, but i'll keep it along the same lines so you get it :D

Edited by .Matt, 23 June 2006 - 12:43 PM.


#6 Ph0enix

    Young Padawan

  • Members
  • Pip
  • 33 posts

Posted 23 June 2006 - 12:40 PM

ok Thanks :D





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users