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