Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dropdown menu from array... help ...

Status
Not open for further replies.

wrstrong

Programmer
Joined
Jun 18, 2002
Messages
3
Location
US
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.
 
I see several problems with your code.

First is that what you want to do will be much easier if you store your data in a multidimensional array.

Second is that I'm not sure what that foreach statement is going to do.

Try:

Code:
<?php
$items = array
(
	array('Item type1', 12.34, 15.00),
	array('Item type2', 12.34, 15.00),
	array('Item type3', 12.34, 15.00),
	array('Item type4', 12.34, 15.00),
	array('Item type5', 12.34, 15.00),
	array('Item type6', 12.34, 15.00),
	array('Item type7', 12.34, 15.00),
	array('Item type8', 12.34, 15.00),
	array('Item type9', 12.34, 15.00),
	array('Item type10', 12.34, 15.00)
);

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

?>

Which will output:

Code:
<select size="1" name="name">
	<option value="Item type1+12.34+15">Item type1&nbsp;+&nbsp;12.34</option>
	<option value="Item type2+12.34+15">Item type2&nbsp;+&nbsp;12.34</option>
	<option value="Item type3+12.34+15">Item type3&nbsp;+&nbsp;12.34</option>
	<option value="Item type4+12.34+15">Item type4&nbsp;+&nbsp;12.34</option>
	<option value="Item type5+12.34+15">Item type5&nbsp;+&nbsp;12.34</option>
	<option value="Item type6+12.34+15">Item type6&nbsp;+&nbsp;12.34</option>
	<option value="Item type7+12.34+15">Item type7&nbsp;+&nbsp;12.34</option>
	<option value="Item type8+12.34+15">Item type8&nbsp;+&nbsp;12.34</option>
	<option value="Item type9+12.34+15">Item type9&nbsp;+&nbsp;12.34</option>
	<option value="Item type10+12.34+15">Item type10&nbsp;+&nbsp;12.34</option>
</select>



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
That did it! I have read up on the multiple array, but couldn't understand it for the life of me... until now. Thanks a bunch!

Take care,

B.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top