首页 > 解决方案 > 使用php显示数据表的问题

问题描述

我在从 PHP 到 HTML 表中显示数据时遇到问题

我得到了正确的数据,但我不知道如何不为每个结果获取列名。

我的PHP代码:

$statement = $connect->prepare($query);
    $statement->execute();
    $result = $statement->fetchAll();
    $total_row = $statement->rowCount();
    $output = '';
    if($total_row > 0)
    {
        foreach($result as $row)
        {
            $output .= '
<table class="table">
 <thead>
    <tr>
      <th scope="col">ID</th>
      <th scope="col">Region</th>
      <th scope="col">Level</th>
      <th scope="col">Status</th>
      <th scope="col">BE</th>
      <th scope="col">RP</th>
      <th scope="col">Current Rank</th>
      <th scope="col">Previous Rank</th>
      <th scope="col">Flex Rank</th>
      <th scope="col">Champions Count</th>
      <th scope="col">Skin Count</th>
     <th scope="col">Last Play</th>
     <th scope="col">Price</th>
    </tr>
 </thead>
</tr>
<td>'.$row["id"].'</td>
<td>'.$row["region"].'</td>
<td>'.$row["level"].'</td>
<td>'.$row["account_status"].'</td>
<td>'.$row["be"].'</td>
<td>'.$row["rp"].'</td>
<td>'.$row["current_rank"].'</td>
<td>'.$row["previous_rank"].'</td>
<td>'.$row["flex_rank"].'</td>
<td>'.$row["champions_count"].'</td>
<td>'.$row["skins_count"].'</td>
<td>'.$row["last_play"].'</td>
<td>'.$row["price"].'</td>
 ';

        }
    }
    else
    {
        $output = '<h3>No Data Found</h3>';
    }
    echo $output;

HTML:

    <div class="col-md-9">
        <div class="row filter_data">

        </div>
    </div>

filter_data 来自 Ajax。

https://prnt.sc/rd4t3f - 结果

我只想在表格中接收来自列的信息,而不是每次都接收列名

标签: phphtmlhtml-table

解决方案


仅对行进行迭代

 $statement = $connect->prepare($query);
    $statement->execute();
    $result = $statement->fetchAll();
    $total_row = $statement->rowCount();
    $output = '';
    if($total_row > 0)
    {

    $output .= '
        <table class="table">
         <thead>
            <tr>
              <th scope="col">ID</th>
              <th scope="col">Region</th>
              <th scope="col">Level</th>
              <th scope="col">Status</th>
              <th scope="col">BE</th>
              <th scope="col">RP</th>
              <th scope="col">Current Rank</th>
              <th scope="col">Previous Rank</th>
              <th scope="col">Flex Rank</th>
              <th scope="col">Champions Count</th>
              <th scope="col">Skin Count</th>
             <th scope="col">Last Play</th>
             <th scope="col">Price</th>
            </tr>
         </thead>
        '

        foreach($result as $row)
        {
           $output .= ' <tr>
           <td>'.$row["id"].'</td>
           <td>'.$row["region"].'</td>
           <td>'.$row["level"].'</td>
           <td>'.$row["account_status"].'</td>
           <td>'.$row["be"].'</td>
           <td>'.$row["rp"].'</td>
           <td>'.$row["current_rank"].'</td>
           <td>'.$row["previous_rank"].'</td>
           <td>'.$row["flex_rank"].'</td>
           <td>'.$row["champions_count"].'</td>
           <td>'.$row["skins_count"].'</td>
           <td>'.$row["last_play"].'</td>
           <td>'.$row["price"].'</td>
          </tr>
          ';

         }

     $output .= ' </table>'
}
else
{
    $output = '<h3>No Data Found</h3>';
}
echo $output;

推荐阅读