I am trying to introduce a rating system on my site where I can include a rating on any page of my site by including a few files. This is mainly for rating the tutorials so far, but I want it to be available for anything else that may need a rating.
I followed a tutorial to get the basic idea on how to do this, and changed it slightly to match my needs. As I am very confused when it comes to working with SQL databases, I could not find a better way to determine which row of a table to select for the ratings, other than by manually adding a value to "name_id" and then requesting this. I am not sure if this will even work though, because so far I have not been able to get my script to add/update any rows in the "ratings" table of my database.
To see the page so far, go to http://www.retina-fx.com/retina.php?page=testtut. To get to the voting part, add "&vote=1" to the end of the url.
I have probably just made a stupid mistake somewhere that can be fixed easily. I will show all my scripts though as I do not quite know where the error is happening, so I apologise in advance.....
Here is the "testtut.php" page where I am just testing out this script. This is the page everything is called from at the moment:
<? $name_id = 1; //CHANGE THIS NUMBER FOR EACH PAGE!! ?>
<table border=0 cellpadding=0 cellspacing=0 height=100% width=100%>
<tr>
<td width=5 height=*>
<td width=*>
<table border=0 cellpadding=0 cellspacing=0 height=100% width=100%>
<tr>
<td height=5 width=*>
</tr>
<tr>
<td valign=top>
<!--Start of content here-->
<div class=text<? echo $skin; ?> align=left>
<h2>Test Tut!</h2><font size=3>
<? include 'stars.php'; ?>
<?
if ($vote)
{
include 'rateform.php';
} else {
?>
This does not teach you anything, but I am just testing the new voting system.
<?
}
?>
</font></div>
<!--End of content here-->
</tr>
</table>
</tr>
</table>
Here is the first page that is included, "stars.php". This determines what image gets shown depending on what the rating is:
<?
if((($new_rating2 >= 0)or($new_rating2 == 0)) && ($new_rating2 <= 0.50)){
echo '<img src="stars/0o5.gif" width="70" height="18">';
}
if((($new_rating2 >= 0.50)or($new_rating2 == 0.50)) && ($new_rating2 <= .99)){
echo '<img src="stars/05o5.gif" width="70" height="18">';
}
if((($new_rating2 >= 1.00)or($new_rating2 == 1.50)) && ($new_rating2 <= 1.49)){
echo '<img src="stars/1o5.gif" width="70" height="18">';
}
if((($new_rating2 >= 1.50)or($new_rating2 == 1.50)) && ($new_rating2 <= 1.99)){
echo '<img src="stars/15o5.gif" width="70" height="18">';
}
if((($new_rating2 >= 2.00)or($new_rating2 == 2.00)) && ($new_rating2 <= 2.49)){
echo '<img src="stars/2o5.gif" width="70" height="18">';
}
if((($new_rating2 >= 2.50)or($new_rating2 == 2.50)) && ($new_rating2 <= 2.99)){
echo '<img src="stars/25o5.gif" width="70" height="18">';
}
if((($new_rating2 >= 3.00)or($new_rating2 == 3.00)) && ($new_rating2 <= 3.49)){
echo '<img src="stars/3o5.gif" width="70" height="18">';
}
if((($new_rating2 >= 3.50)or($new_rating2 == 3.50)) && ($new_rating2 <= 3.99)){
echo '<img src="stars/35o5.gif" width="70" height="18">';
}
if((($new_rating2 >= 4.00)or($new_rating2 == 4.00)) && ($new_rating2 <= 4.49)){
echo '<img src="stars/4o5.gif" width="70" height="18">';
}
if((($new_rating2 >= 4.50)or($new_rating2 == 4.50)) && ($new_rating2 <= 4.99)){
echo '<img src="stars/45o5.gif" width="70" height="18">';
}
if($new_rating2 == 5.0){
echo '<img src="stars/5o5.gif" width="70" height="18">';
}
?>
Here is "rateform.php", the page containing the form to fill out in order to submit your rating:
?>
<form name="Rate_Form" method="post" action="ratings.php?page=<? echo $page; ?>&name_id=<? echo $name_id; ?>">
<select size="1" name="rating">
<option selected value="5">5- Excellent</option>
<option value="4">4 - Good</option>
<option value="3">3- Fair</option>
<option value="2">2 - So So</option>
<option value="1">1 - Poor</option>
<option value="0">0- Awful</option>
</select>
<input type="hidden" name="song_id" value="$song_id">
<input type="submit" value="Go!">
</form>
<?
<form name="Rate_Form" method="post" action="ratings.php?page=<? echo $page; ?>&name_id=<? echo $name_id; ?>">
<select size="1" name="rating">
<option selected value="5">5- Excellent</option>
<option value="4">4 - Good</option>
<option value="3">3- Fair</option>
<option value="2">2 - So So</option>
<option value="1">1 - Poor</option>
<option value="0">0- Awful</option>
</select>
<input type="hidden" name="song_id" value="$song_id">
<input type="submit" value="Go!">
</form>
And finally, this is the page that the form submits to. It is meant to update the table containing the rating and then link back to the original page. This is most likely where the problem is:
<?
//connection info goes here, but you can't see that!
$connection = mysql_connect($dbhost, $dbusername, $dbpass);
$SelectedDB = mysql_select_db($dbname);
$query = mysql_query ("SELECT rating, num_votes FROM ratings WHERE name_id=$name_id") or die (mysql_error());
while(list($rating, $num_votes)= mysql_fetch_array($query))
{
$new_count = ($num_votes + 1);
$rating2 = ($rating * $num_votes);
$new_rating = (($rating + $rating2) / ($new_count));
$new_rating2 = number_format($new_rating, 2, '.', '');
$update_rating = mysql_query("UPDATE songratings SET
rating='$new_rating2',num_votes='$new_count' WHERE name_id=$name_id");
echo '<div align="center"><b>
<p>Thanks for your vote!</p>
<p>The new rating for this song is:
$new_rating2 out of 5</p>';
}
?>
I would really appreciate it if someone could fix this for me. Could you also suggest ways of improving this rating system so that I do not have to manually specify "name_id" in the database and also in each page - there must be a better way to get this working.