duanph1978 2016-09-11 19:38
浏览 65
已采纳

如何使用PHP将数据写入多个HTML表列?

I made this code write data into an HTML table:

<?php
//DB Verbindung
$con = mysqli_connect("localhost","root","","altislife");
$header = -1;
// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="SELECT * FROM players";

if ($result=mysqli_query($con,$sql))
{
  echo "<thead><tr>";
  while ($fieldinfo=mysqli_fetch_field($result))
  {
    echo "<th>$fieldinfo->name</th>";
    $header++;
    echo "$header";
  }
  echo "</tr></thead>";
  mysqli_free_result($result);
}

if ($result=mysqli_query($con,$sql))
{
  echo "<tbody>";
  for ($i=0; $i < $header; $i++) {
    while($sql = mysqli_fetch_array($result))
    {
        echo "<tr><td>" . $sql[$i] . "</td><td> ";
    }
  }
  echo "</tbody>";
}

mysqli_close($con);

The problem is that it only fills the first column.

Then I tried this:

<?php
//DB Verbindung
$con = mysqli_connect("localhost","root","","altislife");
$header = -1;
// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="SELECT * FROM players";

if ($result=mysqli_query($con,$sql))
{
  echo "<thead><tr>";
  while ($fieldinfo=mysqli_fetch_field($result))
  {
    echo "<th>$fieldinfo->name</th>";
    $header++;
    echo "$header";
  }
  echo "</tr></thead>";
  mysqli_free_result($result);
}

if ($result=mysqli_query($con,$sql))
{
  echo "<tbody>";
  while($sql = mysqli_fetch_array($result))
  {
    for ($i=0; $i < $header; $i++) {
      echo "<tr><td>" . $sql[$i] . "</td><td> ";
    }
  }
  echo "</tbody>";
}

mysqli_close($con);

The problem with this code is the same as with the first example. Can anyone see my mistake?

  • 写回答

2条回答 默认 最新

  • dongmi1864 2016-09-11 19:45
    关注
    if ($result=mysqli_query($con,$sql))
    {
        $row = mysqli_fetch_assoc($result);
        $html = '<table><tr><th>'.implode('</th><th>',array_keys($row)).'</th></tr>';
        do {
           $html .= '<tr><td>'.implode('</td><td>',$row).'</td></tr>';
        }while($row = mysqli_fetch_assoc($result));
        $html .='</table>';
    }
    echo $html;
    

    Try it this way, and figure out how this works ;-)

    Points to your code:

    • tbody is not table yours frist example has no table tags
    • a row in a table is defined with <tr></tr> your second example misses a close tag
    • thead and tbody are optional and have no impact on the html

    Last point: it is unnesessary to run the sql twice to generate a table

    implode($sep,$arr) implode takes too args and seperator like <th></th> and merges all entries of an array with is like: array('a','b','c') becomes a<th></th>b<th></th>c

    mysqli_fetch_assoc vs mysqli_fetch_array second gives something like [0=>'a',1=>'b'] and the first ['name'=>'a','color'=>'b']

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题