Generating a dynamic listbox based on database contents

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cineplex_schweihofer
Forum Newbie
Posts: 2
Joined: Fri Jul 18, 2003 6:55 pm

Generating a dynamic listbox based on database contents

Post by cineplex_schweihofer »

I've got a form that's based on a database. It's an order form with several products listed. There is a "quantity" list box next to each product that allows the user to select the amount to order. The list drops down. So if I had 5 units on hand, for instance, it would display 1,2,3,4,and 5. The user could select one of these.

I want the list box's contents to change based upon the contents of the DB. So if someone purchased a unit, the DB would have only 4 remaining and the listbox would be shortened to 4 options from 1 to 4.

Following is my code for the listbox. Can you see and problems or offer any suggestions? Thank you.

Code: Select all

<?php
	  $qtyE1 = mysql_query("select qty from dtzr0001_inventory where item = 'E1'");
	  $qtyE2 = mysql_query("select qty from dtzr0001_inventory where item = 'E2'");
	  $qtyE3 = mysql_query("select qty from dtzr0001_inventory where item = 'E3'");
	  $qtyE4 = mysql_query("select qty from dtzr0001_inventory where item = 'E4'");


      echo "<select> size=";
	  echo "1";
	  echo "name=";
	  echo "lstQty1";
	  echo "onChange=";
	  echo "calculate(this.name, this.selectedIndex)>";
	  echo "<option selected=";
	  echo "selected";
	  echo ">-</option>";
	 
		    $startindex = 0;
		    $select=array();
			$count = $qtyE1;

		    for($i = 1; $i $count; $i++)
			&#123;
				$select=array_fill($startindex,1,$i);
				$startindex = $startindex + 1;
			&#125;
		  while(list($key,$value)=each($select))
		  &#123;
			  echo "<option>$value</option>";
		  &#125;
?>
Post Reply