doulingna9420 2016-04-29 23:22
浏览 64

在php下拉菜单中显示mysql行?

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条回答 默认 最新

  • dongnaizao8039 2016-04-29 23:27
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加