Help Again Please

Hi All,

Been working on this script for the past 2 days and it’s just not working. I’m trying to get it to update records in a database by one for each vote that is cast. I get it voting for the entire website rather than for individual images.

There are no errors or anything like that, it just doesn’t do what I’m trying to get it do which is to provide individual percentage ratings for each individual image ($pid).

What I need is code examples on how to get this script to do that using what I’ve provided. Please use simple terms as I am finding this quite difficult.

<?php 
	
	include ("viewgallery.php");
	
?>
<?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'])) {
	 for ($pid = 0; $pid <= 1000;  $pid++) {
	$sql = mysql_query("UPDATE co_rate SET co_rate.votes = co_rate.votes[$pid] +1, co_rate.total = co_rate.total[$pid] +1, WHERE photo_id= $pid");
		}
		
	} elseif (isset($_POST['idly'])) { 
	 for ($pid = 0; $pid <= 1000;  $pid++) {
		// Update the data
	$sqla = mysql_query("UPDATE co_rate SET dly = dly[$pid] +1, total = total[$pid] +1, WHERE photo_id= $pid");
		}
	}

	// 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>"; 

									  }

?>

View gallery.php


<?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=hidden name=photo_id />
<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;
?>

To increase a value by 1, you do something like this:

UPDATE table SET column_name = column_name + 1 WHERE ...

Get rid of all those weird [$pid] after the column names and your code might work

That does work but gives me the me the percentage for the entire site when I want the percentage for the individual pages/elements in the database.

You need to move one step at a time:

  1. Get the voting part right

  2. Once the data in the database is correct, calculate whatever percentage you’re trying to calculate correctly

The percentage works out fine and the information in the database is correct, its just the first one I’m struggling with, I needed coded examples to make the above script work or someone to point out to me what is needed to be placed where.

What’s the first one? What bit of code isn’t doing what you want?

Getting the voting part right :smiley:

I think the part of the code that isn’t working is shown below:

// What if the don't like yours

    if (isset($_POST['vote'])) {

     for ($pid = 0; $pid <= 1000;  $pid++) {

    $sql = mysql_query("UPDATE co_rate SET co_rate.votes = co_rate.votes[$pid] +1, co_rate.total = co_rate.total[$pid] +1, WHERE photo_id= $pid");

        }

        

    } elseif (isset($_POST['idly'])) { 

     for ($pid = 0; $pid <= 1000;  $pid++) {

        // Update the data

    $sqla = mysql_query("UPDATE co_rate SET dly = dly[$pid] +1, total = total[$pid] +1, WHERE photo_id= $pid");

        }

    }

What I’m trying to do is get it to implement the values in the database by one for individual photo_id’s. At the moment it’s rating all the pictures at the same time where it should be picture 1 has a rating of 100% picture 2 a rating of 85% so on and so forth simply put I need it to give individual ratings for each image not ratings as a whole.

Get rid of the [$pid] stuff in the query, it simply doesn’t belong.

Then leave $pid equal to the picture you want to update. Get rid of that loop from 0 to 1000 around each query.

Each picture is given a separate ID for example picture 1 has an ID of 1 picture 3 ID3 so on and so forth, so how would I create the $pid to equal all the pictures in the table?

If you want to increment the value in every row, then you have one UPDATE query with no WHERE clause. It will affect all rows in the table, and you can increment their values by 1, not set all their values to the same thing.

If you want to increment the value for a row corresponding to a specific “pid”, then you have one UPDATE query with a WHERE clause specifying that “pid”.

I don’t understand which is your goal, but either way you don’t need this 1000 query loop and you need to remove the [$pid] stuff from the query.

If you want something more specific, then give your database schema, explain your database schema, and explain exactly what you want to do with it.

Ok, I have removed that stuff from my code, and it updates all of the results in the table, (not what I want it to do). In the database I have two tables one called, gallery_photo and one called co_rate. Both of these have an photo_id row in them.

In the script below, I believe the $pid is set to equal the photo_id and hence show individual images.

// 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>";
		}
	}