我正在尝试让php通过执行foreach循环在下拉菜单中显示表格中的项目。它几乎可以工作,但是当我希望它们全部显示在同一个菜单中时,它会在单独的下拉菜单中显示这些项目。
<?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>
答案 0 :(得分:1)
您只需将form
和select
移到循环之外:
<?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;
?>
我也把你转到我喜欢称之为“模板php”的地方。您可以使用冒号和特殊指令的组合来使模板更具可读性,而不是使用括号来打开/关闭逻辑块。如果您更喜欢使用支架,请选择它。这是个人喜欢的事情。
答案 1 :(得分:0)
问题出现是因为<form>
和<select>
标记位于foreach循环内部,因此会为$product_array
你应该在致电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){
...
答案 2 :(得分:0)
移动表单并从foreach中选择标签
答案 3 :(得分:0)
试试这个
<?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 } ?>
答案 4 :(得分:0)
试试这个:
<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>