Okay I’m making a rating system which lets users rate pictures, and then it takes and average for that picture (read:hotornot). I then want to take averages for ALL pictures that a user has in a catagory (ie, music, clothing, self), and then make a complete average of those averages. I am not entirely sure of how to do this, but I attempted it this way:
<?php
//this function is for determining the ratings
function ratings($area,$num) {
//$area is passed from the function call
$fetchPicture = mysql_query("SELECT * FROM pics WHERE userid = " . $_GET['userid'] . " AND pending = 'no' AND area = '" . $area . "'");
while($sortPicture = mysql_fetch_array($fetchPicture)) {
$fetchRatings = mysql_query("SELECT * FROM ratings WHERE picId = " . $sortPicture['picid']);
$fetchRateAmnt = mysql_query("SELECT count(*) FROM ratings WHERE picId = " . $sortPicture['picid']);
$rateAmnt = mysql_result($fetchRateAmnt,0,0);
while($sortRatings = mysql_fetch_array($fetchRatings)) {
$picRating[] = $sortRatings['rating'];
}
if($rateAmnt > 0) {
for($i=0;$i<=$rateAmnt;$i++) {
$sumRating = $sumRating+$picRating[$i];
}
$finalRating[] = round($sumRating/$rateAmnt,1);
} else {
$finalRating[] = 'too few...';
}
echo '<p class="title">' . $area . '</p>';
/*echos the picture out; this works fine and can show the INDIVIDUAL
picture rating, but how can I get an average of all ratings for all
pictures in a catagory? Also, $num is passed through runction call,
and shouldn't $finalRating be an array I can do this with, cause
it\'s coming up blank*/
echo '<p><img src="pics/profiles/' . $sortPicture['userid'] . '/' . $sortPicture['FILE'] . '" width="100" /><br />' .
$finalRating[$num] . '</p>';
}
}
//calls function for each area
echo '<p class="title">Ratings</p>';
ratings('clothes',0);
ratings('music',1);
ratings('hair',2);
ratings('art',3);
ratings('self',4);
?>
alright, hope someone can help me out! And if you have ANY suggestions to make this better, I need em cause so far this seems a mess!
OH and here is my tables for ratings and pics
CREATE TABLE `pics` (
`picid` int(14) NOT NULL auto_increment,
`date` timestamp(14) NOT NULL,
`area` enum('clothes','music','hair','art','self')
NOT NULL default 'self',
`userid` int(14) NOT NULL default '0',
`FILE` varchar(255) NOT NULL default '',
`description` varchar(255) NOT NULL default '',
`current` enum('yes','no') NOT NULL default 'no',
`pending` enum('yes','no') NOT NULL default 'yes',
PRIMARY KEY (`picid`)
) TYPE=MyISAM;
CREATE TABLE `ratings` (
`ratingId` int(14) NOT NULL auto_increment,
`date` timestamp(14) NOT NULL,
`picId` int(14) NOT NULL default '0',
`userId` int(14) NOT NULL default '0',
`leaveId` int(14) NOT NULL default '0',
`rating` enum('0','1','2','3','4','5','6','7','8','9','10')
NOT NULL default '0',
PRIMARY KEY (`ratingId`)
) TYPE=MyISAM;
thanks!