form dropdown with arrays ... help...

A

Anonymous

Guest
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] + $item1[1]</option>
<option value=$item2[0];'+';$item2[1];'+';$item2[2]>$item2[0] + $item2[1]</option>
<option value=$item3[0];'+';$item3[1];'+';$item3[2]>$item3[0] + $item3[1]</option>
<option value=$item4[0];'+';$item4[1];'+';$item4[2]>$item4[0] + $item4[1]</option>
<option value=$item5[0];'+';$item5[1];'+';$item5[2]>$item5[0] + $item5[1]</option>
<option value=$item6[0];'+';$item6[1];'+';$item6[2]>$item6[0] + $item6[1]</option>
<option value=$item7[0];'+';$item7[1];'+';$item7[2]>$item7[0] + $item7[1]</option>
<option value=$item8[0];'+';$item8[1];'+';$item8[2]>$item8[0] + $item8[1]</option>
<option value=$item9[0];'+';$item9[1];'+';$item9[2]>$item9[0] + $item9[1]</option>
<option value=$item10[0];'+';$item10[1];'+';$item10[2]>$item10[0] + $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] + $item1[1]</option>");
}
print("
</select>
");

?>

Can anyone help me out with this one?

Thanks in advance...
B.
 
You need to learn about what are known as multi-dimensional arrays. You see, an array element can hold anything, including another array, and when you have an array of arrays, it's called a multidimensional array. This is all covered in the documentation, of course. Your situation is a perfect scenario for such an array, because you have a whole bunch of arrays. Really, any scenario in which you have a bunch of variables named $var1, $var2, $var3, $var4, etc. that all have the same data, you want an array.

So, can we make your code less ugly? Of course we can.

Here's one way to do it:

Code:
<?php
$items = array(
  array('Item type1', 12.34, 15),
  array('Item type2', 12.34, 15),
  array('Item type3', 12.34, 15)
  // ... and so on ...
);

echo "<select name=\"name\">\n"; // HTML attributed need quotation marks, btw

foreach($items as $this_item) {
  echo '<option value="' . $this_item[0] /* ... and so on ... */ . '">' . $this_item[1] ."'</option>\n";
}

echo '</select>';
?>

An even better way would be to use associative arrays, so that you don't have to deal with confusing numerical indices. I don't know what the numbers in your code mean, but say that 12.34 is price1 and 15.00 is price2, then you could have an array like this:

Code:
<?php
$items = array(
  array( 'name'   => 'Item type1',
         'price1' => 12.34,
         'price2' => 15 ),
  array( 'name'   => 'Item type2',
         'price1' => 12.34,
         'price2' => 15 ),
  array( 'name'   => 'Item type3',
         'price1' => 12.34,
         'price2' => 15 ),
  // ... and so on ...
);

echo "<select name=\"name\">\n";

foreach($items as $item) {
  echo '<option value="' . $this_item['name'] /* ... and so on ... */ . '">' . $this_item['price1'] ."'</option>\n";
}

echo '</select>';
?>

Learn to use multidimensional and associative arrays, they'll make your life tons easier.
 
Can you please explain a little more about the '$this_item['name']...."?

I understand the first example... but I'm a little lost by the second.

Thanks for the help. I tried to read up and understand multidimensional arrays, but couldn't understand it well enough....until now... just have to understand the associative arrays better, I guess... LOL

Thanks again.

B.
 
WRStrong said:
Can you please explain a little more about the '$this_item['name']...."?

This is an associative array. Instead of each element having a number for a key (e.g. $item[0]), each element has a string for a key, e.g. $item['myname'].

Code:
<?php

// this array is an ordinary array with numeric keys:
$array = array(
  'swirlee',
  'swirlee@php-forum.com'
);

// in order to access an element, we must remember its numeric
// index (or "key"), which is 0 for the first element and 1 for the second

echo $array[0]; // this outputs 'swirlee'
echo $array[1]; // this outputs 'swirlee@php-forum.com'

// but those keys are difficult to remember. if we have an associative
// array, we can give them names, which are easier to remember and
// make your code easier to read

$better_array = array(
  'name' => 'swirlee',
  'email' => 'swirlee@php-forum.com'
);

// in order to access the elements, we just have to remember what
// name we gave them. in this case, it's obvious:

echo $better_array['name']; // this outputs 'swirlee'
echo $better_array['email']; // this outputs 'swirlee@php-forum.com'

?>
 
Back
Top