rusdy Posted May 5, 2010 Share Posted May 5, 2010 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 More sharing options...
rusdy Posted May 13, 2010 Author Share Posted May 13, 2010 Link to comment https://forums.phpfreaks.com/topic/200754-merged-duplicate-data/#findComment-1057548 Share on other sites More sharing options...
kenrbnsn Posted May 13, 2010 Share Posted May 13, 2010 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 Link to comment https://forums.phpfreaks.com/topic/200754-merged-duplicate-data/#findComment-1057550 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.