user3447733
user3447733

Reputation: 41

grab a value from a database, and use a forloop to fill a drop down menu in php

I've created a rather large dynamic site as a project, and everything is working, except one function.

I'm using PHP to display an HTML table that populates from my database. I can grab the value of ip_stock, which is an int value, I want to take that value, and forloop through it, to populate a drop down menu for each item to be purchased in the table. Each item will have a different value in ['ip_stock']. Here is my code, minus some of the HTML at the top, which is not very relative, but the connection is made and successful, my forloop doesn't work, and doesn't work if I put it in the selection code itself.

   <?php 

    $pagerows = 5;

    if (isset($_GET['p']) && is_numeric($_GET['p'])) {
        $pages=$_GET['p']; 
    } else {
        $query = "SELECT COUNT(ip_id) FROM in_product"; 
        $result = mysqli_query($dbhandle, $query); 
        $rows = @mysqli_fetch_array($result, MYSQLI_NUM); 
        $records = $rows[0]; 

    if ($records > $pagerows) {
        $pages = ceil($records/$pagerows); 
    }else{ 
        $pages = 1; 
        }
    }

    if (isset($_GET['s']) && is_numeric ($_GET['s'])) {
        $start = $_GET['s']; 
    }else{ 
        $start = 0; 
    } 


    echo "<table class='tableadmin2'> 
    <tr>
    <td class='td2'><b>Add&nbsp;to&nbsp;Cart: </b></td>
    <td class='td2'><b>Product Information: </b></td> 
    <td class='td2'><b>Product Photo: </b></td>
    <td class='td2'><b>Select Amount: </b></td>
    </tr>";

    $display6="SELECT * 
        FROM in_product LIMIT $start, $pagerows; " ; 
    $displayResult6 = @mysqli_query($dbhandle, $display6)
                or die(mysqli_error($dbhandle));

    while($row6 = mysqli_fetch_array($displayResult6, MYSQLI_ASSOC)) { 
    $i = 1;
    $x = $row6['ip_stock'];
    $y = " ";
    for($i = 1; $i <= $x; $i++) {
    $y = "<option value=' . $i . '> $i </option>";
     }            
    echo "<tr>
    <input type='hidden' id='prod' value='" . $row6['ip_id'] . "' /></td>
    <td class='td2'><a href='addcartInstate.php?ip_id=" . $row6['ip_id'] . "'>Add To Cart&nbsp</a></td>
    <td class='td2'><strong> " . $row6['ip_name'] . " </strong><br> " . $row6['ip_desc'] . " <br> $" . $row6['ip_price'] . " </td>
    <td class='td2'><img alt='first' src=" . $row6['ip_image'] . " width='300' height='250'></td>
     <td class='td2'><br>
         <b>Quantity:&nbsp;&nbsp;</b><br>
                <select id='orderIn_quantity' name='orderIn_quantity'>
                    <option value='Select'> Select </option> 
                    $y                                              
                </select>
     </td>
    </tr>";
   }
    echo "</table>";

    if(isset($_POST['orderIn_quantity'])) {
        $orderIn_quantity = $_POST['orderIn_quantity'];
        } 

    if($pages > 1) { 
        echo '<p class="table3">'; 
        $current = ($start/$pagerows) + 1;
        if ($current != 1) { 
            echo '<a href="instate.php?s=' . ($start - $pagerows) . '&p=' . $pages . '">Previous Page </a>'; 
        }  
        if ($current != $pages) { 
            echo '<a href="instate.php?s=' . ($start + $pagerows) . '&p=' . $pages . '">Next Page </a> '; 
        } 
    } 
?>

Upvotes: 0

Views: 72

Answers (2)

Steve
Steve

Reputation: 20469

You need to append to $y not overwrite it. Also you should probably stick to eiter concatenation or variable interpretation when echoing strings, so when you output $y do it with concatenation:

$y='';
for($i = 1; $i <= $x; $i++) {
    $y =. "<option value='" . $i . "'>". $i ."</option>";
 }            
echo "<tr>
<input type='hidden' id='prod' value='" . $row6['ip_id'] . "' /></td>
<td class='td2'><a href='addcartInstate.php?ip_id=" . $row6['ip_id'] . "'>Add To Cart&nbsp</a></td>
<td class='td2'><strong> " . $row6['ip_name'] . " </strong><br> " . $row6['ip_desc'] . " <br> $" . $row6['ip_price'] . " </td>
<td class='td2'><img alt='first' src=" . $row6['ip_image'] . " width='300' height='250'></td>
 <td class='td2'><br>
     <b>Quantity:&nbsp;&nbsp;</b><br>
            <select id='orderIn_quantity' name='orderIn_quantity'>
                <option value='Select'> Select </option> 
                ". $y ."                                            
            </select>
 </td>
</tr>";

Upvotes: 0

realshadow
realshadow

Reputation: 2585

As far I can see it will display only the last value, correct? Because you keep overwriting your previous option with the new one. You need to use string concatenation.

You need to change

$y = "<option value=' . $i . '> $i </option>";

to

// this will append new string to previous string
$y .= "<option value=' . $i . '> $i </option>";

Upvotes: 2

Related Questions