【问题标题】:PHP create HTML table with a while loop from query sqlPHP从查询sql创建带有while循环的HTML表
【发布时间】:2015-07-16 06:37:40
【问题描述】:

我必须进行查询才能获得按付款类型过滤的产品总数

我的桌子:

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 ';
}

有什么解决方案可以完成这项工作吗?谢谢,如果提前:)

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

【问题讨论】:

  • 您能在问题中添加更多代码吗?
  • 那么究竟是什么问题?
  • 我的脚本不起作用,我无法在同一行显示同一产品不同付款方式的行
  • 例如对于 id = 87 的产品,脚本会添加 3 行,但实际上它会添加具有不同付款类型的一行

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


【解决方案1】:

此功能可帮助您创建不考虑设计的表格。只需传递 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>';}

【讨论】:

    【解决方案2】:

    我创建了一个脚本,它也会为你做标题:

    获取数据:

    <?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>
    

    【讨论】:

    • 正如我所说,脚本应该显示同一产品的一行(例如 id = 87 的产品),您可以在主帖中查看示例
    • 好吧,您的问题不是很清楚,您的标题表明您想要一种简单的方法来遍历 SQL 结果。但看起来你实际上想要一种方法来PIVOT 你的数据。在 SQL Server 中查看 PIVOT 会更容易:technet.microsoft.com/en-us/library/…
    • 我认为最简单的循环方法是使用 php ,如果它是相同的产品,我们不必创建新行,否则我们创建一个新行
    • 您正在 PHP 中设计一个游标来运行一组数据。无论如何,SQL 都是基于设置的。
    猜你喜欢
    • 2018-05-21
    • 2017-09-01
    • 2019-05-06
    • 2013-10-27
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多