【发布时间】:2011-10-20 12:40:24
【问题描述】:
我正在用 php 构建一个数据分析工具。我正在查询我们的生产服务器,它是 Postgres 并显示数据。
我在一个循环中运行我的选择查询(日期作为变量传递),它显示如下输出:
第一次迭代:
Country sum(yesterday)
India 4500
Southafrica 5000
第二次迭代:
country sum(day before)
India 5000
Southafrica 7000
Japan 4000
我想在这样的表格中显示它。
Country yesterday daybefore
India 4500 5000
Southafrica 5000 7000
Japan empty 4000
我已经根据本教程编写了 DAL
http://net.tutsplus.com/tutorials/php/simple-php-class-based-querying/
任何帮助都会很棒。
Query sample : this is run twice in a loop where $date = array('1','2')
query : select c.country_name, sum(tf.tx_amount_usd)
from
table1 tf,
table2 tp,
table3 d,
table4 c
where
tf.condition = tp.condition
and d.day = current_date-$date
group by 1,2 order by 2
获取数据:
$results = array();
while ($row = pg_fetch_array($res)){
$result = new DALQueryResult();
foreach ($row as $k=>$v){
$result->$k = $v;
}
$results[] = $result;
}
return $results;
显示数据:
$dal = new DAL();
$dates = array('1','2');
foreach ($dates as $date) {
$results = $dal->get_trans_by_date($date);
echo "<h1>Data</h1>";
// check if there were any results
if ($results) {
// cycle through results
foreach ($results as $model) {
/* echo "<pre>";print_r($results);echo "</pre>"; */
echo "<li>$model->country_name ".number_format($model->sum,2)."</li>";
}
}
else {
// Display a message concerning lack of data
echo "<p>No Query Output.</p>";
}
}
【问题讨论】:
-
如果您提供一些用于检索和输出数据的代码,将更容易给出答案。
标签: php html sql postgresql