Vinfoster0701 Vinfoster0701 - 3 months ago 7
PHP Question

Echo data retrieved from query to a table PHP based on same value

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 :)

Answer

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