php - Dynamic value count in dropdown -


i trying create dropdown select menu followed:

i have table of products , each have stock column. let's product has stock of 54. want dropdown start @ option 1 way option 54.

right have this:

<?php ## conection part   $sql = "select * product"; $result = $conn->query($sql);  while ( $row = mysqli_fetch_assoc($result) ) {   ## 1 while loop   ?>     <tr>         <td><input type="checkbox" name="id[]" value="<?= $row['productid'] ?>" /></td>          <td><?php echo $row['productid']; ?></td>         <td><?php echo utf8_encode($row['productname']); ?></td>         <td><?php echo $row['sku']; ?></td>         <select>           <option value="1">1</option>         </select>     </tr> <?php     } $conn->close(); echo "<br>"; ?> 

anyone have idea how accomplish this?

thanks guys!

this done using simple for() loop, change max in for($i=min;$i<=max;$i++) max, ie. $row['stock'].

<select>   <?php for($i=1;$i<=$row['stock'];$i++){ ?>     <option value="<?php echo $i; ?>"><?php echo $i; ?></option>   <?php } ?> </select> 

also, if want account when $row['stock'] 0, show out of stock message -

<?php if($row['stock'] > 0){ ?> <select>   <?php for($i=1;$i<=$row['stock'];$i++){ ?>     <option value="<?php echo $i; ?>"><?php echo $i; ?></option>   <?php } ?> </select> <?php } else { ?> out of stock <?php } ?> 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -