Rating System

Hi All

I am attempting to build a rating script with a slight twist. Rather than having pictures rated 1-10 pictures will be rated via a % of the number of times it was rated.

I’ve created the form to do it, however, I’m unsure how to proceed with the PHP. Shown below is the code for the form:

Any help would be great. :o)

<form action=rate.php method=post />
    <input type=radio name=rate value=1 id=RadioGroup1_0 />
    I Like Yours</label>
  <br />
    <input type=radio name=rate value=0 id=RadioGroup1_1 />
    I Don't Like Yours</label><br />
	<input type=submit name=submit value=Rate />
  <br />

make rate.php

<?php print_r($_POST); ?>

Thanks for the message it needs to do slightly more than that?

It needs to add all of the rates up and come out with a decimal which will be multiplied by 100 to get a percentage.

You will need to create a database to store the data, restrict how many times an individual may vote (i.p. restricted? per user?), perform math on the data as you see fit, then output the data as you like

Ok, Would it not be possible to do this script without the use of a database?

Possible? I guess you could use a CSV file instead of a database. But really, you need a database

Ok, thank you, therefore I’m assuming I’m going to need use While loops and some form of array to make it fully work. I can get it to do the simple part i.e. work out the percentage but the rest is a blur.

I’ve assigned the value of 1 to one radio button and 0 to the other when a user votes for 1 it needs to be added, would this be a for loop?

Sorry to ask so many questions I know some of the basics but not all.

You’ve confused me. Insert each vote into the database. Then mysql_fetch_array the votes as needed

Here’s a script I’ve come up with for it, however it doesn’t seem to update the votes to include a new vote from the form. It works out the rating fine, just not add new votes to the database.

I’m using radio buttons any help?


	// Connects to your Database
	mysql_connect("localhost", "root", "") or die(mysql_error());
	mysql_select_db("cystem_ilikeyours") or die(mysql_error()); 
	// Get the radio button values 
	$vote = $_POST['vote'];
		// Convert the values to the actual name, if you like
	switch($vote) {
	case '1':
	 $vote = "+1";
	case '0':
 	$vote = "0";
	// Update the data
	$updat_array = mysql_query("UPDATE 'co_rate' WHERE rate_id = '1'");
	// Put it into an array 
	$array = mysql_query("SELECT * FROM co_rate ") 
	or die (mysql_error());
	// Loop Through the data 
	while ($rate = mysql_fetch_array($array)) {
									  //This calculates the sites ranking and then outputs it - rounded to 1 decimal
										$current = $rate[votes] + $rate[votes] / $rate[total]; 
									  echo "Current Rating: " . round($current, 2) . "<br>"; 

// One vote per user

// Add the total number votes up 

// Work out the percentage

// Display the result as a percentage 


You are storing the vote total for one object in the ‘rate_id’ row of your table?

If so, before you update you will have to first retrieve the current total… something like

SELECT vote_total FROM co_rate WHERE rate_id = ‘1’

$vote_total = $vote_total+$new_vote

UPDATE co_rate SET vote_total=$vote_total WHERE rate_id = ‘1’