gfxgfx
 
Please login or register.

Login with username, password and session length
 
gfx gfx
gfxgfx
 
Members, please note that our guidelines are located here.
 
gfx gfx
gfx
484580 Posts in 28410 Topics by 2464 Members - Latest Member: austinrobert April 02, 2020, 11:01:18 AM
*
gfx* Home | Help | Calendar | Login | Register | gfx
gfx
The Gaming Universe  |  General  |  General Chit Chat  |  [MYSQL]Update Row help
gfx
gfxgfx
 

Author Topic: [MYSQL]Update Row help  (Read 215 times)

Offline KingOfHumans

  • Messiah
  • ******
  • Posts: 17,866
    • View Profile
[MYSQL]Update Row help
« on: June 11, 2008, 02:11:00 PM »
After the last script became too complicated I decided to simplify it by adding a Rating field in the Downloads table.
I'm working on a script to average all the ratings and then update that info in the Downloads table.

Code: [Select]
function GetRating()
{
    $ratings = mysql_query("SELECT fileid, AVG(rating) FROM Rating GROUP BY fileid");
    $downloads = mysql_query("SELECT * FROM Downloads");
    $n = 1;
    while($row = mysql_fetch_array($downloads)) {
        $raterow = mysql_fetch_array($ratings);
        $rate = ($raterow['AVG(rating)'] == false ? -1 : $raterow['AVG(rating)']);
        echo "row " . $rate;
       
        if($rate != -1)
        {
            mysql_query("UPDATE Downloads SET rating='{$rate}' WHERE id='{$raterow['fileid']}'");
        }
        else
        {
            mysql_query("UPDATE Downloads SET rating='0' WHERE id='$n'");
        }
        $n++;
    }
}
I got it to update the ratings (when that else is removed). However if nobody added any ratings for that file or there's no ratings at all it will not update the field in the Downloads table. I assuming I need an else, but how do I reset the info for the other fields that have no ratings. The $n method is not working. If I remove the "WHERE" part in the else it sets everything to 0.
So I hope this script is a lot easier to help me with.

The Gaming Universe  |  General  |  General Chit Chat  |  [MYSQL]Update Row help
 

gfxgfx
gfx
SMF 2.0.17 | SMF © 2019, Simple Machines Page created in 0.249 seconds with 21 queries.
Helios Multi © Bloc
gfx
Powered by MySQL Powered by PHP Valid XHTML 1.0! Valid CSS!
anything