dozpox8752 2016-08-22 11:40
浏览 9
已采纳

回显从查询检索到基于相同值的表PHP的数据

So I have this array that consists of

array (size=3)
  0 => 
   array (size=2)
    'id' => string '46' (length=2)
    'ship_product_code' => string '122' (length=6)
    'purchase_order_number' => string 'PO-1' (length=4)
  1 => 
   array (size=2)
    'id' => string '47' (length=2)
    'ship_product_code' => string '123' (length=6)
    'purchase_order_number' => string 'PO-2' (length=4)
  2 => 
   array (size=2)
    'id' => string '50' (length=2)
    'ship_product_code' => string '124' (length=6)
    'purchase_order_number' => string 'PO-2' (length=4)

I wanted to echo a table that is based on the Purchase Order Number. So by given above array, how can I create a table that is like following:

  PO-1 //PO Number
  122 // Product Code

  <tr> <td> </td> </tr>//another td for space 

  PO-2 //PO Number
  123 // Product Code
  124 // Product Code

How can I "group" the result based on the Purchase Order Number?? Have been dealing with it quite a while, keep searching, but can't really find any resource to achieve so.

I have following code that work but not perfectly

  if(count($products)>0){
            $i=0;
            foreach($products as $v){
                $PDFCONTENT .= '<tr> 
                    <td align="center" width="7%"> </td>
                    <td align="center" width="50%">'.$v['purchase_order_number'].'</td>
                </tr>';
                $i++;
                $PDFCONTENT .= '
                    <tr>
                        <td align="center" width="7%">'.$i.'</td>
                        <td align="center" width="50%">'.$v['ship_product_code].'</td>
                        <td align="center" width="7%">'.number_format($v['qty']).'</td>
                        <td width="7%"> </td>
                    </tr>
                    <tr>
                        <td></td>
                    </tr>
                ';
            }
        }

above code will produce following table

 PO-1 
 122

 PO-2
 123

 PO-2
 124

Please note that I edited the array data here, so I can be more straightforward. There is no problem in outputting the data. Only how can I group the based on the PO Number.

Thank you so much for the help :)

  • 写回答

3条回答 默认 最新

  • dongzhi8984 2016-08-22 11:54
    关注

    Use following code snippet. With same PO Number we have now only one record.

    $fianlProducts = [];
    if(count($products)>0){
            foreach($products as $row){
                    $fianlProducts[$row['purchase_order_number']]['purchase_order_number'] = $row['purchase_order_number'];
                    $fianlProducts[$row['purchase_order_number']]['ship_product_code'][] = $row;
            }
            $i=0;
            foreach($fianlProducts as $v){
                    $PDFCONTENT .= '<tr> 
                        <td align="center" width="7%"> </td>
                        <td align="center" width="50%">'.$v['purchase_order_number'].'</td>
                    </tr>';
                    foreach($v['ship_product_code'] as $w){
                            $i++;
                            $PDFCONTENT .= '
                                <tr>
                                    <td align="center" width="7%">'.$i.'</td>
                                    <td align="center" width="50%">'.$w['ship_product_code'].'</td>
                                    <td align="center" width="7%">'.number_format($w['qty']).'</td>
                                    <td width="7%"> </td>
                                </tr>
                                <tr>
                                    <td></td>
                                </tr>
                            ';
                    }                
            }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?