2016-09-16 7 views
-1

나는 PostgreSQL 쿼리를 가지고 있으며 출력은 아래와 같습니다. 이제 출력 쿼리 PHP를 어떻게 표시할까요? 다음과 같이 내 PostgreSQL의 쿼리는 다음과 같습니다PostgreSQL 쿼리를 사용하여 PHP로 테이블을 만드는 방법은 무엇입니까?

____________________________________________ 
|0_to_10| 11_to_50 | greater_than_50 | Total| 
--------+----------+-----------------+------| 
| 100 | 0.0 | 0.0   | 0.4 | 
---------------------------------------------- 

내가 그래서 난 시작하는 방법을 잘 모릅니다 매우 beginer에 PHP에서입니다 :

$query = 'select 
    round(
    100.00 * 
    (sum(case when "WELLTYPE"= 'DT' and ("CONC_ARSC" between 0 and 10) then 1 else 0 end))/(sum(case when "WELLTYPE"= 'DT' then 1 else 0 end)),1) "0-10", 
    round(
    100.00 * 
    (sum(case when "WELLTYPE"= 'DT' and ("CONC_ARSC" between 11 and 50) then 1 else 0 end))/(sum(case when "WELLTYPE"= 'DT' then 1 else 0 end)),1) "11-50", 
    round(
    100.00 * 
    (sum(case when "WELLTYPE"= 'DT' and ("CONC_ARSC" >50) then 1 else 0 end))/(sum(case when "WELLTYPE"= 'DT' then 1 else 0 end)),1) ">50", 
    round(
    100.00 * 
    (sum(case when "WELLTYPE"= 'DT' and ("CONC_ARSC" between 0 and 10) then 1 else 0 end))/(sum(case when ("WELLTYPE"= 'DT' or "WELLTYPE"= 'DW' or "WELLTYPE"= 'FT' or "WELLTYPE"= 'ST') and ("CONC_ARSC" between 0 and 10)then 1 else 0 end)),1) "Total" 
    from public."Arsenic_Test"'; 

PostgreSQL의 위의 쿼리의 결과는 같습니다. 나는 데이터베이스 연결을 설정했다. 난 당신이 할 수있는 가장 좋은 방법은, 앉아서 코드의 각각의 라인은 무엇을 자신에게 (큰 소리로) 읽을 생각

if($_SERVER['REQUEST_METHOD'] == 'POST'){ 

    $db_connection = pg_connect("host=localhost port=5433 dbname=BankeDB user=postgres password=admin"); 
     echo $db_connection; 
    $query = 'select.... (above query) '; 
$result = pg_query($db_connection, $query, $POST); 
    $result = pg_query($db_connection, $query); 
$DT = array('0-10','11-50','>50',total); 
    $result = pg_query($db_connection, $query); 
         while ($DT = pg_fetch_row($result)) { 
       echo "<tr>"; 
         echo "<td>$DT[0]</td>"; 
         echo "<td>$DT[1]</td>"; 
         echo "<td>$DT[2]</td>"; 
         echo "<td>$DT[3]</td>"; 
         echo "<td>$row[4]</td>"; 
       echo "</tr>"; 
       } 

    echo $result;    
       pg_close($db); 
+0

시도한 내용과 발생한 오류를 게시하십시오. 또한 원하는 결과물의 예가 좋을 것입니다. – ChristianF

+0

나는 시작하는 법을 모른다. 그래서 나는 PHP에서 매우 beginer이다. 나는 데이터베이스 연결을 설정했다. 나는 웹 (PHP를 사용하여)에서 테이블을 만들 배열을 만들었습니다 – Ram

+0

@ ChristianF 나는 내가 지금까지 한 짓을 몇 가지 정보를 addes했다. 그것을 소트하도록 도와 주시겠습니까? – Ram

답변

0

(PHP를 사용하는) 웹에서 테이블을 만들 수있는 배열을 만들었다. 실제로하는 일, 당신이하고 싶은 일이 아닌, 당신을 생각해보십시오. while ($DT = pg_fetch_row ($result)) {
그것은 상태는, 그만큼 (whilepg_fetch_row()) 남아있는 행이 존재하는 한, 다음 결과 집합의 다음 행을 페치한다 (:

이것의 간단한 예는, 다음 라인 pg_fetch_row()) $ DT 변수에 저장하십시오. 그 변수에 저장 덮어 쓰기 이전 데이터 ($DT =)

몇 가지 실제 프로그래밍을 할 수 있도록, 이것은 당신에게 당신의 코드가 실제로 무엇을하는지 더 나은 이해를 줄 것이다 이렇게 그냥 뭔가 스틱을 기대하고 그것을 물건을 던지는 없습니다 (공장).

작업해야 할 또 다른 필수 조건은 실제로 수행해야 할 단계 계획입니다. 코드를 작성하기 전에 이렇게하면 코드를 작성하는 것이 훨씬 쉬울 것입니다. 실제로 문제의 대부분을 미리 해결 했으므로 솔루션을 일반 (속기) 영어에서 PHP로 변환하면됩니다.
"pseudo code"이라는 기술로이 작업을 수행 할 수 있습니다.

그건 그렇고, 내가 조금 귀하의 코드를 청소하고 그것에 대해 언급했다. 당신이 시작하는 데 도움하려면

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    // Normally you'll want to remove the connection details from the code you post. 
    $db_connection = pg_connect ("host=localhost port=5433 dbname=BankeDB user=postgres password=admin"); 

    $query = 'select.... (above query) '; 

    // Here you're actually running the query three times in a row, 
    // overwriting the previous result each time. 
    // Also, why "$POST"? 
    $result = pg_query ($db_connection, $query, $POST); 
    $result = pg_query ($db_connection, $query); 
    $result = pg_query ($db_connection, $query); 

    // Temporary variable to hold the output, instead of directly (and irreverably) sending it to the browser. 
    // Using the HereDOC syntax, to make things a bit easier to read. 
    // It's here you want to write out the headers, as they're in their own row. (TR == table row) 
    $htmlOut = <<<outHTML 
<table> 
    <thead> 
     <tr> 
      <th></th> 
      .... 
     </tr> 
    </thead> 
    <tbody> 
outHTML; 

    // Why not just add the table header cells to the output directly? 
    $DT = array ('0-10', '11-50', '>50', total); 

    // As you're overwriting (deleting) the contents of above array here anyway. 
    while ($DT = pg_fetch_row ($result)) { 
     // Again, hereDOC syntax for easier reading. 
     // Though, where do you get the "$row" variable from? 
     $htmlOut .= <<<outHTML 
     <tr> 
      <td>{$DT[0]}</td> 
      <td>{$DT[1]}</td> 
      <td>{$DT[2]}</td> 
      <td>{$DT[3]}</td> 
      <td>{$row[4]}</td> 
     </tr> 
outHTML; 
    } 

    // $result is a PG resource, so I assume this is for debugging purposes only. 
    // echo $result; 

    // You'll want to print out the completed array instead. 
    echo $htmlOut."\t</tbody>\n</table>\n"; 
} 

PS를 : 나는 또한 PDO를 찾고 대신 이전 pg_*() 기능을 사용하는 것이 좋습니다.

관련 문제