【问题标题】:MySQL-PHP display data separately in html tableMySQL-PHP在html表格中单独显示数据
【发布时间】:2016-01-06 16:08:44
【问题描述】:

我正在尝试在 html 页面中使用 PHP 显示 MySQL 表中的数据,将数据包装在 HTML 表中。

我在 MySQL 表中有 11 列和 x 行,其中我只需要 6 列即可将所有行打印为 HTML 表。我能做到。(成功)

我的方法:

$result = mysqli_query($connect, "SELECT order_id, order_name, order_from, order_to, order_pickup, order_dropoff FROM users");

// Printing results in HTML
echo "\n";
while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr id='$line[order_id]'>\n";
    foreach ($line as $col_value) {
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
echo "\n";

输出:

我的问题: 在这 6 列中,我希望 1 列名为 order_id 成为行的 id (tr),所以现在我只想要剩下的 5 个作为 table-cell 的 td's。我不希望 order_id 列的值作为表格单元格。

【问题讨论】:

  • 那不回显?我建议使用if
  • divy3993,也请检查我的答案,看看它是否按预期工作。否则不需要,我会删除我的答案。
  • @Naruto 检查我的答案。

标签: php html mysql


【解决方案1】:

还要检查$line 中的列名:

foreach ($line as $col_name => $col_value) {
    if ($col_name != 'order_id') {
        echo "\t\t<td>$col_value</td>\n";
    }
}

【讨论】:

  • =&gt; 在这里做什么?
  • =&gt; 表示foreach 的键值语法。
【解决方案2】:

好的,有很多解决方案。通常在编写此类代码时,通常会将数据库层与模板层分开,并将信息评估到模板层。

为了您的示例,您可以选择删除 foreach 并单独定义单独的列值,我认为这通常是最好的方法,因为您已经知道所有列并且可以更好地控制何时回显它们:

$result = mysqli_query($connect, "SELECT order_id, order_name, order_from, order_to, order_pickup, order_dropoff FROM users");

// Printing results in HTML
echo "\n";

while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr id='$line[order_id]'>\n";

    echo "\t\t<td>$line['title']</td>\n";
    echo "\t\t<td>$line['second title']</td>\n";

    echo "\t</tr>\n";
}

echo "\n";

我相信用户已经提到的另一种方法是使用 if 陈述。您可以在数组的键上执行此操作:

$result = mysqli_query($connect, "SELECT order_id, order_name, order_from, order_to, order_pickup, order_dropoff FROM users");

// Printing results in HTML
echo "\n";

while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr id='$line[order_id]'>\n";

    foreach ($line as $col_name => $col_value) {
        if ($col_name === 'order_id') {
            continue;
        }
        echo "\t\t<td>$col_value</td>\n";
    }

    echo "\t</tr>\n";
}

echo "\n";

【讨论】:

  • 是的,您的解决方案也很有帮助,因为我知道我的列名。非常感谢!
  • 很高兴为您服务,我已经阅读了其他一些 cmets,我认为您仍在尝试弄清楚 PHP 中的工作原理。有一个很棒的网站可以帮助您调用 [link][codecademy.com]。这应该解释大多数基础知识,包括大多数语法,例如 foreach 中的 =>
【解决方案3】:

您的解决方案很简单,您只需要检查数组键是否为order_id,如果是,则不要回显它:D

echo "\n";
while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {

    echo "\t<tr id='$line[order_id]'>\n";
    foreach ($line as $key => $col_value) {
        if ($key != 'order_id')
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
}
echo "\n";

【讨论】:

  • 你误会了,我想将 order_id 作为相应 tr 的 id(我能够成功实现,你在此处删除)但不要打印为 td"表格单元格”。
【解决方案4】:

我确信我会发布一个更好的答案,但我会发布它只是为了看看这个想法有多简单。

我的代码的缺点是它会检查每个循环语句中的if(),从而增加了代码的复杂性。

基本上,您设置一个bool 变量来保存每个表格行的第一次传递。此变量设置为 false ($pass_one = false)。然后,当我们开始获取列信息时,我们将 bool 变量设置为 true 并跳过第一个元素。在下一列迭代中,我们继续进行。完成列集后,我们将 $pass_one 改回 false 并重复处理其他行。

<?php $result = mysqli_query($connect, "SELECT order_id, order_name, order_from, order_to, order_pickup, order_dropoff FROM users");

$pass_one = false; //you set it  to false

// Printing results in HTML
echo "\n";
while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr id='$line[order_id]'>\n";
    foreach ($line as $col_value) {
        if (!$pass_one) { 
            $pass_one = true; // you pass the first echo, so you set the bool to true
            continue; // skip the first pass, (this statement goes to foreach() loop
        }
        echo "\t\t<td>$col_value</td>\n";
    }
    echo "\t</tr>\n";
    $pass_one = false; // we get a new <tr> so we set the $pass_one for that loop to false
}
echo "\n";

?>

【讨论】:

    猜你喜欢
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多