使用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 - 结果


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


MYYA
浏览 71回答 1
1回答

倚天杖

仅对行进行迭代&nbsp;$statement = $connect->prepare($query);&nbsp; &nbsp; $statement->execute();&nbsp; &nbsp; $result = $statement->fetchAll();&nbsp; &nbsp; $total_row = $statement->rowCount();&nbsp; &nbsp; $output = '';&nbsp; &nbsp; if($total_row > 0)&nbsp; &nbsp; {&nbsp; &nbsp; $output .= '&nbsp; &nbsp; &nbsp; &nbsp; <table class="table">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<thead>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">ID</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Region</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Level</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Status</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">BE</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">RP</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Current Rank</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Previous Rank</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Flex Rank</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Champions Count</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th scope="col">Skin Count</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th scope="col">Last Play</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<th scope="col">Price</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</thead>&nbsp; &nbsp; &nbsp; &nbsp; '&nbsp; &nbsp; &nbsp; &nbsp; foreach($result as $row)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$output .= ' <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>'.$row["id"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>'.$row["region"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>'.$row["level"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>'.$row["account_status"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>'.$row["be"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>'.$row["rp"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>'.$row["current_rank"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>'.$row["previous_rank"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>'.$row["flex_rank"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>'.$row["champions_count"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>'.$row["skins_count"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>'.$row["last_play"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>'.$row["price"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp;$output .= ' </table>'}else{&nbsp; &nbsp; $output = '<h3>No Data Found</h3>';}echo $output;
打开App,查看更多内容
随时随地看视频慕课网APP