-2

大家好,我已经制作了一个由两列和 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);
4

2 回答 2

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 列的内容完全相同,我敢打赌他们不会,但您必须解决您对此的逻辑

另一种方法是这样的。此版本将显示| 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() 语句

于 2013-07-04T10:35:10.767 回答
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>";
于 2013-07-04T10:38:42.607 回答