未加星标

PHP creates an HTML table with a while loop of the sql query

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二04 | 时间 2018 | 作者 红领巾 ] 0人收藏点击收藏

I have to do a query to get the total of product filtred by payment type

My 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

My query:

$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

the result of the query is :

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

I need to create table onphp as :

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

Any solution to get this worK ? thanks if advance :)

my script doesn't work , I can't show the row for the same product with different payment type in the same row

I created a script that would do the headers for you aswell:

Get data: <?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); ?> Display data: <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>

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

代码区博客精选文章
分页:12
转载请注明
本文标题:PHP creates an HTML table with a while loop of the sql query
本站链接:https://www.codesec.net/view/621198.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(43)