wmguk Posted March 27, 2009 Share Posted March 27, 2009 Hey guys, I have an order_details table and all the items which have been ordered go into it. I'm trying to find out how many items are postid =1 so ive used the code below... only issue is some of the items are qty of 10, so for example sake, say: ITEM1 x 5 (postid 1) ITEM2 x 10 (postid 1) ITEM3 x 8 (postid 3) I need to know that there are 15 items postid 1 and 8 postid 3 - but the script only says 2 items postid 1 and 1 item postid 3... because there is only 1 record... how can i get it to work it out using the qty field too? $countsql = "SELECT * , count(`postid`) as Counter FROM `orders_details` WHERE order_id = '$ordid' GROUP BY `postid`"; echo $countsql; $r_counts = mysql_query($countsql); while($coun = mysql_fetch_array($r_counts)) { $postid = $coun['postid']; $counter = $coun['Counter']; echo $counter; } Link to comment https://forums.phpfreaks.com/topic/151378-simple-count/ Share on other sites More sharing options...
Yesideez Posted March 27, 2009 Share Posted March 27, 2009 Remove the GROUP BY on the end - should work if I read your post right. Link to comment https://forums.phpfreaks.com/topic/151378-simple-count/#findComment-795089 Share on other sites More sharing options...
wmguk Posted March 27, 2009 Author Share Posted March 27, 2009 Hi, I tried that but dont get any result now... what i need to do is get the count to also check qty in the DB and add the qty... Link to comment https://forums.phpfreaks.com/topic/151378-simple-count/#findComment-795093 Share on other sites More sharing options...
steelaz Posted March 27, 2009 Share Posted March 27, 2009 Try adding to your query: SUM(`post_id`) as Sum Link to comment https://forums.phpfreaks.com/topic/151378-simple-count/#findComment-795096 Share on other sites More sharing options...
revraz Posted March 27, 2009 Share Posted March 27, 2009 The GROUP BY clause is required in this type of query. See if this works. "SELECT postid, count(*) as Counter FROM `orders_details` WHERE order_id = '$ordid' GROUP BY `postid`"; Link to comment https://forums.phpfreaks.com/topic/151378-simple-count/#findComment-795099 Share on other sites More sharing options...
wmguk Posted March 27, 2009 Author Share Posted March 27, 2009 The GROUP BY clause is required in this type of query. See if this works. "SELECT postid, count(*) as Counter FROM `orders_details` WHERE order_id = '$ordid' GROUP BY `postid`"; Ok, I tried this one, and I got 2 1 so it knows that the order is 2 x postid 1 and 1 x postid 3 however it needs to work out the qty too... so postid 1 should be 15..... ITEM1 x 5 (postid 1) ITEM2 x 10 (postid 1) ITEM3 x 8 (postid 3) Link to comment https://forums.phpfreaks.com/topic/151378-simple-count/#findComment-795102 Share on other sites More sharing options...
wmguk Posted March 27, 2009 Author Share Posted March 27, 2009 Try adding to your query: SUM(`post_id`) as Sum tried this but didnt get any output, again, i dont think this will take account of the qty man this is making my brain ache... must be a way to do it though! Link to comment https://forums.phpfreaks.com/topic/151378-simple-count/#findComment-795103 Share on other sites More sharing options...
wmguk Posted March 27, 2009 Author Share Posted March 27, 2009 currently using $countsql = "SELECT postid, count(*) as Counter FROM `orders_details` WHERE order_id = '$ordid' GROUP BY `postid`"; echo $countsql; $r_counts = mysql_query($countsql); while($coun = mysql_fetch_array($r_counts)) { $postid = $coun['postid']; $counter = $coun['Counter']; $qty = $coun['prodqty']; //$counter = ($counter * qty); echo $counter; } but doesnt work either.... Link to comment https://forums.phpfreaks.com/topic/151378-simple-count/#findComment-795107 Share on other sites More sharing options...
steelaz Posted March 27, 2009 Share Posted March 27, 2009 This - "$qty = $coun['prodqty'];" doesn't work because you don't have "prodqty" in SELECT query. Again, try: SELECT postid, COUNT(*) as Counter, SUM(prodqty) as Quantity FROM `orders_details` WHERE order_id = '$ordid' GROUP BY `postid` If it doesn't work, run it in phpMyAdmin (replace $ordid) and let us know what the outcome is. Link to comment https://forums.phpfreaks.com/topic/151378-simple-count/#findComment-795121 Share on other sites More sharing options...
wmguk Posted March 27, 2009 Author Share Posted March 27, 2009 This - "$qty = $coun['prodqty'];" doesn't work because you don't have "prodqty" in SELECT query. Again, try: SELECT postid, COUNT(*) as Counter, SUM(prodqty) as Quantity FROM `orders_details` WHERE order_id = '$ordid' GROUP BY `postid` If it doesn't work, run it in phpMyAdmin (replace $ordid) and let us know what the outcome is. Ok, in myadmin sql... it works!!!! postid Counter Quantity 1 2 11 2 1 1 3 1 1 however on my php page i get 0 0 0 $countsql = "SELECT postid, COUNT(*) as Counter, SUM(prodqty) as Quantity FROM `orders_details` WHERE order_id = '$ordid' GROUP BY `postid`"; echo $countsql."<br>"; $r_counts = mysql_query($countsql); while($coun = mysql_fetch_array($r_counts)) { $postid = $coun['postid']; $counter = $coun['Counter']; $qty = $coun['Quantity']; $counter2 = ($counter * qty); echo "Count: " .$counter2."<br>"; } Link to comment https://forums.phpfreaks.com/topic/151378-simple-count/#findComment-795125 Share on other sites More sharing options...
steelaz Posted March 27, 2009 Share Posted March 27, 2009 You forgot dollar sign before your variable name: $counter2 = ($counter * $qty); Link to comment https://forums.phpfreaks.com/topic/151378-simple-count/#findComment-795132 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.