It rates but

Hi All,

Ive been working on a script that allows people to rate. I’ve managed to get it to do the maths and rate pictures. Problem is it applies the same rating for all the pictures, I need the script to apply different ratings for different pictures see the script below:

The Rate Script:

?php 
	
	include ("viewgallery.php");
	
?>
<?php

	// Connects to your Database
	mysql_connect("localhost", "#", "") or die(mysql_error());
	mysql_select_db("###") or die(mysql_error()); 
	
	// Get the radio button values 
		
	
	// Insert record 
		
	// What if the don't like yours
	if (!isset($_POST['idly'])) {
	$update_idy = mysql_query("UPDATE co_rate SET votes = votes +1, total = total +1 WHERE photo_category='".addslashes($pid)."'")
	or die (mysql_error());
	} else { 
	
		// Update the data
	$update = mysql_query("UPDATE co_rate SET dly = dly +1, total = total +1")
	or die (mysql_error());
	}

	// 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[total] * 100; 
										echo "Current Rating: " . round($current, 1) . "% <br>"; 

									  }

?>

and here is the view gallery script;

<?php
	include("config.inc.php");
	
	// initialization
	$result_array = array();
	$counter = 0;

	$cid = (int)($_GET['cid']);
	$pid = (int)($_GET['pid']);
	

	// Category Listing

	if( empty($cid) && empty($pid) )
	{
		$number_of_categories_in_row = 4;

		$result = mysql_query( "SELECT c.category_id,c.category_name,COUNT(photo_id)
						FROM gallery_category as c
						LEFT JOIN gallery_photos as p ON p.photo_category = c.category_id
						GROUP BY c.category_id" );
		while( $row = mysql_fetch_array( $result ) )
		{
			$result_array[] = "<a href='viewgallery.php?cid=".$row[0]."'>".$row[1]."</a> "."(".$row[2].")";
		}
		mysql_free_result( $result );	

		$result_final = "<tr>\
";

		foreach($result_array as $category_link)
		{
			if($counter == $number_of_categories_in_row)
			{	
				$counter = 1;
				$result_final .= "\
</tr>\
<tr>\
";
			}
			else
			$counter++;

			$result_final .= "\	<td>".$category_link."</td>\
";
		}

		if($counter)
		{
			if($number_of_categories_in_row-$counter)
			$result_final .= "\	<td colspan='".($number_of_categories_in_row-$counter)."'>&nbsp;</td>\
";

			$result_final .= "</tr>";
		}
	}


	// Thumbnail Listing

	else if( $cid && empty( $pid ) )
	{
		$number_of_thumbs_in_row = 5;

		$result = mysql_query( "SELECT photo_id,photo_caption,photo_filename FROM gallery_photos WHERE photo_category='".addslashes($cid)."'" );
		$nr = mysql_num_rows( $result );

		if( empty( $nr ) )
		{
			$result_final = "\	<tr><td>No Category found</td></tr>\
";
		}
		else
		{
			while( $row = mysql_fetch_array( $result ) )
			{
				$result_array[] = "<a href='viewgallery.php?cid=$cid&pid=".$row[0]."'><img src='".$images_dir."/tb_".$row[2]."' border='0' alt='".$row[1]."' /></a>";
			}
			mysql_free_result( $result );	

			$result_final = "<tr>\
";
	
			foreach($result_array as $thumbnail_link)
			{
				if($counter == $number_of_thumbs_in_row)
				{	
					$counter = 1;
					$result_final .= "\
</tr>\
<tr>\
";
				}
				else
				$counter++;

				$result_final .= "\	<td>".$thumbnail_link."</td>\
";
			}
	
			if($counter)
			{
				if($number_of_photos_in_row-$counter)
			$result_final .= "\	<td colspan='".($number_of_photos_in_row-$counter)."'>&nbsp;</td>\
";

				$result_final .= "</tr>";
			}
		}
	}

	// Full Size View of Photo
	else if( $pid )
	{
		$result = mysql_query( "SELECT photo_caption,photo_filename FROM gallery_photos WHERE photo_id='".addslashes($pid)."'" );
		list($photo_caption, $photo_filename) = mysql_fetch_array( $result );
		$nr = mysql_num_rows( $result );
		mysql_free_result( $result );	

		if( empty( $nr ) )
		{
			$result_final = "\	<tr><td>No Photo found</td></tr>\
";
		}
		else
		{
			$result = mysql_query( "SELECT category_name FROM gallery_category WHERE category_id='".addslashes($cid)."'" );
			list($category_name) = mysql_fetch_array( $result );
			mysql_free_result( $result );	

			$result_final .= "<tr>\
\	<td>
						<a href='viewgallery.php'>Categories</a> > 
						<a href='viewgallery.php?cid=$cid'>$category_name</a></td>\
</tr>\
";

			$result_final .= "<tr>\
\	<td align='center'>
					<br />
					<img src='".$images_dir."/".$photo_filename."' border='0' alt='".$photo_caption."' />
					<br />
					$photo_caption
					$photo_rating
					<br /><br />
					<form action=report.php method=post/>
					<input type=submit name=report value=Report />
					</form>
					<form name=rate action=rate.php method=post>
<b>Pick Only one Please!</b>
<br />
<INPUT TYPE=checkbox NAME=vote
onClick=return KeepCount()> I Do Like Yours
<br />
<INPUT TYPE=checkbox NAME=idly
onClick=return KeepCount()> I Don't Like Yours
<br />
<input type=submit name=rateit value=Rate It />

</FORM>
					
					
					</td>
					</tr>";
		}
	}


// Final Output
echo <<<__HTML_END

<html>
<head>
	<title>Gallery View</title>
</head>
<body>
<table width='100%' border='0' align='center' style='width: 100%;'>
$result_final

</table>
</body>
</html>

__HTML_END;
?>

Any help on this would be great. This is the last bit I need to get it all working :smiley:

In your co_rate table, simply add another field, photo_id, which corresponds with a photo. Then modify your voting form to include the photo_id upon submission, and change your voting queries to update the row where photo_id matches whichever photo is being rated.

I’ve added the new photo_id to the table, with regards to the form would this be a hidden field?

Yes

Hi There

This is what I’ve got so far, but it doesn’t work any suggestions?

<?php
	$cid = (int)($_GET['cid']);
	$pid = (int)($_GET['pid']);
	
	// Connects to your Database
	mysql_connect("localhost", "root", "") or die(mysql_error());
	mysql_select_db("####") or die(mysql_error()); 
	
	// Get the radio button values 
		
	
	// Insert record 
		
	// What if the don't like yours
	if (isset($_POST['vote'])) {
	$sql = mysql_query("UPDATE co_rate SET co_rate.votes = co_rate.votes +1, co_rate.total = co_rate.total +1, co_rate.photo_id = $pid WHERE photo_id= $pid");
		mysql_free_result( $sql );	
		
	} elseif (isset($_POST['idly'])) { 
	
		// Update the data
	$sqla = mysql_query("UPDATE co_rate SET dly = dly +1, total = total +1, photo_id = $pid WHERE photo_id= $pid");
		mysql_free_result( $sqla );	
	}

	// 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[total] * 100; 
										echo "Current Rating: " . round($current, 1) . "% <br>"; 

									  }

?>