【问题标题】:Combine multiple SQL columns into a single column of an HTML table using PHP使用 PHP 将多个 SQL 列组合成 HTML 表的单个列
【发布时间】:2016-05-07 03:48:19
【问题描述】:

是否可以将 MySQL 表中多列的数据合并到 HTML 表中的单个列中,其中每条记录将是 HTML 表中的新行?

在这个例子中,MySQL 中的一个表有两列(Col1,Col2)。 Col1 中的数据显示在 HTML 表的第一列中。 MySQL 中 Col2 的数据显示在 HTML 表的第二列中。换言之,HTML 表与 MySQL 表的布局相匹配。

<?php
$con = new mysqli('domain', 'username', 'password', 'database');
$sql = "select * from table1";
$sql_query = mysqli_query($con, $sql);

while ($row = mysqli_fetch_array($sql_query)) {
  $col1 = $row['col1'];
  $col2 = $row['col2'];

  echo "<table>";
  echo "<tr>";
  echo "<td> $col1 </td>";
  echo "<td> $col2 </td>";
  echo "</tr>";
  echo "</table>";
}
?>

MySQL 表:

| - - - - | - - - - - - - |
| Col1    |    Col2       |
| - - - - | - - - - - - - |
| Blue    |    Car        |
| Green   |    Truck      |
| Yellow  |    Van        |
| - - - - | - - - - - - - |

HTML 表格:

| - - - - | - - - - - - - |
| Column1 |    Column2    |
| - - - - | - - - - - - - |
| Blue    |    Car        |
| Green   |    Truck      |
| Yellow  |    Van        |
| - - - - | - - - - - - - |

如果将 $col1 和 $col2 放在单个 TD 标记内,这确实会使 $col1 和 $col2 都显示在 HTML 表的 Col1 中。但是,$col1 和 $col2 都显示在同一个单元格中。

<?php
$con = new mysqli('domain', 'username', 'password', 'database');
$sql = "select * from table1";
$sql_query = mysqli_query($con, $sql);

while ($row = mysqli_fetch_array($sql_query)) {
  $col1 = $row['col1'];
  $col2 = $row['col2'];

  echo "<table>";
  echo "<tr>";
  echo "<td> $col1 $col2 </td>";
  echo "</tr>";
  echo "</table>";
}
?>

HTML 表格:

| - - - - - - - - - - - - |
| Column1                 |
| - - - - - - - - - - - - |
| Blue Car                |
| Green Truck             |
| Yellow Van              |
| - - - - - - - - - - - - |

是否可以在 HTML 表的 Column1 中回显 $Col1 和 $Col2 并使每条记录在 HTML 表中的自己的行中?

| - - - - - - - - - - - - |
| Column1                 |
| - - - - - - - - - - - - |
| Blue                    |
| Green                   |
| Yellow                  |
| Car                     |
| Truck                   |
| Van                     |
| - - - - - - - - - - - - |

【问题讨论】:

  • 你想要你问题中最后一个数字的结果吗??

标签: php html mysql


【解决方案1】:

不需要有两个循环。它可以在单循环中完成。将不同变量中的 col1 和 col2 值分开。最后打印出来。

<?php
$con = new mysqli('domain', 'username', 'password', 'database');
$sql = "select * from table1";
$sql_query = mysqli_query($con, $sql);
$str = "<table>";
$str_col2 = "";
while ($row = mysqli_fetch_array($sql_query)) {
  $col1 = $row['col1'];
  $col2 = $row['col2'];    

  $str .= "<tr><td> $col1 </td></tr>";
  $str_col2 .= "<tr><td> $col2 </td></tr>";
}
echo $str . $str_col2 . "</table>";
?>

【讨论】:

  • 这非常有效。非常感谢 Aju 的解决方案!
  • 很高兴听到...!!谢谢:)
  • 你说的是数据库中结果为空的场景吗?
【解决方案2】:

您的 HTML 有效,但不正确。这可能是您的结果表的结构。

创建一个新数组来存储col2 值,以便在所有col1 值之后显示。在为显示的第二个col2 值再次完成col1 值循环后。

$col2 = array();
echo "<table>";
while ($row = mysqli_fetch_array($sql_query)) {
  $col1 = $row['col1'];
  $col2[] = $row['col2'];

  echo "<tr>";
  echo "<td> $col1 </td>";
  echo "</tr>";
}
for($i = 0; $ < count($col2); $i++){
  echo "<tr>";
  echo "<td> $col2[$i] </td>";
  echo "</tr>";
}
echo "</table>";

【讨论】:

  • 这是一个令人着迷的解决方案。我没有考虑在“while”循环之后添加“for”循环。我将对此进行实验。这实际上对我正在处理的场景也很有用,因为我实际上简化了这个问题以使问题更容易消化:)
  • 是的,@AjuJohn 对这个问题有很好的答案,你必须和他一起去。
猜你喜欢
  • 1970-01-01
  • 2016-07-16
  • 2014-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
  • 2023-03-09
  • 1970-01-01
相关资源
最近更新 更多