2016-09-16 5 views
-1

私はいくつかのPostgreSQLのクエリを持っており、その出力は以下のようになります。今私はその出力クエリPHPを表示する方法はこれを行うには?次のように 私のPostgreSQLのクエリは次のとおりです。PostgreSQLクエリを使用して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のクエリ上記の出力は次のようになります。私は起動する方法を知らないので、私はPHPで非常にbeginer午前

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

。私はセットアップデータベース接続とその正常に動作していた。私はあなたができる最善のことは、座って、コードの個々の行が何をするか自分自身に(声を出して)読み取ることがあると思い

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の初心者ですので、私はどのように起動するのかわかりません。私はセットアップデータベース接続とその正常に動作していた。私はWebでテーブルを作成するための配列を作成しました(PHPを使用して) – Ram

+0

@ChristianF私はこれまで何をしていたのですか?あなたはそれを整理するために私を助けてもらえますか? – 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_*()機能を使用することをお勧めします。

関連する問題