Page 1 of 1

Dynamic Listbox based on mySQL DB

Posted: Fri Jul 18, 2003 7:34 pm
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; 
?>

Posted: Sat Aug 02, 2003 8:03 pm
by jmarcv
$select=array_fill($startindex,1,$i);
Why not just

Code: Select all

<?php
$select[$startindex]=$i; 
?>
?
You could also just do this:

Code: Select all

<?php
$select[]=$i; 
?>
Do you use $select elsewhere? Why not just do you option command instead of filling the array and then outputting it.
By the way, this is wrong:

Code: Select all

for($i = 1; $i $count; $i++)
should be

Code: Select all

for($i = 1; $i &lt;= $count; $i++) 
         &#123; 
           echo "&lt;option&gt;$i&lt;/option&gt;"; 
        &#125;