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

标签 php html css twitter-bootstrap

我一直在尝试循环整个 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/

相关文章:

css - 如何创建网格/平铺 View ?

jquery - 通过在表格上拖动来提醒选定的单元格计数

html - 如何使用CSS选择每个重复的类系列的最后一个子代?

javascript - 设置html页面的最小高度

javascript - 在 php 之前运行 jQuery 脚本

php - getRow() 函数出现 fatal error

php - CSS 不适用于 ajax 响应

php - 当我将空值视为这样的数组时,为什么 PHP 不会提示?

html - 为什么我的 css 文件没有被使用?不显示背景图像

javascript - 将 div 高度设置为等宽 ( javascript )