"; //Connect to the database $con = mysql_connect($sql_host,$sql_username,$sql_password); //if (!$con) die('Could not connect: ' . mysql_error()); if (!$con) die('Website Under Maintenance: please, try later.'); mysql_select_db($sql_database, $con); //Update the database with User Preferences if (isset($_POST['score']) && !empty($_POST['track'])){ $track=$_POST['track']; $score=$_POST['score']; $tracklist=$_POST['tracklist']; $result = mysql_query("SELECT score FROM iga_torcs_preferences WHERE id_track='$track' AND username='$username'"); if($row = mysql_fetch_array($result)){ //update $oldscore=$row['score']; mysql_query("UPDATE iga_torcs_preferences SET score = $score WHERE id_track='$track' AND username='$username'"); } else { //insert $oldscore=0; mysql_query("INSERT INTO iga_torcs_preferences (id_user,username,id_track,score) VALUES (0,'$username','$track',$score)"); } $diff=$score-$oldscore; mysql_query("UPDATE $table_tracks SET score = score + $diff WHERE id_track=$track"); } //Check if user requests more tracks if (empty($_GET['curtrack'])) $curtrack=0; else $curtrack=$_GET['curtrack']; //Fetch the tracks with less views //print_r($tracklist); if (empty($tracklist)){ $result = mysql_query("SELECT id_track,trackname,no_generation,public_trackname FROM $table_tracks WHERE active=1 ORDER BY view_count LIMIT $curtrack,$num_tracks"); }else{ $result = mysql_query("SELECT id_track,trackname,no_generation FROM $table_tracks WHERE id_track IN ($tracklist) ORDER BY view_count"); } $i=0; $tracklist="0"; while($row = mysql_fetch_array($result)){ $tracks[$i]=$row; $tracklist=$tracklist . "," . $row['id_track']; $i++; } $size=$i; //Update view_count for each track $result = mysql_query("UPDATE $table_tracks SET view_count=view_count+1 WHERE id_track IN ($tracklist)"); //Fetch the preferences of the current user //usare la stringa tracklist per velocizzare la query! TODO $result = mysql_query("SELECT id_track,score FROM iga_torcs_preferences WHERE username='$username'"); $scores=array(); while($row = mysql_fetch_array($result)){ //print_r($row); $t=$row['id_track']; $scores["$t"]=$row['score']; } //Close DB Connection mysql_close($con); function genRandomString() { $length = 4; $characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $string = ""; for ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, strlen($characters))]; } return $string; } ?>