2011-10-20 3 views
0

저는 PHP로 데이터 분석 도구를 구축하고 있습니다. Postgres와 데이터를 표시하는 프로덕션 서버를 쿼리하고 있습니다.PostgreSQL이 쿼리에서 가져온 데이터를 표시합니다.

1 반복 :

Country  sum(yesterday) 
India  4500 
Southafrica 5000 

2 반복 :

country  sum(day before) 
India  5000 
Southafrica 7000 
Japan  4000  

나는 다음과 같은 출력을 표시 루프에서 내 선택 쿼리를 (날짜 변수로 전달됩니다) 실행 해요

이렇게 테이블에 표시하고 싶습니다.

Country   yesterday daybefore 
India   4500  5000 
Southafrica  5000  7000  
Japan   empty  4000 

나는 어떤 도움이 좋을 것

http://net.tutsplus.com/tutorials/php/simple-php-class-based-querying/

이 튜토리얼에 따라 DAL를 작성했습니다.

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>"; 
    } 
} 
+0

데이터를 검색하고 출력하는 데 사용하는 코드를 제공하면 대답하기가 더 쉬울 것입니다. –

답변

0

그것은 당신이 뭘 하려는지 약간 불분명하다. 이러한 모든 태그와 우리는 PHP에서 데이터를 테이블로 작성하거나 Postgres에서 즉시 수행하도록 요청하고 있습니다.

당신은 포스트 그레스에서 의미 가정하면, 여기 당신이 원하는 것을 달성하기위한 하나의 방법입니다

@> SELECT name AS country, yesterday_sum, daybefore_sum \ 
    FROM countries C \ 
    INNER JOIN iteration2 A ON C.id = A.country \ 
    LEFT JOIN iteration1 B ON C.id = B.country; 

    country | yesterday_sum | daybefore_sum 
-------------+---------------+--------------- 
India  |   4500 |   5000 
Southafrica |   5000 |   7000 
Japan  |    |   4000 
(3 rows) 

(나는 테이블을 생성하고 출력에 따라 시험 데이터 삽입) 다시

하지만은, 애매한 질문으로 인해 나는 이것이 당신이 요구하고있는 것이 아닌지 확신 할 수 있습니다.

관련 문제