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 4 of 4
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,088
    Thanks
    296
    Thanked 12 Times in 12 Posts

    Exclamation using php to add column values in a db?

    Hi All,

    I Am trying to add the values of four columns in my db table together and divide it by a number, then update another table with the total.

    here is what i have
    PHP Code:
        //INSERT NEW TOTALS INTO STORES TABLE
        
    $gettotals "SELECT * FROM rating2 WHERE storeID = '$id'";
        
    $query mysql_query($gettotals)or die(mysql_error());
        
    $row mysql_fetch_array($query);
        
    $totalRating $row['storeValue'] + $row['storeEase'] + $row['storeDelivery'] + $row['storeCharge'] /$row['totalVotes'];
        
    $updateStores "Update stores 
        SET rating = '$totalRating' 
        WHERE storeID = '$id'"
    ;
        
    $runupdate mysql_query($updateStores)or die(mysql_error()); 
    but its not adding the expected results to the other table?

    is my $totalRating wrong? i have checked the values to be added and they have been inserted correctly and i have cross checked the results with the expected results and they dont match.

    any ideas
    thanks
    Luke

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,088
    Thanks
    296
    Thanked 12 Times in 12 Posts
    ok sorted out this little problem

    was updating the old obsolite file not the new version

    so i have this code
    PHP Code:
    $totalRating = ($row['storeValue'] + $row['storeEase'] + $row['storeDelivery'] + $row['storeCharge'])/$row['totalVotes']; 
    how to i get the result to two decimal places???
    thanks

  • #3
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    why not do it in query ?

    PHP Code:
    UPDATE stores 
        SET rating 
    = (SELECT SUM(SUM(storeValue,storeEase,storeDelivery,storeCharge)/totalVotes) AS SUM FROM rating2 WHERE storeID '$id'
    something like that you'll have to look up correct syntax but you should be able to do the entire calc in query including the update and the select.
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,088
    Thanks
    296
    Thanked 12 Times in 12 Posts
    thanks mate, will try that


  •  

    Posting Permissions

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