dachshund Posted December 8, 2011 Share Posted December 8, 2011 Is it possible to add up a bunch of selected rows? For example, something like this: $sql = "SELECT (and add up) weight FROM store WHERE id LIKE $id" (there are multiple $id's) Obviously that isn't the answer - but hopefully it gives an idea of what I'm after. Thanks Jack Quote Link to comment https://forums.phpfreaks.com/topic/252788-add-up-weights-of-selected-items/ Share on other sites More sharing options...
Drummin Posted December 8, 2011 Share Posted December 8, 2011 You would use SUM(weight) in your query. $sql = "SELECT SUM(weight) FROM store WHERE id LIKE $id" Quote Link to comment https://forums.phpfreaks.com/topic/252788-add-up-weights-of-selected-items/#findComment-1296001 Share on other sites More sharing options...
dachshund Posted December 8, 2011 Author Share Posted December 8, 2011 thanks, and what about if some some rows needed to be included twice. for example if it needed to add the weight of row 1,1,2 and 4? Quote Link to comment https://forums.phpfreaks.com/topic/252788-add-up-weights-of-selected-items/#findComment-1296002 Share on other sites More sharing options...
Drummin Posted December 8, 2011 Share Posted December 8, 2011 Are you talking about different table fields? Or the same "weight" field? If it's the same field then you will get the sum using the same as posted above. If you need several column fields then get the sum for each and add them together. Quote Link to comment https://forums.phpfreaks.com/topic/252788-add-up-weights-of-selected-items/#findComment-1296005 Share on other sites More sharing options...
dachshund Posted December 8, 2011 Author Share Posted December 8, 2011 it's the same field, so for the above example it would need to be (weight of id 1 x 2) + weight of id 2 + weight of id 4 Quote Link to comment https://forums.phpfreaks.com/topic/252788-add-up-weights-of-selected-items/#findComment-1296009 Share on other sites More sharing options...
Drummin Posted December 9, 2011 Share Posted December 9, 2011 Give it a try. SUM(weight) should add up the weight of the queried rows. Quote Link to comment https://forums.phpfreaks.com/topic/252788-add-up-weights-of-selected-items/#findComment-1296011 Share on other sites More sharing options...
dachshund Posted December 9, 2011 Author Share Posted December 9, 2011 this is what i've tried but it's not quite working <?php $basket = $_SESSION['basket']; if ($basket) { $items = explode(',',$basket); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } foreach ($contents as $id=>$qty) { $totalweight = mysql_query("SELECT SUM(weight) FROM store WHERE id LIKE '$id'"); echo $totalweight; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/252788-add-up-weights-of-selected-items/#findComment-1296017 Share on other sites More sharing options...
dachshund Posted December 9, 2011 Author Share Posted December 9, 2011 ok I worked out SUM, but it just echoed out the weight of that item, and didn't times it by the quantity or add all the different items together Quote Link to comment https://forums.phpfreaks.com/topic/252788-add-up-weights-of-selected-items/#findComment-1296023 Share on other sites More sharing options...
dachshund Posted December 9, 2011 Author Share Posted December 9, 2011 i worked it out, it's: $itemweight = $rows['weight'] * $qty; $totalweight += $rows['weight'] * $qty; Quote Link to comment https://forums.phpfreaks.com/topic/252788-add-up-weights-of-selected-items/#findComment-1296025 Share on other sites More sharing options...
kicken Posted December 9, 2011 Share Posted December 9, 2011 Better to use one query rather than in a loop like you did. $ids = array_keys($contents); $sql = 'SELECT id, SUM(weight) as weight FROM store WHERE id IN ('.implode(',', $ids).') GROUP BY id'; $res = mysql_query($sql); $weights = array(); while ($row=mysql_fetch_assoc($res)){ $weights[$row['id']] = $row['weight']; } $totalweight=0; foreach ($contents as $id=>$qty){ $totalweight += $weights[$id] * $qty; } Quote Link to comment https://forums.phpfreaks.com/topic/252788-add-up-weights-of-selected-items/#findComment-1296028 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.