![](/img/trans.png)
[英]How to arrange data fetched from mysql as per columns in html table?
[英]How can i merge Rows of An HTML table fetched from Mysql
我有一个正在建设的网站的MySQL数据库,我使用以下所示的PHP代码在我的页面上的MySQL数据库中显示了表格的内容。 我想知道的是,是否有一种方法可以合并具有相同值的列的行。
注意:我不希望更改发生在数据库中,而只是在页面上显示的html表上
这是我使用的代码
$arr = array();
while($row = mysql_fetch_row($result)) {
if (empty($arr[$row[0]])) $arr[$row[0]] = array();
$arr[$row[0]] [$row[1]] [$row[2]] [$row[3]] [$row[4]] [$row[5]] [$row[6]][] = $row[7];
}
foreach ($arr as $key => $subordinatearr) {
echo '<tr>';
echo '<td rowspan="', count($subordinatearr), '">', $key, '</td>';
echo '<td>', $subordinatearr[0], '</td>';
echo '<td>', $subordinatearr[1], '</td>';
echo '<td>', $subordinatearr[2], '</td>';
echo '<td>', $subordinatearr[3], '</td>';
echo '<td>', $subordinatearr[4], '</td>';
echo '<td>', $subordinatearr[5], '</td>';
echo '<td>', $subordinatearr[6], '</td>';
if (count($subordinatearr) > 1) {
for ($i = 1; $i < count($subordinatearr); $i++) {
echo '</tr><tr><td>', $subordinatearr[$i], '</td>';
}
}
echo '</tr>';
}
请将此脚本更改为
while($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>".$row['Location']."</td>";
echo "<td>".$row['Vessel ']."</td>";
echo "</tr>";
}
我没有清楚地理解这一部分-“合并具有相同值的列的行”。 如果您不想显示列的重复记录,则可以通过两种方式进行:
将特定列的值保留在数组中。 在显示该列之前,请使用is_array()检查数组中是否存在该值。 如果存在,则跳过,否则显示并添加到数组中。
进行查询时,使用DISTINCT(col_name)仅获取唯一值。
将结果存储在数组中。
$arr = array();
while($row = mysql_fetch_row($result)) {
if (empty($arr[$row[0]]) $arr[$row[0]] = array();
$arr[$row[0]][] = $row[1];
}
然后使用rowspan放置数组,如下所示:
foreach ($arr as $key => $subordinatearr) {
echo '<tr>';
echo '<td rowspan="', count($subordinatearr), '">', $key, '</td>';
echo '<td>', $subordinatearr[0], '</td>';
if (count($subordinatearr) > 1) {
for ($i = 1; $i < count($subordinatearr); $i++) {
echo '</tr><tr><td>', $subordinatearr[$i], '</td>';
}
}
echo '</tr>';
}
如果数据太大,可能会导致内存错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.