|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Ratingsystem in PHP
Hello..
Someone know how to make/where to get a rating system like the one that is being used here at devArticles? So we can rate on the article.. Love Frank |
|
#2
|
|||
|
|||
|
i think mytch will be writting an article on this soon
![]()
__________________
Apache Expert |
|
#3
|
|||
|
|||
|
Hey,
Annette just wrote one yesterday: http://www.devarticles.com/art/1/141 It's in ASP but it isn't too hard to translate into PHP because she explains everything.
__________________
i am cope. i drink coke. i am in hope.i am cope. i drink coke. i am in hope.i am cope. i drink coke. i am in hope. |
|
#4
|
|||
|
|||
|
Thanx
Okey..
I have read it, but as a beginner I am - I don't understand so much. But I can try of course.. Tell me if anyone can come up with something smart then.. ![]() Frank |
|
#5
|
|||
|
|||
|
Re: Rating script
There is a simple rating script tutorial on my site.
http://www.developers-resources.com...2/06/26/2127157 That you can use that may help. Retr0 |
|
#6
|
|||
|
|||
|
Hey Retr0,
I clicked on the link you posted, but I got an error saying that the "story could not be found." Could you post the correct link? |
|
#7
|
|||
|
|||
|
Sorry!
Hello Web Guy
I pulled it as it as the code that the author wrote had bugs. I have done something else for my own sites rating system. I would post my own sites code but it may not be as helpful to you. The code below should give you a good idea as to how it's done. Database Schema / SQL Dump Here's the SQL dump which you can use to create the required tables. Code:
CREATE TABLE rates ( ID int(5) DEFAULT '0' NOT NULL auto_increment, FileName varchar(50) NOT NULL, Num_Votes int(4) NOT NULL, Votes int(5) NOT NULL, Rating int(5) NOT NULL, PRIMARY KEY (ID) ); Our table name is rates. FileName is the name of the file or article we want to rate. Num_Votes is the total number of votes for a article. Votes is the total votes calculated so far and finally Rating is the average (Num_Votes / Votes). The script is based on the mathematical formula of Rating = Num_Votes / Votes. Rating Page This is where we will place the link to enable users to rate an article or a file. Code:
<html>
<head></head>
<body>
<?php
// Define Database Conection Variables
$q="SELECT * from rates";
$result= mysql_db_query($db_name, $q, $connection) or die
("Could not execute query : $q." . mysql_error());
while ($row=mysql_fetch_array($result))
{
$id=$row["id"];
$FileName=$row["FileName"];
}
?>
Rating <?php echo "$FileName"; ?>: <form action="<?php echo "rate.php?id=$id"; ?>" method="post">
<select name="Rate">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<input type="submit" value="Rate">
</form>
</body>
</html>
The only thing here that you need to take note of is rate.php?id=$id which indicates that we are going to pass a id variable into rate.php. $q="SELECT * from rates"; We use a standard select query, to output all the records in the table. $result= mysql_db_query($db_name, $q, $connection) or die ("Could not execute query : $q." . mysql_error()); MySQL will output an error telling you what went wrong, if the query can't be executed, otherwise, it will execute the query. while ($row=mysql_fetch_array($result)) { $id=$row["id"]; MySQL will fetch the records row by row from the table and the record is stored in an array. We want the elements in the array and hence we have to 'explode' them out. $id=$row["id"]; will extract the id element out from the array for us. Rating Script (rate.php) Next we need to make a new file, named rate.php. Code:
<?php
// SQL database Variables
$q="SELECT * from rates where id='$id'";
$result= mysql_db_query($db_name, $q, $connection) or die
("Could not execute query : $q." . mysql_error());
while ($row=mysql_fetch_array($result))
{
$id=$row["id"];
$Filename=$row["Filename"];
$Num_Votes=$row["Num_Votes"];
$Votes =$row["Votes"];
$Rating=$row["Rating"];
$new_Votes=$Num_Votes+1;
$Votes=$Votes+$Rate;
$Rating=round(($Votes/$new_Votes),2);
$q="update rates set Num_Votes='$new_Votes', Votes='$Votes', Rating='$Rating' where id='$id'";
$result= mysql_db_query($db_name, $q, $connection) or die
("Could not execute query : $q." . mysql_error());
if ($result) {
echo "Thank you. The article has rating = $Rating after your vote.";
}
}
?>
Here's the explanation for the syntax above. $new_Votes=$Num_Votes+1; We let new_Votes be equal to the current number of votes in the record +1. If the Num_Votes value is 5, we want $new_Votes to be 6, (5+1). $Votes=$Votes+$Rate; $Rate has to be exactly what you used in the html page. If a user give a vote value 5, and the current vote value in the record is 25, our $Vote will be (25+5) = 30. $Rating=round(($Votes/$new_Votes),2); We compute the formula for average rating, then round it up to 2 integers. round is a standard PHP function.. $q="update rates set Num_Votes='$new_Votes', Votes='$Votes', Rating='$Rating' where id='$id'"; We then update the record with our new values.[/code] Code:
Happy hacking Ret0 Last edited by retr0 : July 1st, 2002 at 11:01 PM. |
|
#8
|
|||
|
|||
|
Wow! Thanks Retr0!
Quote:
Thanks, -Corbb Last edited by WebGuy : July 8th, 2002 at 05:13 PM. |
|
#9
|
|||
|
|||
|
You said it!
If you want to round to the nearest tenth, just use round(value, 1); Another function is ceil() which will round to the nearest integer value. ie: 4.56699999 will round to 5, rather than 4.6.
__________________
____________________________________________ Developer Shed Weekly Writer | DevArticles Forum Moderator Build Your Own KlipFolio Klip With PHP FrankManno.com - Under Construction Design Interactive Group - Under Construction |
|
#10
|
|||
|
|||
|
this is a good newbie friendly tutorial
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Ratingsystem in PHP |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|