MySQL and php, adding columns

  • jshaulis
  • Student
  • Student
  • No Avatar
  • Joined: May 05, 2004
  • Posts: 70
  • Status: Offline

Post June 29th, 2004, 8:09 am

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
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 29th, 2004, 8:09 am

  • harryhood
  • Graduate
  • Graduate
  • No Avatar
  • Joined: May 15, 2004
  • Posts: 141
  • Status: Offline

Post June 29th, 2004, 9:26 am

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
  • No Avatar
  • Joined: May 05, 2004
  • Posts: 70
  • Status: Offline

Post June 29th, 2004, 9:45 am

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

Justin
  • jshaulis
  • Student
  • Student
  • No Avatar
  • Joined: May 05, 2004
  • Posts: 70
  • Status: Offline

Post June 29th, 2004, 10:15 am

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
  • No Avatar
  • Joined: May 05, 2004
  • Posts: 70
  • Status: Offline

Post June 29th, 2004, 10:49 am

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
  • No Avatar
  • Joined: May 05, 2004
  • Posts: 70
  • Status: Offline

Post June 29th, 2004, 11:13 am

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

Justin
  • harryhood
  • Graduate
  • Graduate
  • No Avatar
  • Joined: May 15, 2004
  • Posts: 141
  • Status: Offline

Post June 30th, 2004, 4:29 am

Glad that worked for you. :)

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 239 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
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.