我是HTML CSS的新手,我已经尝试了很多次,但都失败了。请帮帮忙。我想创建这样的表:

我已经在mysql中插入了相关数据,但无法以给定的格式/表格显示数据。
<tbody>
<?php
foreach($record as $row)
{
$pre_stop = $row['pre_stop'];
$current_stop_id = $row['current_stop'];
$cur_stop_name=mysql_query("SELECT stop_name_urd FROM stop WHERE stop_id = '$current_stop_id'");
while($cur_stop=mysql_fetch_array($cur_stop_name))
{
$stop_name=$cur_stop[0];
}
$inter_distance = $row['inter_distance'];
$total_distance += $row['inter_distance'];
echo "<tr>";
echo "<td class='border' style='text-align:center;'>" . $inter_distance . "</td>";
echo "<td class='border' style='text-align:center;'>" . $stop_name . "</td>";
echo "<td class='border' style='text-align:center;'>".$total_distance."</td>";
for ($i=0; $i < 1; $i++) {
echo "<td rowspan='".$count."' class='noBorder' style='text-align:center;'>" . "" . "</td>";
echo "<td class='noBorder'>" . $stop_name . "</td>";
}
}
//echo "<td class='noBorder' style='text-align:center;'>" . $stop_name . "</td>";
//echo "<td></td>";
?>
</tr>
</tbody>发布于 2016-04-06 18:52:12
如果您不熟悉HTML编码,那么我建议您尝试手动创建一个较小版本的表(比如3行),这样您就可以知道生成的HTML代码应该是什么样子的,并且您可以正确地获取SQL数据。然后,您可以将这些知识转换为PHP代码。
不过,一定要注意你的循环:
for ($i=0; $i < 1; $i++) {
echo "<td rowspan='".$count."' class='noBorder' style='text-align:center;'>" . "" . "</td>";
echo "<td class='noBorder'>" . $stop_name . "</td>";
}这只会循环一次,这似乎违背了您的目的,因为您的逻辑检查很奇怪。您的for循环为:
的值加1
这意味着它将只经历一次循环,然后退出循环。
我没有从你的代码示例中看到你是如何获得数据的,但我想你会想要这样的东西:
for ($i=0; $i < $resultCount; $i++) {
echo "<td rowspan='".$count."' class='noBorder' style='text-align:center;'>" . "" . "</td>";
echo "<td class='noBorder'>" . $stop_name . "</td>";
}其中$resultCount等于SQL查询返回的行数。
发布于 2016-04-06 19:00:06
看起来您在回显html时颠倒了列的顺序。而且,在这种方法中使用colspan比rowspan更容易。由于不知道count的初始值,我们将假设标题为0,所有其他行都是1到m(其中m是行数,因为我们将矩阵读取为m x n(行x列))。
总而言之,试试这个:
$count = 0;
$total_distance = 0;
// the header row
echo "<tr>" . PHP_EOL;
// Apply the space on the left
echo "\t<th colspan='" . (count($record) - $count) . "' class='noBorder' style='text-align:right;'></th>" . PHP_EOL;
// Provide headers on the last three columns
echo "\t<th class='border' style='text-align:center;'>Total Distance</th>" . PHP_EOL;
echo "\t<th class='border' style='text-align:center;'>Stop Name</th>" . PHP_EOL;
echo "\t<th class='border' style='text-align:center;'>Inter-Distance</th>" . PHP_EOL;
echo "</tr>" . PHP_EOL;
foreach($record as $row)
{
// get the values
$pre_stop = $row['pre_stop'];
$current_stop_id = $row['current_stop'];
$inter_distance = $row['inter_distance'];
$total_distance += $inter_distance;
// Assume the last stop in the results has the desired stop name
$cur_stop_name=mysql_query("SELECT stop_name_urd FROM stop WHERE stop_id = '$current_stop_id'");
while($cur_stop=mysql_fetch_array($cur_stop_name))
{
$stop_name = $cur_stop[0];
}
// Start the row
echo "<tr>" . PHP_EOL;
// Apply the space on the left
echo "\t<td colspan='" . (count($record) - $count) . "' class='noBorder' style='text-align:right;'>" . $stop_name . "</td>" . PHP_EOL;
// Apply the last three columns
echo "\t<td class='border' style='text-align:center;'>".$total_distance."</td>" . PHP_EOL;
echo "\t<td class='border' style='text-align:center;'>" . $stop_name . "</td>" . PHP_EOL;
echo "\t<td class='border' style='text-align:center;'>" . $inter_distance . "</td>" . PHP_EOL;
// End the row
echo "</tr>" . PHP_EOL;
// increment the count
$count++;
}https://stackoverflow.com/questions/36448660
复制相似问题