ajannick Posted June 1, 2007 Share Posted June 1, 2007 Hi, I have a website with products for sale and along side each item I have displayed the number of each of these items that are left in stock. What I want to do is to have a dropdown box so that the user can choose the qty of itmes to order but they should not be able to order more than what is in stock. For example if there are 5 items left then the drop down box must be initialised at 0 and show the numbers 1,2,3,4,5. When a purchase is made then the drop down must be adjusted to match what is left in the database. <?php $link = mysql_connect("mysql", "name", "password"); mysql_select_db("product"); $query = "SELECT qty FROM product WHERE id='1'"; $result = mysql_query($query); echo "<select name=\"Qty\">"; echo "<option selected>0</option>"; while ($line = mysql_fetch_array($result)) { echo "<option>". ??????? ."</option>"; } mysql_free_result($result); mysql_close($link); ?> </select> I have thought about may be a for loop displaying numbers from 1 to the amount left in the database but not sure how to go about it. Any help would be most appreciated. N. Quote Link to comment https://forums.phpfreaks.com/topic/53827-dynamically-populate-a-drop-down-box/ Share on other sites More sharing options...
saf Posted June 1, 2007 Share Posted June 1, 2007 while ($line = mysql_fetch_array($result)) { for($i=1; $i<=$line['qty']; $i++) echo "<option>".$i."</option>"; } Quote Link to comment https://forums.phpfreaks.com/topic/53827-dynamically-populate-a-drop-down-box/#findComment-266099 Share on other sites More sharing options...
ajannick Posted June 1, 2007 Author Share Posted June 1, 2007 Your a star! I've been scratching over that one all night! Quote Link to comment https://forums.phpfreaks.com/topic/53827-dynamically-populate-a-drop-down-box/#findComment-266107 Share on other sites More sharing options...
saf Posted June 1, 2007 Share Posted June 1, 2007 No problem...I remember my days spending all night trying to figure things out Quote Link to comment https://forums.phpfreaks.com/topic/53827-dynamically-populate-a-drop-down-box/#findComment-266121 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.