gpt4 book ai didi

php - 如何在php中使用html div标签

转载 作者:太空宇宙 更新时间:2023-11-03 17:28:27 24 4
gpt4 key购买 nike

我一直在尝试循环整个 html div 部分,以便它通过 div 标签创建 12 列(twitter-bootstrap)。在这种情况下,它表明变量“dep”未定义..

 <? php
$q1="select * from product limit 12";
$ret=mysqli_query($mysqli,$q1);
while($dep=mysqli_fetch_assoc($ret)){
?>
<div class="col-sm-4">
<div class="product-image-wrapper">
<div class="single-products">
<div class="productinfo text-center">
<?php echo "<img src=\"images/home/".$dep['product_image']."\" alt='".$dep['product_name']."' />";
echo "<h2>".$dep['product_price']."</h2>";
echo "<p>".$dep['product_name']."</p>";
?>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>";
</div>
<div class="product-overlay">
<div class="overlay-content">
<?php echo "<h2>".$dep['product_price']."</h2>";
echo "<p>".$dep['product_name']."</p>";
?>
<a href="#" class="btn btn-default add-to-cart"><i class="fa fa-shopping-cart"></i>Add to cart</a>
</div>
</div>
</div>
<div class="choose">
<ul class="nav nav-pills nav-justified">
<li><a href=""><i class="fa fa-plus-square"></i>Add to wishlist</a></li>
<li><a href=""><i class="fa fa-plus-square"></i>Add to compare</a></li>
</ul>
</div>
</div>
</div>
}
?>

我也试过这种格式,但它只显示 echo 关键字。在这种情况下,它显示 echo 标签,就好像它们是 html 本身的一部分一样

  <? php
$q1="select * from product";
$ret=mysqli_query($mysqli,$q1);
while($dep=mysqli_fetch_assoc($ret)){

echo '<div class="col-sm-4">';
echo '<div class="product-image-wrapper">';
echo '<div class="single-products">';
echo '<div class="productinfo text-center">';
echo "<img src=\"images/home/".$dep['product_image']."\" alt='".$dep['product_name']."' />";
echo "<h2>".$dep['product_price']."</h2>";
echo "<p>".$dep['product_name']."</p>";
echo "<a href=\"#\" class=\"btn btn-default add-to-cart\"><i class=\"fa fa-shopping-cart\"></i>Add to cart</a>";
echo "</div>";
echo "<div class=\"product-overlay\">";
echo "<div class=\"overlay-content\">";
echo "<h2>".$dep['product_price']."</h2>";
echo "<p>".$dep['product_name']."</p>";
echo "<a href=\"#\" class=\"btn btn-default add-to-cart\"><i class=\"fa fa-shopping-cart\"></i>Add to cart</a>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "<div class=\"choose\">";
echo "<ul class=\"nav nav-pills nav-justified\">";
echo "<li><a href=\"\"><i class=\"fa fa-plus-square\"></i>Add to wishlist</a></li>";
echo"<li><a href=\"\"><i class=\"fa fa-plus-square\"></i>Add to compare</a></li>";
echo "</ul>";
echo "</div>";
echo "</div>";
echo "</div>";
}
?>

我不明白我哪里出错了。请帮忙..

最佳答案

尝试:

<?php
$q1 = "select * from product";
$ret = mysqli_query($mysqli,$q1);
$html = "";
while($dep=mysqli_fetch_assoc($ret)){
$html .= "\t<div class='col-sm-4'>\r\n";
$html .= "\t\t<div class='product-image-wrapper'>\r\n";
$html .= "\t\t\t<div class='single-products'>\r\n";
$html .= "\t\t\t\t<div class='productinfo text-center'>\r\n";
$html .= "\t\t\t\t\t<img src='images/home/{$dep['product_image']}' alt='{$dep['product_name']}' />\r\n";
$html .= "\t\t\t\t\t<h2>{$dep['product_price']}</h2>\r\n";
$html .= "\t\t\t\t\t<p>{$dep['product_name']}</p>\r\n";
$html .= "\t\t\t\t\t<a href='#' class='btn btn-default add-to-cart'><i class='fa fa-shopping-cart'></i>Add to cart</a>\r\n";
$html .= "\t\t\t\t</div>\r\n";
$html .= "\t\t\t\t<div class='product-overlay'>\r\n";
$html .= "\t\t\t\t\t<div class='overlay-content'>\r\n";
$html .= "\t\t\t\t\t\t<h2>{$dep['product_price']}</h2>\r\n";
$html .= "\t\t\t\t\t\t<p>{$dep['product_name']}</p>\r\n";
$html .= "\t\t\t\t\t\t<a href='#' class='btn btn-default add-to-cart'><i class='fa fa-shopping-cart'></i>Add to cart</a>\r\n";
$html .= "\t\t\t\t\t</div>\r\n";
$html .= "\t\t\t\t</div>\r\n";
$html .= "\t\t\t</div>\r\n";
$html .= "\t\t\t<div class='choose'>\r\n";
$html .= "\t\t\t\t<ul class='nav nav-pills nav-justified'>\r\n";
$html .= "\t\t\t\t\t<li><a href='#'><i class='fa fa-plus-square'></i>Add to wishlist</a></li>\r\n";
$html .= "\t\t\t\t\t<li><a href='#'><i class='fa fa-plus-square'></i>Add to compare</a></li>\r\n";
$html .= "\t\t\t\t</ul>\r\n";
$html .= "\t\t\t</div>\r\n";
$html .= "\t\t</div>\r\n";
$html .= "\t</div>\r\n";
}
echo $html;
?>

发现 2 个语法错误,<? php和一个echo"..." . echo 语法最终可能会被 PHP 忽略,但我注意到了。

关于php - 如何在php中使用html div标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31061814/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com