MySQL and php, adding columns

  • jshaulis
  • Student
  • Student
  • jshaulis
  • Posts: 70

Post 3+ Months Ago

Hello,

This my seem basic or not, I am not sure yet, but I want to add quantities up and have that number displayed. My query is
Code: [ Select ]
Select * FROM 'products' WHERE 'date' LIKE '$search' AND 'location' LIKE '$location1' ORDER BY 'itemnumber' ASC LIMIT 0, 10000

Now when there are like itemnumbers that are ordered I want to add them up and display the totals so we can have the monthly totals in one spot. Is there a easy way to do this. I was thinking about doing something like
Code: [ Select ]
$num=mysql_numrows($result);
$i=0;
while ($i < $num)
{

if ($itemnumber = mysql_result($result,$i +1,"itemnumber");
{
add them up
}
}
  1. $num=mysql_numrows($result);
  2. $i=0;
  3. while ($i < $num)
  4. {
  5. if ($itemnumber = mysql_result($result,$i +1,"itemnumber");
  6. {
  7. add them up
  8. }
  9. }


Is there a easier way? Thanks,

Justin
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • harryhood
  • Graduate
  • Graduate
  • harryhood
  • Posts: 141

Post 3+ Months Ago

How about using the Group By function? I think that would work for you.

http://dev.mysql.com/doc/mysql/en/GROUP ... tions.html
  • jshaulis
  • Student
  • Student
  • jshaulis
  • Posts: 70

Post 3+ Months Ago

Excellent, I am going to have to try that. I will let you know how it goes, THANKS,

Justin
  • jshaulis
  • Student
  • Student
  • jshaulis
  • Posts: 70

Post 3+ Months Ago

OK, I am having a hard time following that page, now I think it means(in my example) to do this,
Code: [ Select ]
Select * FROM 'products' WHERE 'date' LIKE '$search' AND 'location' LIKE '$location1' GROUP BY 'itemnumber' LIMIT 0, 10000

Would that first get the results by date and location, then from those results, group them up if they have the same item number?

Thanks again,

Justin
  • jshaulis
  • Student
  • Student
  • jshaulis
  • Posts: 70

Post 3+ Months Ago

I tried this but it does not add up the quantity like i want it too. Am I missing something? Thanks,

Justin
  • jshaulis
  • Student
  • Student
  • jshaulis
  • Posts: 70

Post 3+ Months Ago

never mind... I forgot the SUM(quantity) I feel dumb now LOL

Justin
  • harryhood
  • Graduate
  • Graduate
  • harryhood
  • Posts: 141

Post 3+ Months Ago

Glad that worked for you. :)

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 140 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.