0

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>

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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){
            ...

Comments

0

Move the form and select tags out of the foreach

Comments

0

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 } ?>

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.