Rating System

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Rating System

Post by kkonline »

From a page pages.php?page=100 where 100 represents a unique number for articles. Similarly i am using for different sections like quotes, news... etc.

I am sending

Code: Select all

<?=NumRates(100);?> , <?ShowResult(100);?>,    <?ShowStars(100);?>
where 100 represents the article id . Then the below functions work properly.

But as I have different sections (article,news etc. and each has similar id) so i want to send article1,article2 from article page... news1,news2 from news page (rather than only the id i am using the name of the section to store/identify a particular data)

If i write

Code: Select all

<?ShowResult(article12);?>
It gives LINE 74:Unknown column 'article12' in 'where clause'

How o correct this

Code: Select all

function ShowForm($id,$num,$rate_me="Rate Me",$rate_it="Rate!",$class="")
{
   echo '<form action="'.$GLOBALS['PHP_SELF'].'" method="post">';
   echo '<select name="rates" class="'.$class.'">';
   echo '<option value="x" selected>'.$rate_me.'</option>';
   for($x=$num;$x>0;$x--)
    echo '<option value="'.$x.'">'.$x.'</option>';
   echo '</select>';
   echo '<input type="hidden" name="rateit_id" value="'.$id.'">';
   echo '<input type="hidden" name="action" value="doit"> ';
   echo '&nbsp;<input type="submit" value="'.$rate_it.'" class="'.$class.'">';
   echo '</form>';
}

function NumRates($id)
{
 $res = mysql_query("SELECT count(*) AS num FROM ratings WHERE article='".$id."'") or die("LINE 50:".mysql_error());
 $ar = mysql_fetch_array($res);
 if(empty($ar[num])) $ar[num] = "0";
 return $ar[num];
}

function ShowResult($id)
{
 /*
  The formula for calculating the top 250 films gives a true Bayesian estimate:
 
     weighted rank (WR) = (v ÷ (v+m)) × R + (m ÷ (v+m)) × C

  where:
     R = average for the movie (mean) = (Rating)
     v = number of votes for the movie = (votes)
     m = minimum votes required to be listed in the top 250 (currently 1250)
     C = the mean vote across the whole report (currently 6.9)
 */
 $query = "SELECT COUNT(*) as num, ((COUNT(*)/(COUNT(*)+".$GLOBALS['m']."))*AVG(rate)+(".$GLOBALS['m']."/(COUNT(*)+".$GLOBALS['m']."))*".$GLOBALS['C'].") AS rate FROM ".$GLOBALS['table']." WHERE rate>0 AND rate<11 AND article=".$id;
 $res = mysql_query($query) or die("<b>LINE 74</b>:".mysql_error());
 $ar  = mysql_fetch_array($res);
 $procent = $ar[rate];
 if(empty($procent) || $procent<0) $procent=0;
 
 echo $procent;
}

function ShowStars($id,$width=20)
{
 $query = "SELECT COUNT(*) as num, ((COUNT(*)/(COUNT(*)+".$GLOBALS['m']."))*AVG(rate)+(".$GLOBALS['m']."/(COUNT(*)+".$GLOBALS['m']."))*".$GLOBALS['C'].") AS rate FROM ".$GLOBALS['table']." WHERE rate>0 AND rate<11 AND article=".$id;
 $res = mysql_query($query) or die("<b>LINE 85</b>:".mysql_error());
 $ar  = mysql_fetch_array($res);
 $procent = (int) $ar[rate]+0.5;
 if(empty($procent) || $procent<0) $procent=0;
 
 for($x=1;$x<=$procent;$x++)
   echo '<img src="./gfx/star1.jpg" alt="" width="'.$width.'"/>';
 
 $io = $ar[rate] - (int)$ar[rate];
 if($io>0.49) echo '<img src="./gfx/star2.jpg" alt="" width="'.$width.'"/>';
}
The article field is defined as varchar(20) in db
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Take a look at the name of the column in the database table. It suggests that you have miss-typed the column name hence the error message.
Post Reply