Question : Adding option and value to dynamically to dropdown menu

I am dynamically  populating a dropdown menu.

The values are gathered from a database using the explode function. So if the database says "small, medium, large" these appear as a list in the dropdown.

Currently both the option and value fields of the dropdown are both populated by the same query but i need to have the 'value' field reading from another query.

To explode the query i use:


$sizes = $row_rs_items['size'];
$dropdownOptions = explode(",", $sizes);


The dropdown then reads:

           <select name="size">
      <? foreach ($dropdownOptions as $option): ?>
      <option value="<?=trim($option)?>"><?=trim($option)?></option>
      <? endforeach; ?>
</select>

So you can see that 'size' fills both the option and value for the dropdown.

How would i keep size as the option but use another query called 'price' for the value?

The aim being that if someone selects a size from the drop the corresponding price will be added to the cart.

I am using PHP and MYSQL

Answer : Adding option and value to dynamically to dropdown menu

Hey BrighteyesDesign,

Here is how I normally would do it:

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
<?php

/*  Testing database code:

create table products (
id int auto_increment not null,
size varchar(20) null,
price varchar(20) null,
primary key (id)
);

insert into products (size, price) values ('small', '2.33');
insert into products (size, price) values ('medium', '3.23');
insert into products (size, price) values ('large', '4.35');
*/

$query = "select size, price from products";
$result = mysql_query($query);

echo '<select name="size">';
while($row = mysql_fetch_array($result)){
	echo '<option value="' .  trim($row['size'])  . '">' . trim($row['price']) . '</option>';
}
echo '</select>';

?>
Random Solutions  
 
programming4us programming4us