php - 使用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。
我只想在表格中接收来自列的信息,而不是每次都接收列名
解决方案
仅对行进行迭代
$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;
推荐阅读
- reactjs - 在 FlatList 中 React Native 更新状态
- algorithm - 展开方法:当 n = 0 和 2T(n-1) + 1 时 T(n) = 1
- javascript - 如何在反应中为数组中的特定对象设置状态
- python - scikit-learn - SVM.fit 函数的执行永无止境
- sql-server - SQL Server 返回一行,其中包含具有最小和最大数据的列
- python - 密码/解密 Python 初学者程序
- php - 仅重定向一次并保存访问者 ip
- java - [3,5,0,3,4] 中不是有“132”模式吗?
- java - 如何在 2D 平面上随机生成一堆站点,它们之间的空间量大致相同?
- java - 递归打印,没有任何循环