PDA

View Full Version : [php]array not working? (making a rating system...)



radioxromance
April 8th, 2004, 06:40 AM
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!

radioxromance
April 9th, 2004, 02:11 PM
can anyone help me? Or have any info about making a voting/rating system? Any help is greatly appreciated!

radioxromance
April 9th, 2004, 07:39 PM
gahhh nobody here has made a PHP rating system and can help me? Man, you all act like you deal with some stupid, obscure languagte like AS in FLASH. :P
Seriously, though... need help, thanks :)

radioxromance
April 9th, 2004, 08:30 PM
ooook I suppose I fixed it... sort of... I think I was passing $num wrong... ended up just setting $num=0 and autoincrementing it at the end of the rate while loop, and then it does the function fine... I think. I jsut tried it with two pictures... it's having a problem putting them in one area, but I'll fix that. I have some more problems, if anyone is reading this, and I can't solve em, I'll be a postin'