购物车中带有html选项标记的foreach循环php
MYSQL查询:购物车中带有html选项标记的foreach循环php,php,html,foreach,option,cart,Php,Html,Foreach,Option,Cart,MYSQL查询: $sql_product = "SELECT * FROM products WHERE product_id= $product_id"; $sql_result = $dbh->query($sql_product); PHP: foreach($sql\u结果为$row) { $product_id=$row['product_id']; $product_name=$row['product_name']; $price=$row['price'
$sql_product = "SELECT * FROM products WHERE product_id= $product_id";
$sql_result = $dbh->query($sql_product);
PHP:
foreach($sql\u结果为$row)
{
$product_id=$row['product_id'];
$product_name=$row['product_name'];
$price=$row['price'];
$picture=$row['picture'];
//输出
回声“
$product\U名称
美元价格
数量:
1.
2.
3.
";
如果(isset($_POST['amount']))
{
$amount=$_POST['amount'];
回显$amount,“
”;
}
}
呼应“买”;
我对PHP foreach循环中的选项标记有问题。它是一个购物车,表单中有一个HTML选项标记。然后将选项标记中的值转换为名为$amount的变量。但每当我按下按钮时,购物车中所有产品的金额都会发生变化。因此,如果我的购物车中有3种产品,我想购买第一种产品中的1种,第二种和第三种产品中的2种,那么所有产品的变量$amount都会变为1。有人能帮我吗?在for循环中添加计数器或其他东西 差不多
$x=0;
foreach() {
//your code here
<form action='cart.php' method='POST' id='buy'>
Amount: <select name='amount'".$x.">
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
</select>
</form>
//your code here
$x++;
}
到
你可以用那1000美元
`name='amount'".$k."'>`
///*更新*// 正如评论中所说,问题可能是表单是在foreach中创建的。所以我建议在外面创建它。这就是你能做到的。有一些变化,试着找到它们
// output is only $html at the end //
$html = '<form action="cart.php" method="POST" id="buy">';
foreach($sql_result as $row) {
$product_id=$row['product_id'];
$product_name=$row['product_name'];
$price=$row['price'];
$picture=$row['picture'];
$html.='
<div>$product_name</div>
<div>$price</div>
<div>
Amount: <select name="amount'.$row.'">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>';
if (isset($_POST['amount'])) {
$amount = $_POST['amount'];
$html.= $amount.'<br /><br />'; //here string concatennation in php is with .
}
}
$html.='<button type="submit" class="btn btn-primary" form="buy">Buy</button>';
$html.='</form>';
// output happends here
echo $html;
// now all of this above you could wrap in a function and then
// return $html;
//最后的输出仅为$html//
$html='';
foreach($sql\u结果为$row){
$product_id=$row['product_id'];
$product_name=$row['product_name'];
$price=$row['price'];
$picture=$row['picture'];
$html.='
$product\U名称
美元价格
数量:
1.
2.
3.
';
如果(isset($_POST['amount'])){
$amount=$_POST['amount'];
$html.=$amount.
;//这里是php中的字符串concatennation。
}
}
$html.='Buy';
$html.='';
//输出发生在这里
echo$html;
//现在,所有这些都可以封装在一个函数中,然后
//返回$html;
您正在为循环中创建的所有表单分配相同的id(购买),并且每页应该只有一个id。
做一些类似于:
<form action='cart.php' method='POST' id='buy_$row@index'>
并将按钮放入循环中,具有正确的for id:
echo "<button type='submit'class='btn btn-primary' form='buy_$row@index'>Buy</button>";
echo“买”;
你买什么_$row@index”“是吗?它给了我以下通知:注意:第67行的/home/erik/public_html/winkelvangentje.php中的数组到字符串转换。所有产品的产品数量都保持不变。使用您的计数器变量($x),这里的关键是构建一个唯一的数字添加到buy,以创建一个唯一的id标识符,如:id='buy'$x'(buy''0,buy''1,等等)。很抱歉我在之前的回复中使用了这种结构。这是给smarty的。也许我不明白你的意思。我试过你写的东西,但仍然不起作用。。对不起,我是新手,我不知道你的代码背后的逻辑是什么。当用户在下拉列表中选择数量时,您希望发生什么?当用户点击“购买”按钮时,您希望发生什么?答案将取决于这些答案。答案不变。。是否有可能在循环中使用带有select标记的表单,并为购物车中的每个产品提供一个变量或更改的内容?它应该更改!也许还有别的问题。你能不能把你的名字也绕在按钮上,在submit和class='submit'class='btn-btn-primary'之间缺少一个空格我应该修改一下这个部分:如果(isset($\u POST['aantal']){$aantal=$\u POST['aantal'];echo“aantal:,$aantal,}对不起,aantal是dutchvar\u dump($\u POST)中的金额; 看看安塔尔是什么样子
// output is only $html at the end //
$html = '<form action="cart.php" method="POST" id="buy">';
foreach($sql_result as $row) {
$product_id=$row['product_id'];
$product_name=$row['product_name'];
$price=$row['price'];
$picture=$row['picture'];
$html.='
<div>$product_name</div>
<div>$price</div>
<div>
Amount: <select name="amount'.$row.'">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>';
if (isset($_POST['amount'])) {
$amount = $_POST['amount'];
$html.= $amount.'<br /><br />'; //here string concatennation in php is with .
}
}
$html.='<button type="submit" class="btn btn-primary" form="buy">Buy</button>';
$html.='</form>';
// output happends here
echo $html;
// now all of this above you could wrap in a function and then
// return $html;
<form action='cart.php' method='POST' id='buy_$row@index'>
echo "<button type='submit'class='btn btn-primary' form='buy_$row@index'>Buy</button>";