PHP使用查询sql中的while循环创建HTML表

时间:2015-05-06 08:31:22

标签: php sql-server while-loop html-table

我必须进行查询才能获得按付款类型

筛选的产品总数

我的表:

product_id, paymentType     price
87               E          1
87               E          3
87               C          5
87               V          30
100              C          1
359              E          12
359              C          32

我的查询:

$query = select count(*) as qte, paymentType as PT, product_id as PID, sum(price) as PR from mytable 
        group by product_id, paymentType 
        order by product_id 

查询的结果是:

  product_id   paymentType  qte        price
    87         E             2           4
    87         C             1           5
    87         V             1           30
    100        C             1           1
    359        E             1           12
    359        C             1           32

我需要在php上创建表格:

$output .= '<table  border="1" >';
$output .= '<thead>';
    $output .= '<tr>';
        $output .= '<th><div style="width:150px" >product</div></th>';
        $output .= '<th>paymentType E</th>';
        $output .= '<th>Qte E</th>';
        $output .= '<th>total price E</th>';
        $output .= '<th>paymentType C </th>';
        $output .= '<th>Qte C</th>';
        $output .= '<th>total price C</th>';
        $output .= '<th>paymentType v </th>';
        $output .= '<th>Qte V</th>';
        $output .= '<th>total price V</th>';
    $output .= '</tr>';
$output .= '</thead>';
$output .= '<tbody>';

if ($row){


        $old_product_id = -1;  

        do {
                // ID du produit 
                $product_id = $row->PID;

            /*  switch($row->PT){
                    case 'E': {
                         $output .= ' <td  class="center"  >'.$row->PT.'</td>';
                         $output .= ' <td class="center"  >'.$row->qte.' </td>';
                         $output .= ' <td class="center"  >'.$row->PR.'</td>';
                         break;
                    }
                    case 'C: {
                         $output .= ' <td  class="center"  >'.$row->PT.'</td>';
                         $output .= ' <td class="center"  >'.$row->qte.' </td>';
                         $output .= ' <td class="center"  >'.$row->PR.'</td>';
                             break;
                    }
                    case 'V': {
                         $output .= ' <td  class="center"  >'.$row->PT.'</td>';
                         $output .= ' <td class="center"  >'.$row->qte.' </td>';
                         $output .= ' <td class="center"  >'.$row->PR.'</td>';
                         break;
                    }
                }
                */

                 if ($old_product_id == $product_id){
                     $output .= '<tr>';                
                         $output .= ' <td style="width:200px;">'.$row->PID.'</td>';
                         // payment type  E
                         $output .= ' <td  class="center"  >'.$row->PT.'</td>';
                         $output .= ' <td class="center"  >'.$row->qte.' </td>';
                         $output .= ' <td class="center"  >'.$row->PR.'</td>';
                         // payment type C
                         $output .= ' <td  class="center"  >'.$row->PT.'</td>';
                         $output .= ' <td class="center"  >'.$row->qte.' </td>';
                         $output .= ' <td class="center"  >'.$row->PR.'</td>';
                        // payment type V
                        $output .= ' <td  class="center"  >'.$row->PT.'</td>';
                         $output .= ' <td class="center"  >'.$row->qte.' </td>';
                         $output .= ' <td class="center"  >'.$row->PR.'</td>';
                     $output .= '</tr>';

                 }else{

                 }

                 $old_product_id = $product_id;



           }while ($row = sqlsrv_fetch_object($result));

            $output .= '</tr>';
            $output .= '</tbody>';
            $output .= '</table>';


            $this->db->free();

            echo  $output;  

}else {
    echo 'no data found ';
}

解决这个问题的任何解决方案?谢谢,如果提前:)

我的脚本不起作用,我无法在同一行显示同一产品的行具有不同的付款类型

2 个答案:

答案 0 :(得分:1)

我创建了一个脚本,可以为你做标题:

获取数据

<?php 

require('connect.php'); 

$tsql = "SELECT * FROM BLAH";

$stmt = sqlsrv_query($conn, $tsql);

## Get Results ##

$data = array(); ## Variable to hold data.

while (($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) !== false) {
    $data[] = $row;
}

sqlsrv_free_stmt($stmt);

sqlsrv_close($conn);

?>

显示数据:

            <thead>
              <tr>
                <?php
                  foreach(array_keys($data[0]) as $key) {
                    echo "<th>" . $key . "</th>";
                  }
                ?>
              </tr>
            </thead>
            <tbody>
              <?php
                foreach($data as $key) {
                    echo "<tr>";
                      foreach($key as $vals) {
                        echo "<td>" . $vals . "</td>";
                      }
                    echo "</tr>";
                }
              ?>
            </tbody>

答案 1 :(得分:0)

这是一个可以帮助您创建一个不考虑设计的表的函数。只需传递SQL。

呼叫就像这样 -

$fieldname=array("name","Roll");
$query = "Select name,roll from Student";
create_table($query,$fieldname,$id='mytable');


function create_table($query,$fieldname,$id='mytable'){
print' <table id="'.$id.'" class="table table-striped">';
print'<tr>';
for($i=0;$i<sizeof($fieldname);$i++){
print'<th>'.$fieldname[$i].'</th>';
}
print'</tr>';


$result = mysql_query($query);
while($row= mysql_fetch_array($result)){

print ' <tr>';

for($i=0;$i<sizeof($row)/2;$i++){
print'<td>'.$row[$i].'</td>';
}

print' </tr>';

}

print'</table>';}