【发布时间】: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