【问题标题】:MYSQL fetching rows to different tablesMYSQL 获取行到不同的表
【发布时间】:2015-05-12 21:17:10
【问题描述】:

我在这里遇到以下问题。我有多个记录的表。每一行代表某种新闻,所以我需要区分它们。这样做的方法是为这些新闻中的每一个(或某种包装器)制作一个表格,但是我遇到了将每个结果提取到每个表格(包装器)的问题。
我不知道如何告诉 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 个)新闻。
任何帮助将不胜感激

【问题讨论】:

    标签: php mysql row fetch


    【解决方案1】:

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

    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
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 2021-01-15
      相关资源
      最近更新 更多