大家好我已经制作了一个包含两列35行的mysql表, 现在问题是我想跨越或合并其中的一些列,但我不知道该怎么做 这是我的代码
$result = mysqli_query($con,"SELECT * FROM Spec");
echo "<table border='1' id='spec'>
<tr>
<th class='th'>Engine</th>
<th class='th'>Spec</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='td1'>" . $row['Engine'] . "</td>";
echo "<td class='td'>" . $row['Spec'] . "</td>";
echo "</tr>";
}
if($row['Engine'] = $row['Suspension'])
{
echo "<td colspan='2'>" . $row['Suspension'] . "</td>";
}
echo "</table>";
mysqli_close($con);
答案 0 :(得分:0)
您的逻辑存在一些问题以及一些代码问题。既然你没有准确解释你的数据格式是什么以及你想要实现什么样的布局,我会在两个版本都有一个bash,你可以测试它们,看看哪个适合你想要的布局。
此版本修复了IF语句以正确使用==未赋值=的比较运算符 如果引擎和悬挂列值匹配,它还会创建一个新的表格行,因此您最终会看到类似这样的内容
| ENGINE | SPEC |
| SUSPENSION |
$result = mysqli_query($con,"SELECT * FROM Spec");
echo "<table border='1' id='spec'>
<tr>
<th class='th'>Engine</th>
<th class='th'>Spec</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td class='td1'>" . $row['Engine'] . "</td>";
echo "<td class='td'>" . $row['Spec'] . "</td>";
echo "</tr>";
if($row['Engine'] == $row['Suspension']) {
echo "<tr><td colspan='2'>" . $row['Suspension'] . "</td></tr>";
}
}
echo "</table>";
mysqli_close($con);
当然,它依赖于您的Suspension列的内容与您的引擎列的内容完全相同,我会打赌它们没有,但您必须解决您的逻辑
另一种方式就是这样。此版本会显示| ENGINE | SPEC |
,或者如果您的引擎列与您的暂停列相匹配,则只会显示| SUSPENSION |
列。
$result = mysqli_query($con,"SELECT * FROM Spec");
echo "<table border='1' id='spec'>
<tr>
<th class='th'>Engine</th>
<th class='th'>Spec</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
if($row['Engine'] == $row['Suspension']) {
echo "<tr><td colspan='2'>" . $row['Suspension'] . "</td></tr>";
} else {
echo "<tr>";
echo "<td class='td1'>" . $row['Engine'] . "</td>";
echo "<td class='td'>" . $row['Spec'] . "</td>";
echo "</tr>";
}
}
echo "</table>";
mysqli_close($con);
虽然我怀疑两者都不会为你提供你想要的东西但我认为你根本不需要IF()声明
答案 1 :(得分:0)
在while循环中有问题,请相应更新。
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='td1'>" . $row['Engine'] . "</td>";
echo "<td class='td'>" . $row['Spec'] . "</td>";
echo "</tr>";
}
if($row['Engine'] = $row['Suspension'])
{
echo "<tr>";
echo "<td colspan='2'>" . $row['Suspension'] . "</td>";
echo "</tr>";
}
echo "</table>";