Location: PHPKode > scripts > Ratings Script > kbratings/kbratings/index.php
<?php
include("../../../config.php");
include("rating_function.php");

$err="0";
$crr="0";
$err1="";
$err2="";

if(isset($_REQUEST['listid']))
{
	$listid=$_POST['listid'];
	if($listid=='')
	{
	$err="1";
	$err1="Item is blank<br>";
	}
}
else
{
$timestamp=time();
//To have a random Listing select for the demo.
//For your use you can PASS here the listing id/code value.

$qryratinglist="SELECT * FROM scrratinglist ORDER BY scrratinglist.listid";
$rslratinglist=mysql_query($qryratinglist);
$nratinglist = mysql_num_rows($rslratinglist);

$listid=$timestamp%$nratinglist+1;
for ($i=0; $i<$nratinglist; $i++)
{
$arrratinglist =  mysql_fetch_row($rslratinglist);
if($listid=="$arrratinglist[0]")
{
$listnam="$arrratinglist[1]";
}
}
}

$qryratinglistdtl="SELECT * FROM scrratinglist WHERE listid='$listid'";
$rslratinglistdtl=mysql_query($qryratinglistdtl);
$nratinglistdtl = mysql_num_rows($rslratinglistdtl);
$arrratinglistdtl =  mysql_fetch_row($rslratinglistdtl);

$ipaddress = $_SERVER['REMOTE_ADDR'];
$tmestmpnow = time();

$votagain="600"; //For voting again after some time. Here 600 Seconds. You should make it 1 day or 1 week.
$ratingvottmeout=$tmestmpnow-$votagain;

	$qryratingvotdel="DELETE FROM scrratingvot WHERE ratingvottme<'$ratingvottmeout'";
	$rslratingvotdel=mysql_query($qryratingvotdel);
	
	$qryratingvot="SELECT * FROM scrratingvot where
	ratingvotlistcod='$listid' AND ratingvotusrip='$ipaddress' AND ratingvottme>'$ratingvottmeout'
	";
	$rslratingvot=mysql_query($qryratingvot);
	$nratingvot = mysql_num_rows($rslratingvot);
	$arrratingvot =  mysql_fetch_row($rslratingvot);


if( $nratingvot!='0' || isset($_COOKIE['has_voted_'.$listid]) )
{
	$err="1";
	$err2="The Rating Vote has been submitted before<br>
	You can try to rate again in few days<br>";
}
else
{

if(isset($_REQUEST['userratingsub']))
{

$err3="";
$err4="";
$crr1="";


if(isset($_REQUEST['userrating']))
{
	$userrating=$_POST['userrating'];
	if($userrating=='')
	{
	$err="1";
	$err3="Please Select Rating<br>";
	}
}




if( isset($_REQUEST['userratingsub']) && $err=="0" )
{
// Cookie settings
$expire = time() + 99999999;
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false; // make cookies work with localhost
setcookie('has_voted_'.$listid,$listid,$expire,'/',$domain,false);

$newratingvotins = mysql_query ("INSERT INTO scrratingvot VALUES (
		'',		'$userrating',		'$listid',	'$ipaddress',
	
		'$tmestmpnow'
		)");

	

$olduserrating=$arrratinglistdtl[2];
$olduservotes=$arrratinglistdtl[3];
$olduserratingvalue=$olduserrating*$olduservotes;

$newuserratingvalue=$olduserratingvalue+$userrating;
$newuservotes=$olduservotes+1;
$newuserrating=$newuserratingvalue/$newuservotes;

    $qrynewuserratingupd="UPDATE scrratinglist SET listrte='$newuserrating',listrtevot='$newuservotes' WHERE listid='$listid'";
	$rslnewuserratingupd=mysql_query($qrynewuserratingupd);
	if($rslnewuserratingupd)
	{
	$crr="1";
	$crr1="Your Vote has been submitted<br>
	Thanx for Voting<br>";
	}
	else
	{
	$err="1";
	$err4="Your Vote was not submitted<br>";
	}

	
}

}
?>
<html>
<head>
  <title>KB Ratings PHP Script!</title>
</head>
<body>
<h1>Demo User Ratings</h1>
<p>The demo here shows the Ratings submitted by the user. A user can submit the rating for an
 article, listing etc. The ratings can only be given once by a user. If a user has already
  rated/voted then for a specific time he cannot do it again in that time period. After this time period he can do rating/voting again. Here time period is 10 minutes.
</p>
<?php
if( (isset($_REQUEST['userratingsub']) && $err!="0") || (!isset($_REQUEST['userratingsub'])) )
{
	if(isset($_REQUEST['userratingsub']) && $err!="0")
	{
	echo "<font color='red'>
	$err3 $err4
	</font><br>";
	}

?>

<table  border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%">
		<tr>
        <td width="60%" valign="top">
        <!--Table for the User Login form-->	
<form method='POST' action=''
onsubmit='return verifyuserrating(this.userrating)'>
<table border="0" cellpadding="0" cellspacing="0" width="100%" bordercolor="#336699">
<tr>
<td valign='top'>
<h2>User Ratings</h2>
</td>
</tr>

<tr>
<td valign='top'>
<?php
echo "Rating for: $arrratinglistdtl[1]";
?>

</td>
</tr>

<tr>
<td valign='top'>
<?php
$timestamp = time();
echo "
<select size='1' name='userrating'> 
<option value=''>Ratings</option>
";

$qryrating="SELECT * FROM scrrating WHERE ratingact='1'ORDER BY scrrating.ratingcod";
$rslrating=mysql_query($qryrating);
$nrating = mysql_num_rows($rslrating);
for ($i=0; $i<$nrating; $i++)
{
$arrrating =  mysql_fetch_row($rslrating);

	echo "<option value='$arrrating[4]'>$arrrating[4] $arrrating[3]</option>";

}

echo "
	</select> 
";
?>

</td>
</tr>

<tr>
<td valign='top'>
<?php
		echo "   
<input type='submit' value='Rate It!' name='userratingsub'>
<input type='hidden' value='$listid' name='listid'>	
			";
?>
</td>
</tr>

</table>
</form>

<?php
}
	elseif(isset($_REQUEST['userratingsub']) && $crr!="0")
	{
	echo "<font color='green'>
	$crr1
	</font><br>";
	}
}
	if($err!="0")
	{
	echo "<font color='red'>
	$err1 $err2
	</font><br>";
	}
?>        
     </td>
    </tr>
   </table>
   
<table border="0" cellpadding="0" cellspacing="0" width="100%" bordercolor="#336699">
<tr>
<td valign='top'>
<?php
scrlistrating($listid); //You can use this function to have rating on any page.
?>
</td>
</tr>
</table>
   
   <script>

function verifyuserrating(userrating)
{
	var passed=false
 	
       if (userrating.value=='')
 			{
 			alert("Please select User Rating")
 			userrating.focus()
 			} 	 				 				
else
  passed=true
return passed
}
  </script>
</body>
</html>
Return current item: Ratings Script