PDA

View Full Version : Creating dropdown menu with array... help.....


WRStrong
06-29-2004, 09:44 PM
I am tinkering with an idea & am stuck. If I want to make a drop-down menu box to use in a form and want the info to be able to be modified from an array, I came up with this so far...

<?php

//
// Whereas item1[0] = 'Item type1'
// Whereas item1[1] = '12.34'
// Whereas item1[2] = '15.00'
//
// And so on...
//

$item1 = array ("Item type1", 12.34, 15.00);
$item2 = array ("Item type2", 12.34, 15.00);
$item3 = array ("Item type3", 12.34, 15.00);
$item4 = array ("Item type4", 12.34, 15.00);
$item5 = array ("Item type5", 12.34, 15.00);
$item6 = array ("Item type6", 12.34, 15.00);
$item7 = array ("Item type7", 12.34, 15.00);
$item8 = array ("Item type8", 12.34, 15.00);
$item9 = array ("Item type9", 12.34, 15.00);
$item10 = array ("Item type10", 12.34, 15.00);

print ("
<select size=1 name=name>
<option value=$item1[0];'+';$item1[1];'+';$item1[2]>$item1[0]&nbsp;+&nbsp;$item1[1]</option>
<option value=$item2[0];'+';$item2[1];'+';$item2[2]>$item2[0]&nbsp;+&nbsp;$item2[1]</option>
<option value=$item3[0];'+';$item3[1];'+';$item3[2]>$item3[0]&nbsp;+&nbsp;$item3[1]</option>
<option value=$item4[0];'+';$item4[1];'+';$item4[2]>$item4[0]&nbsp;+&nbsp;$item4[1]</option>
<option value=$item5[0];'+';$item5[1];'+';$item5[2]>$item5[0]&nbsp;+&nbsp;$item5[1]</option>
<option value=$item6[0];'+';$item6[1];'+';$item6[2]>$item6[0]&nbsp;+&nbsp;$item6[1]</option>
<option value=$item7[0];'+';$item7[1];'+';$item7[2]>$item7[0]&nbsp;+&nbsp;$item7[1]</option>
<option value=$item8[0];'+';$item8[1];'+';$item8[2]>$item8[0]&nbsp;+&nbsp;$item8[1]</option>
<option value=$item9[0];'+';$item9[1];'+';$item9[2]>$item9[0]&nbsp;+&nbsp;$item9[1]</option>
<option value=$item10[0];'+';$item10[1];'+';$item10[2]>$item10[0]&nbsp;+&nbsp;$item10[1]</option>
</select>
");

?>

I'd like it to be more compact and more dynamic...

Something like... (and I know it doesn't work, yet...)

<?php

//
// Whereas item[0] = 'Item type1'
// Whereas item[1] = '12.34'
// Whereas item[2] = '15.00'
//
// And so on...
//

$item = array ("Item type1", 12.34, 15.00, "Item type2", 12.34, 15.00, "Item type3", 12.34, 15.00, "Item type4", 12.34, 15.00, "Item type5", 12.34, 15.00, "Item type6", 12.34, 15.00, "Item type7", 12.34, 15.00, "Item type8", 12.34, 15.00, "Item type9", 12.34, 15.00, "Item type10", 12.34, 15.00);

print ("
<select size=1 name=name>");
foreach $item /3
{
print ("<option value=$item1[0];'+';$item1[1];'+';$item1[2]>$item1[0]&nbsp;+&nbsp;$item1[1]</option>");
}
print("
</select>
");

?>

Can anyone help me out with this one?

Thanks in advance...
B.

bored
06-29-2004, 10:05 PM
$arr = array("1", "2", "3");

foreach($arr as $option){

$options .= "<option value=\"$option\">$option</option>\r\n";

}


<select name="array">
<?=$options?>
</select>

Hope that helped.

WRStrong
09-10-2004, 03:02 AM
Sorry for such the long delay in my response. I forgot to subscribe to notifications...lol

I found the answer to what I wanted to do:::


<?
$items = array
(
array('Red Hat', '$1.00'),
array('White Hat', '$1.25'),
array('Blue Hat', '$3.00'),
);

print '<select size=1 name=hat>';
print '<option selected></option>';
foreach($items as $item) {
$temp = str_replace(" ", "_", $item[0]);
print ("<option value=$temp$item[1]>$item[0]&nbsp;&nbsp;+&nbsp;$item[1]</option>");}
print '</select>';
?>


Just in case anyone was wanting to do it themselves & I could be of any assistance... :)

Thanks for the help... bored!

Take care,

Bill

raf
09-10-2004, 08:46 AM
running a search here woul have delivered lots of functions that generates a dropdown (always better to use a small function for this sort of stuff you'll frequently gonna need)
example:
http://www.codingforums.com/showpost.php?p=226704&postcount=13