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 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据