Manoj Kumar
Manoj Kumar

Reputation: 646

Dynamic Data in Drop down

I want to have a drop down the contains the quantity in such a way that if the number of products in stock is 45 then the drop down will show counting from 1-45 in drop down and if suppose 5 products are sold out then the drop down will show 1-40

how this is possible

Upvotes: 0

Views: 348

Answers (2)

filype
filype

Reputation: 8380

on your PHP you would query the database to findout how many products are left in stock.

for example:

   select in_stock from products where id = '$id';

then on your php you can do something like

$in_stock_q = mysql_query("select in_stock from products where id = '$id'");
$in_stock_r = mysql_fetch_assoc($in_stock_q);
$in_stock = $in_stock_r['in_stock'];

printf("<select name=\"in_stock\">");
for($i=1;$i<$in_stock;$i++) {
    printf("<option value=\"%s\">%s</option>", $i, $i);
}
printf("</select>");

Upvotes: 1

Christoph Grimmer
Christoph Grimmer

Reputation: 4313

Assuming you have the number of products stored in $numInStock you can generate a dropdown as follows.

<select>
<?php
    for ($i = 0; $i < $numInStock; $i++){
        echo "<option value='$i'>$i</option>";
    }
?>
</select>

Upvotes: 0

Related Questions