Joe Chavez
Joe Chavez

Reputation: 11

Displaying mysql rows in php dropdown menu?

I am trying to get php to display the items in my table in a drop down menu by doing a foreach loop. It almost working but it displays the items in separate dropdown menus when I want them to be all be displayed in the same menu.

<?php
$product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
if (!empty($product_array)) { 
    foreach($product_array as $key=>$value){
?>
    <form method="post" action="index.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
    <select id="Select_Product" name="Select_Product" class="select">

            <?
            echo '<option value=$product_array[$key]["code"]>'.$product_array[$key]["name"].'</option>'; 
        }
                }
            ?>
    </select>
            <div>Amount:<input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" class="btnAddAction" /></div>
    </form>

Upvotes: 0

Views: 87

Answers (5)

Poiz
Poiz

Reputation: 7617

Try this:

<form method="post" action="index.php?action=add&code=<?php echo $code; ?>">
    <select id="Select_Product" name="Select_Product" class="select">
<?php
    // YOU SHOULD REMOVE THE FORM & SELECT TAGS FROM THE FOREACH LOOP... THAT SEEMS TO BE THE ISSUE HERE
    // DO IT LIKE SO, THEN...
    // IF YOU NEED THE PRODUCT-CODE ON THE FORM'S ACTION ATTRIBUTE, 
    // YOU MAY WANT TO USE JAVASCRIPT TO WORK-AROUND IT SINCE THE VALUE OF 
    // THE SELECT OPTIONS CORRESPONDS TO A SPECIFIC CODE 

    $product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");
    if (!empty($product_array)) { 
        foreach($product_array as $key=>$value){
    ?>

        <?php
            echo '<option data-code="' . $product_array[$key]["code"] . '" value="' . $product_array[$key]["code"] . '">'.$product_array[$key]["name"].'</option>';
        }
    }
            ?>
    </select>
    <div>Amount:<input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" class="btnAddAction" /></div>
</form>

Upvotes: 0

Rafael Mota
Rafael Mota

Reputation: 127

try this

<?php $product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC"); ?>
    <?php if (!empty($product_array)) {  ?>
        <form method="post" action="index.php?action=add">
        <select id="Select_Product" name="Select_Product" class="select">
        <?php foreach($product_array as $product){ ?>
            <option value="<?php echo $product['code']; ?>"><?php echo $product["name"]; ?></option>
        <?php } ?>
        </select>
<?php } ?>

Upvotes: 0

Christian Cerri
Christian Cerri

Reputation: 1275

Move the form and select tags out of the foreach

Upvotes: 0

Sergey Mitroshin
Sergey Mitroshin

Reputation: 113

The problem appears because the <form> and <select> tags are located inside the foreach loop, so they are displayed for each element of the $product_array

You should put them before you call foreach

if (!empty($product_array)) { 
    ?>
    <form method="post" action="index.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
    <select id="Select_Product" name="Select_Product" class="select">
        <?php
        foreach($product_array as $key=>$value){
            ...

Upvotes: 0

The Maniac
The Maniac

Reputation: 2626

You just need to move your form and select outside of the loop:

<?php
$product_array = $db_handle->runQuery("SELECT * FROM tblproduct ORDER BY id ASC");

if (!empty($product_array)):
?>
    <form method="post" action="index.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
    <select id="Select_Product" name="Select_Product" class="select">
    <?php 
        foreach($product_array as $key=>$value):
            echo '<option value=$product_array[$key]["code"]>'.$product_array[$key]["name"].'</option>'; 
        endforeach;
    ?>    
    </select>
    <div>Amount:<input type="text" name="quantity" value="1" size="2" /><input type="submit" value="Add to cart" class="btnAddAction" /></div>
    </form>

<?php
endif;
?>

I've also switched you over to what I like to call "template php." Instead of using the brackets to open/close logical blocks, you can use a combination of colons and special directives to make your templates much more readable. If you prefer to use brackets, go for it. This is a personal preference sort of thing.

Upvotes: 1

Related Questions