php - MYSQL 获取行到不同的表

标签 php mysql row fetch

我在这里遇到以下问题。我有包含多条记录的表。每一行代表某种新闻,因此我需要将其中的一个与另一个区分开来。做到这一点的方法是为每个新闻(或某种包装器)制作一个表,但是我在将每个结果获取到每个表(包装器)时遇到了问题。
我不知道如何告诉 SQL 将记录分开。下面是我的代码块以及表格的设计。

.news{
    width:400px;
    height:auto;
    border: 1px solid red;
    float:left;
}

.news tr:first-child{
    font-weight: bold;
    height:40px;
    text-align: center;
}
.news tr:last-child{
    background-color: whitesmoke;
    height: 200px;
    padding-bottom: auto;
    text-align: center;
}
.details td{
    width:200px;
    height: 40px;
    text-align: center;
}
echo "<table class='news'>";
    while ($row = mysqli_fetch_array($result)) {
        echo "<tr>
                <td colspan='2'>
                    $row[0]
                </td>
            </tr>
            <tr class='details'>
                <td>
                    $row[1]
                </td>
                <td>
                    $row[2]
                </td>
            </tr>    
            <tr>    
                <td colspan='2'>
                    $row[3]
                </td>
            </tr>";
        echo "</table>";
    }

$query = " SELECT headline, date, concat (firstName,' ',lastName), body FROM "
        . "school_info natural join teachers ORDER BY id_school_inf DESC LIMIT 2;";
$result = mysqli_query($conn, $query);

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^这是我的查询。有一个限制,所以我总是可以获取最新的(比如说最后 5 条)新闻。
任何帮助将不胜感激

最佳答案

您需要为您拥有的每种类型的记录创建一个单独的表吗?很简单,如果您按类型排序记录:

SELECT * FROM whatever ORDER BY type_of_record

然后

$previous = null;
while(... fetch from db ...) {
    if ($row['type'] != $previous) {
         ... got new record type, start new table 
    }
    ... display row
    $previous = $row['type']; // store current type for comparison later
}

关于php - MYSQL 获取行到不同的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30201586/

相关文章:

php - 在 PHP 中删除数组项的最佳方法是什么?

mysql - ASP.NET MVC 4、代码优先、MySQL

php - Laravel 5 Seeder - 数据库中的多行

php - Laravel 数据库选择带占位符的下拉列表

php - Zend_Db_Table_Row 类的对象无法转换为字符串

php - 来自数据库的 3 列动态表

php - WordPress 查询结果,其中所有列都与所选 id 的列具有相同的值

javascript - 如何在单击的某些元素上忽略 Bootgrid 单击事件

javascript - JQuery 等高行

php - Laravel Redis 队列忽略限制