php - 通过 PHP 将 MySql 中的项目插入到 HTML Bootstrap 页面

标签 php html mysql twitter-bootstrap

我正在尝试匹配此 PHP code 的结果进入此Bootstrap HTML structure .

到目前为止,我能够实现的是以 3 列布局打印数据库中的项目,但我不知道如何将此结果与/信息我的 bootstrap html“结构/表”结合起来。我想我需要使用“id”并在每次“echo”(h4)后增加它?但我对 PHP 很陌生。感谢您提供任何实用的建议。

PHP 代码:

        // Run query on connection
        $query = "SELECT
                            product.id,
                            product.name as product_name,
                            product.price,
                            product.type
                FROM        pizza_project.product
                INNER JOIN  pizza_project.product_type on product.type = product_type.id
                where       product.type in (1,2)
                order by    product.type ASC, product.price ASC";


        $result = $con->query($query);
        ?>

        <table>
            <tr>

            <?php
           $split = 0;
           $id = $row['id']; // just an idea I think I need to start with

           if (mysqli_num_rows($result) > 0) {
                foreach ($result as $row) {
                    echo '<td>' . $row['product_name'] . ' ' . $row['id'] . '</td>';

                    $split++;
                    if ($split%3 == 0) {
                        echo '</tr> </tr>';
                    }
                }
            }  
            ?>

            </tr>        
        </table>

最佳答案

<?php                                                                                                                                                                   

$itemsInRow = 0;                                                                                                                                                        
foreach ($result as $row) {                                                                                                                                             

        if ($itemsInRow === 0)                                                                                                                                          
                echo "<div class=\"row text-center\">";                                                                                                                 

        echo "<div class=\"col-md-4\">";                                                                                                                                
        echo "<span class=\"fa-stack fa-4x\">";                                                                                                                         
        echo "<img class=\"product-image\" src=\"" . $row['img'] . "\">";                                                                                               
        echo "</span>";                                                                                                                                                 
        echo "<h4 class=\"service-heading\">" . $row['headline'] . "</h4>";                                                                                             
        echo "<p class=\"text-muted\">" . $row['text']  . "</p>";                                                                                                       
        echo "</div>";                                                                                                                                                  

        if ($itemsInRow === 0)                                                                                                                                          
                echo "</div>";                                                                                                                                          

        if ($itemsInRow % 3 === 0) {                                                                                                                                    
                $itemsInRow = 0;                                                                                                                                        
                continue;                                                                                                                                               
        }                                                                                                                                                               

        $itemsInRow++;                                                                                                                                                  
}

这是您想要实现的目标吗?如果方法正确,我会改进我的答案。这尚未经过测试。

关于php - 通过 PHP 将 MySql 中的项目插入到 HTML Bootstrap 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33980812/

相关文章:

php - 无法理解如何将数组发送到 mySQL 数据表

php - Laravel 使用关系显示 View ,加入

html - 在多个换行中一致地删除填充/边距

javascript - 我怎样才能获取大量经常重复使用的 HTML 源代码?

php - mysql 两个日期之间的索引

MySQL REGEXP 重复字符

php - jQuery Endless/Infinite Scroll 和 PHP-MySQL 教程

php - 无法按照最新的聊天顺序对成员进行排序

javascript - 将MySQL结果放入php变量

html - 使用图像作为按钮上传文件