MYSQL将行提取到不同的表

时间:2015-05-12 21:17:10

标签: 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>";
    }
&#13;
&#13;
&#13;

$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个)新闻 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:2)

您需要为每种类型的记录创建一个单独的表吗?很简单,如果您按类型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
}