【问题标题】:how to use html div tags in php如何在php中使用html div标签
【发布时间】:2015-06-25 22:21:35
【问题描述】:

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

我也尝试过这种格式,但它只显示回显关键字。在这种情况下,它显示回显标签就好像它们是 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>";
}
                    ?>

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

【问题讨论】:

  • 欢迎来到 Stack Overflow。有很多方法可以做到这一点。您遇到了什么错误或“出了什么问题”?
  • 也许将 'mysqli_fetch_assoc' 更改为 'mysqli_fetch_array'?
  • 既然你说它显示 echo 标签就好像它们是 html 的一部分,我认为第二个例子中的所有错误都是 php 而不是
  • @billirichards- 是的,它是

标签: php html css twitter-bootstrap


【解决方案1】:

试试:

<?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 个语法错误,&lt;? php 和一个 echo"..."。 echo 语法最终可能会被 PHP 忽略,但我注意到了。

【讨论】:

    【解决方案2】:

    您的第二个代码在&lt;?php 之间有一个空格,否则它们本质上是相同的。或者可能是格式问题?

    【讨论】:

      猜你喜欢
      • 2018-02-15
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-16
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      相关资源
      最近更新 更多