Jump to content

Recommended Posts

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

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

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.