Jump to content

Merged duplicate data


rusdy

Recommended Posts

Here my MySQL format

 

product             | total
------------------------------------------
puma,adidas         | 100.00,125.00     
puma                | 80.00
reebok,adidas,puma  | 70.00,100.00,125.00
adidas,umbro        | 125.00,56.00      

 

How to combine, split, merge the product title and total it like this in php?

 

puma     485.00
adidas   350.00
reebook  70.00
umbro    56.00

 

My current code will display the Puma in many time. How to fix it?

 

<?php
$select = "SELECT product,total FROM orders WHERE status=2";
$query  = $db->rq($select);
while ($order = $db->fetch($query)) {
    $item     = $order['product'];
    $quantity = $order['qty'];
    $total    = $order['total'];
    $item     = split(",", $item);
    $total    = split(",", $total);
    for ($i = 0; $i < count($item); ) {
?>


<tr>
<th scope="row" class="spec"><?php echo $item[$i]; ?></th>
<td>$ <?php echo $total[$i]; ?></td>
</tr>
<?php $i++;  } } ?>
<tr>
<th colspan="1" class="spec" scope="row">Total</th>
<td style="text-align:center;">Sum Of Total Here</td>
</tr>

 

Link to comment
https://forums.phpfreaks.com/topic/200754-merged-duplicate-data/
Share on other sites

  • 2 weeks later...

Try something like this:

<?php
$total = array();
while ($order = $db->fetch($query)) {
     $tmp_prd = explode(',',$order['product']);
     $tmp_ttl = explode(',',$order['total']);
     for ($i=0;$i<count($tmp_prd);++$i) {
           if (isset($total[$tmp_prd[$i]]) {
              $total[$tmp_prd[$i]] += $tmp_ttl[$i];
           } else {
              $total[$tmp_prd[$i]] = $tmp_ttl[$i];
           }
     }
}
//
//  customize the following for your own code
//
foreach($total as $prd => $ttl) {
   echo $prd . ': ' . $ttl . "<br>\n";
}
?>

 

Note: untested

 

Ken

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.