我有这段代码,但我不知道如何解决这个问题......
当我在我的网站上运行时,点击此链接
<a href="#add"> <img src="image/addtocart.png" width="50%" style="margin-left: 170px;"></a>
来自$row[product_name]
将不会存储在此课程中
<!---- POPUP #CHOOSE SIZE ----->
<a href="#x" class="overlay" id="add"></a>
<div class="box">
<h2><span style="color:blue;font-weight:bold">Choose Size</span></h2>
<?php echo "<h5>$row[product_name]</h5></br>"; ?>
<?php
$sql1="SELECT product_size, product_price FROM products where product_name='$row[product_name]'";
foreach ($conn->query($sql1) as $row1){
echo " $row1[product_price]";
echo "<p><button name='submit' type='submit' class='a_demo_five' value='$row1[product_size]'>$row1[product_size] P$row1[product_price].00</button></p>";
}
?>
这是我的原始代码:
<?php
include 'core/admin/admin_db.php';
$sql="SELECT DISTINCT product_name, product_description, product_image FROM products WHERE product_category = 'Chicken' GROUP BY product_name";
echo"<div class='scroll2'>";
foreach($conn->query($sql) as $row){
echo "<li>";
echo "<div class='grow2 pic'><img src='$row[product_image]'></div>";
echo "<h5>$row[product_name]</h5></br>";
?>
<a href="#add"> <img src="image/addtocart.png" width="50%" style="margin-left: 170px;"></a>
<!---- POPUP #CHOOSE SIZE ----->
<a href="#x" class="overlay" id="add"></a>
<div class="box">
<h2><span style="color:blue;font-weight:bold">Choose Size</span></h2>
<?php echo "<h5>$row[product_name]</h5></br>"; ?>
<?php
$sql1="SELECT product_size, product_price FROM products where product_name='$row[product_name]'";
foreach ($conn->query($sql1) as $row1){
echo " $row1[product_price]";
echo "<p><button name='submit' type='submit' class='a_demo_five' value='$row1[product_size]'>$row1[product_size] P$row1[product_price].00</button></p>";
}
?>
提前致谢!
答案 0 :(得分:1)
您不应将变量括在双引号中,因为它们是用于字符串的。你也错过了一些结束括号,这使得整个代码无法运作。
请注意,您必须为不同的项使用不同的ID,否则页面只会在您单击“添加”时加载第一个类。
<?php
include 'core/admin/admin_db.php';
$sql = "SELECT DISTINCT product_name, product_description, product_image FROM products WHERE product_category = 'Chicken' GROUP BY product_name";
echo "<div class='scroll2'>";
foreach($conn->query($sql) as $row){
$product_name = $row['product_name'];
echo "<li>";
echo "<div class='grow2 pic'><img src='".$row['product_image']."'></div>";
echo "<h5>".$product_name."</h5>";
echo "<a href='#add_".$row['product_id']."'><img src='image/addtocart.png' style='width:50%;margin-left:170px;'></a>";
// POPUP #CHOOSE SIZE
echo "<a href='#x' class='overlay' id='add_".$row["product_id"]."'></a>";
echo "<div class='box'>";
echo "<h2><span style='color:blue;font-weight:bold'>Choose Size</span></h2>";
echo "<h5>".$product_name."</h5>";
$sql1="SELECT product_size, product_price FROM products WHERE product_name='$product_name'";
foreach ($conn->query($sql1) as $row1){
echo $row1['product_price'];
echo "<p><button name='submit' type='submit' class='a_demo_five' value='".$row1['product_size']."'>".$row1['product_size']." P".$row1['product_price'].".00</button></p>";
}
echo "</div>";
echo "</li>";
}
echo "</div>";
?>