Search code examples
phpjqueryajaxrating-system

Ajax rate system with php


So, I grabbed this code from here. and I changed it a little to fit my db and display the rate based on my idGames, it's working fine, but now I want to do something like this.

How can I display the rates when I enter in the game page and rate that particular game? I have an index.php page, on which I show all my games, and I have a gameview.php that is the path for each game in index.php, so when you click in the game in index that game is displayed in gameview. Is there a way to implement that code to rate the games in gameview.php? I don't know if I'm being clear enough, but take that link as an example, it's exactly what I want. And I know that MYSQL is deprecated, but I don't know MYSQLi or PDO. So if you can help me with this I appreciate.

<?php
include_once('settings.php');
connect();

$query  = "SELECT idGames FROM jogos";
$result = mysql_query($query);
$ids    = array();
while ($row = mysql_fetch_assoc($result)) {
$ids[] = $row['idGames'];
}
?>
<html>
<head>
</head>
 <body>
<?php
for ($i = 0; $i < count($ids); $i++) {
$rating_tableName = 'jogos';
$id               = $ids[$i];

$q = "SELECT total_votes, total_value FROM $rating_tableName WHERE idGames=$id";
$r = mysql_query($q);

if (!$r)
    echo mysql_error();
while ($row = mysql_fetch_array($r)) {

    $v  = $row['total_votes'];
    $tv = $row['total_value'];
    if ($v)
        $rat = $tv / $v;
    else
        $rat = 0;

}

$j  = $ids[$i];
$id = $ids[$i];
echo '<div class="product">
       Rate Item ' . $j . '
        <div id="rating_' . $id . '" class="ratings">';
for ($k = 1; $k < 6; $k++) {
    if ($rat + 1 > $k)
        $class = "star_" . $k . "  ratings_stars ratings_vote";
    else
        $class = "star_" . $k . " ratings_stars ratings_blank";
    echo '<div class="' . $class . '"></div>';
}
echo ' <div class="total_votes"><p class="voted"> Rating:     <strong>' . @number_format($rat) . '</strong>/5 (' . $v . '  vote(s) cast) 
        </div>
    </div></div>';
}
?>
</body>
</html>

Solution

  • Try this to retrieve the ratings count with starts. It will display based on your css style.

    If you user stars with css background u can this line like this

    $ratings = (@round($rs[total_value] / $rs[total_votes],1)) * 20;

    I hove it will helpfull.

    // Query to retrieve rating per game or article

    $query = "SELECT idGames FROM jogos";

    $result = mysql_query($query);

    while ($row = mysql_fetch_assoc($result)) {

    $id = $row['idGames'];
    
    $sql = "SELECT total_votes, total_value FROM $rating_tableName WHERE idGames=$id";
    
    $results = mysql_query($sql);
    
    $rs = mysql_fetch_array($results);
    
    // set clss of star
    
    $ratings = (@round($rs[total_value] / $rs[total_votes],1));
    
    
     <div class="product">
    
        <div id="rating_' . $id . '" class="ratings">
    
        $class = "star_" . $ratings . " ratings_stars ratings_blank";//stars with css style 
    
       <div class="' . $class . '"></div>
    
       Rating: <strong> $ratings / 5 </strong> //  shows count out of 5
    

     </div>
    

    }