Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10

Thread: Math from Data

  1. #1
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    228
    Thanks
    45
    Thanked 3 Times in 3 Posts

    Math from Data

    Hello how can you get the total of your products.

    EG.. Database.

    client_id, Product_id, Title, Cost

    1,1, Product1, 25.00
    1,2, Product2, 32.00
    etc.

    I was wondering how I can get them from the database and add them together to get a total cost.

    Thanks in advance.

    S.

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    A quick google search returned this:
    http://www.tizag.com/mysqlTutorial/mysqlsum.php

    Exactly what you're looking for.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #3
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    228
    Thanks
    45
    Thanked 3 Times in 3 Posts
    Hey BluePanther.

    It is close... it worked partially..

    but not there yet..

    I need to sum that last column.. get that final total.

    any thoughts.

    S.

  • #4
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by SlayerACC View Post
    Hey BluePanther.

    It is close... it worked partially..

    but not there yet..

    I need to sum that last column.. get that final total.

    any thoughts.

    S.
    I don't get what you mean. SUM(field) should be used to return the summation of the field through all rows. If you want to specify specific fields, use the WHERE clause. If you want to group it by different fields, use the group by.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #5
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    228
    Thanks
    45
    Thanked 3 Times in 3 Posts
    I finally got it figured out..

    Thanks for the help.

    PHP Code:
    while ($row mysql_fetch_assoc($result)){     $sum += $row['Value']; }  echo $sum
    I was meaning get the sum of the column of data.

    Thanks,

    Slayer.
    Last edited by SlayerACC; 01-03-2012 at 07:07 PM.

  • #6
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by SlayerACC View Post
    I finally got it figured out..

    Thanks for the help.

    PHP Code:
    while ($row mysql_fetch_assoc($result)){     $sum += $row['Value']; }  echo $sum
    I was meaning get the sum of the column of data.

    Thanks,

    Slayer.
    Not that you seem to care, lol, but the mysql SUM() function will do exactly that for you. Much faster, much more efficient.

    SELECT SUM(column) as total FROM table
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #7
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    228
    Thanks
    45
    Thanked 3 Times in 3 Posts
    I do care...

    I tried the example you gave and it did not work..

    can you please explain.. I would love to know.

    I could only get it to sum the product... which is fine..

    but I also needed it to sum the column of products as well.

    if there is a better way.. please fill me in.

    Thanks, Slayer.

  • #8
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Apologies, I just got the impression you didn't really care because you jumped the gun for another solution.

    You want the total cost of all columns in the product table? So the total from the two-row example you gave in the first post would be 57?
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • #9
    Regular Coder
    Join Date
    Sep 2009
    Location
    Calgary, Alberta
    Posts
    228
    Thanks
    45
    Thanked 3 Times in 3 Posts
    I am looking for the cost of all the products the client_id has.

    for instance.. if the client bought 2 products from me..

    I want it to list..

    the product number, the name, the cost.

    then below the products purchased.. a total of the column of products purchased.. after that I will add the GST and sum the total.

    then I have to figure out this decimal thing.. dang.

    Yes I am always interested in the "way" to do things. I am no a pro.. just a guy who needs to get things done... not always to best way. but I need to learn.

    Thanks again.. S.

  • #10
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Misunderstood the requirements. Your way is probably alright, but you could add (SUM(cost) as totalCost) in the query that includes 'WHERE client_id=x'. That way, totalCost returned from the query will be the sum of cost for that client_id.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •