从获取的数据查询数组中动态使用列名
Dynamically use column names from fetched data query array
我有一个数组,其中包含从数据库查询中获取的数据:
Array
(
[0] => stdClass Object
(
[dataid] => 925977
[camp_id] => 2003001
[cid] =>
[userid] => ushio12
[ip_client] => 36.74.213.75
[register_time] => 2015-04-01 22:39:04
[country] => Indonesia
[game_name] => TOUCH
[server] => SALSA
[nominal] => Rp 100000
[logintime] => 2015-04-01 22:39:12
)
//...
[2] => stdClass Object
(
[dataid] => 929703
[camp_id] => 2003001
[cid] =>
[userid] => fiqri179
[ip_client] => 125.162.70.16
[register_time] => 2015-04-02 23:40:23
[country] => Indonesia
[game_name] => TOUCH
[server] => K-POP
[nominal] => Rp 100000
[logintime] => 2015-04-02 23:40:26
)
)
到目前为止,根据上面的数组结果,我像这样手动获取视图中的数据:
<tr>
<th>dataid</td>
<th>camp_id</td>
<th>cid</td>
<th>ip_client</td>
<th>register_time</td>
<th>country</td>
<th>game_name</th>
<th>server</th>
</tr>
<?php
if(isset($result))
{
foreach ($result as $row)
{
echo "<tr>";
echo "<td>".$row->dataid."</td>";
echo "<td>".$row->camp_id."</td>";
echo "<td>".$row->cid."</td>";
echo "<td>".$row->ip_client."</td>";
echo "<td>".$row->register_time."</td>";
echo "<td>".$row->country."</td>";
echo "<td>".$row->game_name."</td>";
echo "<td>".$row->server."</td>";
echo "</tr>";
}
}
?>
因为我知道用户会搜索什么信息。
但是,这一次,我不知道用户将搜索什么(返回哪些列)。他们可能只搜索某些特定信息或整个信息,因此数组结果将包含比现在更少或更多的列。
所以我的问题是,如何根据用户尝试搜索的键(列名)数组自动生成视图,以便<th>".$key."</th>
是动态的?
这应该适合你:
这里我只是将数组中索引为 0 的第一个 stdClass 转换为数组,然后我可以用 array_keys()
.
获取所有键
<?php
$columns = array_keys((array)$result[0]);
echo "<tr>";
foreach($columns as $column)
echo "<th>$column</th>";
echo "</tr>";
?>
编辑:
对于行,您可以像这样执行相同的操作:
foreach ($result as $row) {
echo "<tr>";
foreach($columns as $column)
echo "<td>" . $row->$column . "</td>";
echo "</tr>";
}
我有一个数组,其中包含从数据库查询中获取的数据:
Array
(
[0] => stdClass Object
(
[dataid] => 925977
[camp_id] => 2003001
[cid] =>
[userid] => ushio12
[ip_client] => 36.74.213.75
[register_time] => 2015-04-01 22:39:04
[country] => Indonesia
[game_name] => TOUCH
[server] => SALSA
[nominal] => Rp 100000
[logintime] => 2015-04-01 22:39:12
)
//...
[2] => stdClass Object
(
[dataid] => 929703
[camp_id] => 2003001
[cid] =>
[userid] => fiqri179
[ip_client] => 125.162.70.16
[register_time] => 2015-04-02 23:40:23
[country] => Indonesia
[game_name] => TOUCH
[server] => K-POP
[nominal] => Rp 100000
[logintime] => 2015-04-02 23:40:26
)
)
到目前为止,根据上面的数组结果,我像这样手动获取视图中的数据:
<tr>
<th>dataid</td>
<th>camp_id</td>
<th>cid</td>
<th>ip_client</td>
<th>register_time</td>
<th>country</td>
<th>game_name</th>
<th>server</th>
</tr>
<?php
if(isset($result))
{
foreach ($result as $row)
{
echo "<tr>";
echo "<td>".$row->dataid."</td>";
echo "<td>".$row->camp_id."</td>";
echo "<td>".$row->cid."</td>";
echo "<td>".$row->ip_client."</td>";
echo "<td>".$row->register_time."</td>";
echo "<td>".$row->country."</td>";
echo "<td>".$row->game_name."</td>";
echo "<td>".$row->server."</td>";
echo "</tr>";
}
}
?>
因为我知道用户会搜索什么信息。
但是,这一次,我不知道用户将搜索什么(返回哪些列)。他们可能只搜索某些特定信息或整个信息,因此数组结果将包含比现在更少或更多的列。
所以我的问题是,如何根据用户尝试搜索的键(列名)数组自动生成视图,以便<th>".$key."</th>
是动态的?
这应该适合你:
这里我只是将数组中索引为 0 的第一个 stdClass 转换为数组,然后我可以用 array_keys()
.
<?php
$columns = array_keys((array)$result[0]);
echo "<tr>";
foreach($columns as $column)
echo "<th>$column</th>";
echo "</tr>";
?>
编辑:
对于行,您可以像这样执行相同的操作:
foreach ($result as $row) {
echo "<tr>";
foreach($columns as $column)
echo "<td>" . $row->$column . "</td>";
echo "</tr>";
}