Forum Moderators: coopster

Message Too Old, No Replies

Rating Script

         

PartisanEntity

8:44 pm on Feb 8, 2010 (gmt 0)

10+ Year Member



Hi,

Just to give you an idea of what I am trying to achieve:

The script should check to see if the current user has voted for an image, if they have, then it should show them the current rating for the image. This works.

If they have not voted for an image, it should display the vote box. This is not working.

The problem is that my part of the script that should display the vote box is not being carried out for all images, but only as many times as the number of results returned by the script. I have attached a screenshot.

i.e. if only two images have been voted for, then the vote box is displayed for only two images that have not been voted for. So the number of times the vote box is displayed, is based on how many images have been voted for, but I want it to display for any image that has not been voted for.

I hope I have managed to explain it somewhat.

//include our db data to make the connection
include ('db.php');

$content = mysql_query("SELECT * FROM cars ORDER BY id DESC");

while($row = mysql_fetch_assoc($content))
{
$mainImage = $row['mainImage'];
$oid = $row['oid'];
$thumbImage = $row['thumbImage'];
$car_username = $row['username'];
$username = $_SESSION['username'];
$date = $row['date'];

echo '<div class="gallimg"';
echo '<a href="'.$mainImage.'" name="'.$oid.'" rel="lightbox[cars]"><img src="'.$thumbImage.'" title="'.$car_username.'s car"/></a><br />';
echo 'by <span class="fat">'.$car_username.'</span> on '.$date.'<br />';

//get the votes for each image
$content2 = mysql_query("SELECT * FROM votes WHERE oid = '$oid' AND username = '$username'");

while($row2 = mysql_fetch_array($content2))
{
$voter = $row2['username'];
$vote = $row2['vote'];
}

//if the user has voted for an image
if (strlen($voter) > 1) {
//show the average vote
$content3 = mysql_query("SELECT SUM(vote) FROM votes WHERE oid = '$oid'");

while($row3 = mysql_fetch_array($content3))
{
$totalVotes = $row3['SUM(vote)'];
}

$content4 = mysql_query("SELECT * FROM votes WHERE oid = '$oid'");

$numbVoters = mysql_affected_rows();

if (strlen($totalVotes) > 0 && strlen($numbVoters) > 0) {
echo 'Rating: <span class="fat">' . $totalVotes / $numbVoters . '</span> Votes: ' . $numbVoters;
}


} else {
//show the vote box, and for testing purposes display the words: no vote
echo 'no vote';

}

echo "</div>";
}

Matthew1980

9:04 pm on Feb 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there partisanentity,

Just scanning through the code I see a couple of things, not causing a problem, but I thought I would just point them out:-

echo '<div class="gallimg">';


Your missing the closing > on the opening div, and also, there is no need to assign the $_SESSION to a var, all you need to do echo that directly into the sql query, but make sure that it is sanitised first just in case there has been an injection, its doubtful, but worth doing.

Other than that, the only other thing I'm not certain about is the:-


if (strlen($totalVotes) > 0 && strlen($numbVoters) > 0)


if your looking for an int there you dont need to see how many chars there is in it..


if ($totalVotes > 0 && $numbVoters > 0)


Other than that, (without reading it indepth) I'm not sure.. Though I shall ponder it a little longer...

Cheers,

MRb