在列而不是行中显示php查询

时间:2013-01-15 07:13:16

标签: php html css postgresql

嘿家伙我有一个postgres数据库,我正在连接并从使用PHP获取数据。我是php的新手。但无论如何,我将此作为我的查询

$sql = "SELECT to_char (a.CALLDATE,'yyyymm') as month,min(a.calldate) as   start_time,max(a.calldate) as end_time,
         ceil(SUM (a.CALLDURATION::integer) / 60) AS minutes,
         COUNT (DISTINCT a.IDENTIFIANT) AS distinct_callers,
a.zoneiddest as country_code,b.country
FROM cdr_data a,COUNTRY_CODES b
WHERE  a.CALLSUBCLASS = '002'
     AND  a.CALLCLASS = '008'
and a.zoneiddest::integer > 0
AND SUBSTR (a.CALLEDNUMBER, 1, 2) NOT IN
('77', '78', '75', '70', '71', '41', '31', '39', '76','79')
and not substr(a.zoneiddest , 1 ,3) in ('254','255','256','211','257','250','256')
and trim(a.zoneiddest)  = trim(b.country_code)
GROUP BY to_char (a.CALLDATE,'yyyymm') ,a.zoneiddest,b.country
ORDER BY 1";

我有这个来返回被查询的数据:

// iterate over result set
// print each row
while ($row = pg_fetch_array($result)) {
     echo "Month: " . $row[0] . "<br />";
     echo "Start Time: " . $row[1] . "<br />";
     echo "End Time: " . $row[2] . "<br />";
     echo "Minutes: " . $row[3] . "<br />";
     echo "Distinct Callers: " . $row[4] . "<br />";
     echo "Country Code: " . $row[5] . "<br />";
     echo "Country: " . $row[6] . "<br />";

  }

以此格式返回我的数据

Month: 201212
Start Time: 2012-12-28 15:51:04
End Time: 2012-12-28 15:51:04
Minutes: 0
Distinct Callers: 1
Country Code: 44
Country: United Kingdom of Great Britain and Northern Ireland

但是我希望在表格中有这些数据,其中月份,开始时间,结束时间,分钟,不同的来电者,国家/地区代码和国家/地区是表格中的列标题。我怎样才能做到这一点。

4 个答案:

答案 0 :(得分:2)

请检查以下代码。如果我对您的要求的理解不正确,请告诉我。

   <table>
    <tr>
    <td>Month</td>
    <td>Start Time</td>
    <td>End Time</td>
    <td>Minutes</td>
    <td>Distinct Callers</td>
    <td>Country Code/td>
    <td>Country</td>
    </tr>
   <?php 
    while ($row = pg_fetch_array($result)) {
      echo '<tr>';
         echo "<td>" . $row[0] . "</td>";
         echo "<td>" . $row[1] . "</td>";
         echo "<td>" . $row[2] . "</td>";
         echo "<td>" . $row[3] . "</td>";
         echo "<td>" . $row[4] . "</td>";
         echo "<td>" . $row[5] . "</td>";
         echo "<td>" . $row[6] . "</td>";
     echo '</tr>';
        }
    ?>
</table>

答案 1 :(得分:1)

别忘了添加循环

echo "<table cellpadding='5' cellspacing='1' style='background-color: #D7D2D2;font-size:12px;font-family:Tahoma;'>";
echo "<tr>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Month</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Start Time</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>End Time</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Minutes</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Distinct Callers</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Country Code</th>";
echo "<th style='background-color: #FFFFFF;font-size:11px' align='left'>Country</th>";
echo "</tr>";
echo "<tr>";
echo "<td style='background-color: #FFFFFF;'>$row[0]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[1]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[2]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[3]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[4]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[5]</td>";
echo "<td style='background-color: #FFFFFF;'>$row[6]</td>";
echo "</tr>";
echo "</table>";

答案 2 :(得分:1)

你可以这样做

$output = "<table>";
$output .= "<tr>";
$output .= "<td> Month </td>";
$output .= "<td> Start Time </td>";
$output .= "<td> End Time </td>";
$output .= "<td> Minutes </td>";
$output .= "<td> Distinct Callers </td>";
$output .= "<td> Country Codes </td>";
$output .= "<td> Country </td>";
$output .= "</tr>";

while ($row = pg_fetch_array($result)) {
     $output .= "<tr>";
     $output .= "<td>". $row[0] . "</td>";
     $output .= "<td>". $row[1] . "</td>";
     $output .= "<td>". $row[2] . "</td>";
     $output .= "<td>". $row[3] . "</td>";
     $output .= "<td>". $row[4] . "</td>";
     $output .= "<td>". $row[5] . "</td>";
     $output .= "<td>". $row[6] . "</td>";
     $output .= "</tr>";

  }
  $output .= "</table>";

  echo $output;

希望这有帮助

答案 3 :(得分:1)

echo "<table>";
   echo "<tr>";
       echo "<td>Month</td>";
       echo "<td>Start Time</td>";
       echo "<td>End Time</td>";
       echo "<td>Minutes</td>";
       echo "<td>Distinct Callers</td>";
       echo "<td>Country Code</td>";
       echo "<td>Country</td>";
   echo "</tr>";

   while ($row = pg_fetch_array($result)) 
   {
    echo "<tr>";
      echo "<td>".$row[0]."</td>";
      echo "<td>".$row[1]."</td>";
      echo "<td>".$row[2]."</td>";
      echo "<td>".$row[3]."</td>";
      echo "<td>".$row[4]."</td>";
      echo "<td>".$row[5]."</td>";
      echo "<td>".$row[6]."</td>";
   echo "</tr>";
  }
echo "</table>";