Location: PHPKode > scripts > Star Rating System class > star-rating-system-class/cls_rate.php
<?php
/*
#################    STAR RATING SYSTEM   ##################
############################################################
CWB PRO			$Name:  $
Revision		$Revision: 1.7 $
Author			$Author: zelteto $
Created 03/01/02        $Date: 2004/01/07 10:18:22 $
Writed by               GraFX (hide@address.com)
Scripts Home:           http://www.grafxsoftware.com
############################################################
File purpose            RATEING SCRIPT FILE
############################################################
*/

/*
//      Creating a star rating system with PHP and MySQL (Version 1.0.3)
//
//      Author: Gregory Boshoff
//
//      The latest version of this script is available for download at.
//      http://www.developers-resources.com/scripts/rating.zip
//
//		Actual version of star rating system with PHP and MySQL (Version 1.3)
//		Modified by GraFX http://www.grafxsoftware.com

*/



class Rating{

var $rate;
var $output;
var $result;
var $go;
var $file;
var $file_real;

function Rating($go,$file,$file_real)

{

global $DEFAULT_URL,$DB_HOST,$DB_USR,$DB_PWD,$DB_NAME;
global $notyetrated,$rated,$from,$altrate,$recentrated;
global $excellent,$good,$fair,$poor,$useless,$formrate;
global $line1,$line2,$line3,$line4,$line5,$numberof;
global $hasread1,$hasread2;



$this->go=$go;
$this->file=$file;
$this->file_real=$file_real;

$db = mysql_connect($DB_HOST, $DB_USR, $DB_PWD)
or die ("Could not connect to database");
mysql_select_db($DB_NAME, $db);

$sql = "SELECT id FROM Rate ";
$sql .= "WHERE Item = '$file'";

$result =  mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);

// getting the id
$id=$row[0];



if (!mysql_num_rows($result)) {

    $sql = "INSERT INTO Rate (Item, Rating, Votes, Num_Votes,viewed) VALUES ('$file','','','',0)";
    $result =  mysql_query($sql) or die(mysql_error());
    $id=mysql_insert_id();

} // end if


  $ip_address="no use";



  $sql = "SELECT Rating, IP,Votes,viewed,Num_Votes FROM Rate ";
  $sql .= "WHERE Item = '$file' ";
  $result = mysql_query($sql);
  $row = mysql_fetch_array($result);



  $this->rate = "<br>";

  $isMarked=$this->isMarked($id);

  if ($row[0] < 1 && !$isMarked) {

    $this->rate  .= "$notyetrated";

  }

  else
    {
        $i = 0;

        While (++$i <= $row[0])
             $this->rate  .= "<img src=\"".$RATE_URL."graphic/rate.gif\" border=\"0\" alt=\"$altrate\"></a>\n";


    $numberofvotes=$row["Num_Votes"];
    $viewed=$row["viewed"];


    $this->rate  .= "<strong>$rated $row[0] $from 10</strong><br>\n";
    $this->rate  .= "<strong>".$hasread1.$viewed.$hasread2."</strong><br>\n";
	$this->rate  .= "<strong>$numberof $numberofvotes</strong><br>\n";

  } // end else


 if ($isMarked)
 $this->rate  .= "$recentrated";
 else
 {


	   if($this->go=="Rate" && !$isMarked )
		{
			$sql = "UPDATE Rate set viewed=viewed+1 ";
            $sql  .= "WHERE id =".$id;
           	$result =  mysql_query($sql) or die(mysql_error());

		}

		// Select box

		$options = array("$excellent", "$good", "$fair", "$poor", "$useless");

		$this->rate  .= "<form name=\"rate\" action=\"".substr($DEFAULT_URL, 0, strlen($DEFAULT_URL)-1)."/rate.php\" method=\"post\">\n";
        $this->rate  .= "<select name=\"go\">\n";

		for ($i = 0; $i < count($options); $i++)
		  $this->rate  .= "<option name = \"$options[$i]\">$options[$i]</option>\n";

 		$this->rate  .= "</select>\n";
    	$this->rate  .= "<input type=\"hidden\" name=\"file\" value=\"$this->file\">\n";
        $this->rate  .= "<input type=\"hidden\" name=\"file_real\" value=\"$this->file_real\">\n";
        $this->rate  .= "<input type=\"submit\" value=\"$formrate\">\n";
        $this->rate  .= "</form>\n";


 } // end else



if ($this->go<>"Rate") {

     switch ($this->go) {

      case "$excellent": $Rate = 10;
      break;

      case "$good": $Rate = 8;
      break;

      case "$fair": $Rate = 6;
      break;

      case "$poor": $Rate = 4;
      break;

      case "$useless": $Rate = 2;
      break;

    }



    $sql = "SELECT Item, Rating, Votes, Num_Votes,viewed FROM Rate ";
    $sql  .= "WHERE Item = '$file' ";

    $result =  mysql_query($sql);
    $row = mysql_fetch_array($result);

    $new = $row[3] + 1;
    $row[2] = $row[2] + $Rate;
    $row[1] = round(($row[2] / $new), 2);

    $numberofvotes=$new;
  	$viewed=$row["viewed"];


	// thanx to motogpd
	if ($Rate > 0)
	{
	$this->setIp($id);
	}


    // Update Rating

     if(!$isMarked)
        {
            $sql = "UPDATE Rate set Num_Votes='$new', Votes='$row[2]', Rating='$row[1]', IP='$ip_address' ";
            $sql  .= "WHERE Item = '$file' ";

        	$retid =  mysql_query($sql);
          	if (!$retid) { echo( mysql_error()); }

            //$this->setIp($id);
         }

    $br = "<br><br>";

	if ($result) {
         header("Location: $this->file_real"); 
		 exit;

	} // end if

  } // end if

  mysql_close($db);

  }// end Rating

function getRating()

{

	return $this->rate;

}//end setIdDomenii

function getResult()

{

	return $this->output;

}//end setIdDomenii


function setIp($id){
global $DEFAULT_URL,$DB_HOST,$DB_USR,$DB_PWD,$DB_NAME,$TIME_EXPIRE,$datamissing;


$remote_addrs = $_SERVER['REMOTE_ADDR'];

if(empty($_SERVER['REMOTE_HOST']))
  $remote_host=$remote_addrs;
else
  $remote_host=$_SERVER['REMOTE_HOST'];

if(empty($_SERVER['HTTP_USER_AGENT']))
$user_agent=$datamissing;
else
$user_agent=$_SERVER['HTTP_USER_AGENT'];


$db = mysql_connect($DB_HOST, $DB_USR, $DB_PWD)
or die ("Could not connect to database");
mysql_select_db($DB_NAME, $db);

$sql = "Update Logger set viewed=viewed+1 ";
$sql .= "WHERE regtime >= now() AND ";
$sql .= "id=$id AND ";
$sql .= "remote_addrs='$remote_addrs'";

$result =  mysql_query($sql) or die(mysql_error());

if(mysql_affected_rows()==0)
{
    $sql = "INSERT INTO Logger (id,remote_addrs,remote_host, user_agent,regtime,viewed)";
    $sql .= " VALUES ('$id','$remote_addrs','$remote_host','$user_agent',DATE_ADD(now(),INTERVAL $TIME_EXPIRE SECOND),0)";
    $result =  mysql_query($sql) or die(mysql_error());
    return true;

}
else
return false;


}


function isMarked($id){
global $DEFAULT_URL,$DB_HOST,$DB_USR,$DB_PWD,$DB_NAME,$TIME_EXPIRE;


$remote_addrs = $_SERVER['REMOTE_ADDR'];


$db = mysql_connect($DB_HOST, $DB_USR, $DB_PWD)
or die ("Could not connect to database");
mysql_select_db($DB_NAME, $db);


$sql = "Select count(id) from Logger ";
$sql .= "WHERE regtime >=now() AND ";
$sql .= "id=$id AND ";
$sql .= "remote_addrs='$remote_addrs'";

$result =  mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);

if($row[0]!=0)
{

$sql = "Update Logger set viewed=viewed+1 ";
$sql .= "WHERE regtime >= now() AND ";
$sql .= "id=$id AND ";
$sql .= "remote_addrs='$remote_addrs'";

$result =  mysql_query($sql) or die(mysql_error());

}

return $row[0];

}



}// end class

?>
Return current item: Star Rating System class