Prolem in Rate this Item Script...!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
asad_black
Forum Newbie
Posts: 20
Joined: Wed Feb 11, 2009 1:59 am

Prolem in Rate this Item Script...!

Post by asad_black »

hi...!
em working on this site http://jugnoo.tv and currently working on the rate this video script..!

when you visit the following link you will see the list of video.
http://jugnoo.tv/category_activity.php

when you click on any thumbnail of video the video will be played and you will also see the rate this video on the bottom of the player...!

now my problem is that: this script work on the one video means when rate any video it will show same rating on every video...!

Code: Select all

<?php
 $vid_id = $_GET['id'];
$con = mysql_connect("localhost","sample","sample");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("sample", $con);
 
$result = mysql_query("SELECT * FROM links WHERE id =$vid_id");
 
echo "<table border='0'>
<tr valign=top>
 
 
</tr>";
 
while($row = mysql_fetch_array($result))
  {
  echo "<tr valign=top>";
 
  //echo "<td>" . $row['kw'] . "</td>";
 //echo "<td>" . $row['des'] . "</td>";
  echo "<td>" . $row['embed'] . "</td>";
   echo "<td width='20'></td>";
   echo "<td class=ddesc>" . $row['ddes'] . "</td>";
  //echo "<td valgin=top >" . $row['text'] . "</td></center>";
  echo "</tr>";
  }
echo "</table>";
 
mysql_close($con);
?></td>
  </tr>
</table>
 
    
</td>
    <td bgcolor="#FFFFFF">&nbsp;</td>
  </tr>
  <tr bgcolor="#FF0000">
    <td valign="top" bgcolor="#FFFFFF">&nbsp;</td>
    <td height="35" colspan="2" valign="top" bgcolor="#FFFFFF">
 
//************THIS IS RATING SCRIPT******//
    <?php rating_form("test2"); ?><br>
 
//************end of script******//
<?php
    include("lowermenu.php");
    ?></td>
the above code is my code where include the rate script

and this is actual code of rating:

Code: Select all

 
<?php
class rating{
 
    public $average = 0;
    public $votes;
    public $status;
    public $table;
    private $path;
    
    function __construct($table){
        try{
            $pathinfo = pathinfo(__FILE__);
            $this->path = realpath($pathinfo['dirname']) . "/database/ratings.sqlite";
            $dbh = new PDO("sqlite:$this->path");
            $this->table = $dbh->quote($table);
            // check if table needs to be created
            $table_check = $dbh->query("SELECT * FROM $this->table WHERE id='1'");
            if(!$table_check){
                // create database table
                $dbh->query("CREATE TABLE $this->table (id INTEGER PRIMARY KEY, rating FLOAT(3,2), ip VARCHAR(15))");
                $dbh->query("INSERT INTO $this->table (rating, ip) VALUES (0, 'master')");              
            } else {
                $this->average = $table_check->fetchColumn(1);
            }
            $this->votes = ($dbh->query("SELECT COUNT(*) FROM $this->table")->fetchColumn()-1);
        }catch( PDOException $exception ){
                die($exception->getMessage());
        }
        $dbh = NULL;        
    }
 
    function set_score($score, $ip){
        try{
            $dbh = new PDO("sqlite:$this->path");
            $voted = $dbh->query("SELECT id FROM $this->table WHERE ip='$ip'");
            if(sizeof($voted->fetchAll())==0){
                
                $dbh->query("INSERT INTO $this->table (rating, ip) VALUES ($score, '$ip')");
                $this->votes++;
                
                //cache average in the master row
                $statement = $dbh->query("SELECT rating FROM $this->table");
                $total = $quantity = 0;
                $row = $statement->fetch(); //skip the master row
                while($row = $statement->fetch()){
                    $total = $total + $row[0];
                    $quantity++;
                }
                $this->average = round((($total*20)/$quantity),0);
                $statement = $dbh->query("UPDATE $this->table SET rating = $this->average WHERE id=1");
                $this->status = '(thanks!)';
            } else {
                $this->status = '(already scored)';
            }
            
        }catch( PDOException $exception ){
                die($exception->getMessage());
        }
        $dbh = NULL;
    }
}
 
function rating_form($table){
    $ip = $_SERVER["REMOTE_ADDR"];
    if(!isset($table) && isset($_GET['table'])){
        $table = $_GET['table'];
    }
    $rating = new rating($table);
    $status = "<div class='score'>
                <a class='score1' href='?score=1&table=$table&user=$ip'>1</a>
                <a class='score2' href='?score=2&table=$table&user=$ip'>2</a>
                <a class='score3' href='?score=3&table=$table&user=$ip'>3</a>
                <a class='score4' href='?score=4&table=$table&user=$ip'>4</a>
                <a class='score5' href='?score=5&table=$table&user=$ip'>5</a>
            </div>
    ";
    if(isset($_GET['score'])){
        $score = $_GET['score'];
        if(is_numeric($score) && $score <=5 && $score >=1 && ($table==$_GET['table']) && isset($_GET["user"]) && $ip==$_GET["user"]){
            $rating->set_score($score, $ip);
            $status = $rating->status;
        }
    }
    if(!isset($_GET['update'])){ echo "<div class='rating_wrapper'>"; }
    ?>
    <div class="sp_rating">
        <div class="rating">Rating:</div>
        <div class="base"><div class="average" style="width:<?php echo $rating->average; ?>%"><?php echo $rating->average; ?></div></div>
        <div class="votes"><?php echo $rating->votes; ?> votes</div>
        <div class="status">
            <?php echo $status; ?>
        </div>
    </div>
    <?php
    if(!isset($_GET['update'])){ echo "</div>"; }
}
 
if(isset($_GET['update'])&&isset($_GET['table'])){
    rating_form($_GET['table']);
}
 
 
i adopt the rate script from
http://www.colorpowered.com/colorrating/
Post Reply