【发布时间】:2015-06-16 00:28:58
【问题描述】:
我有一个从数据库查询中获取数据的数组:
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> 是动态的?
【问题讨论】: