Jump to content

5 Star Rating System..


eugene2009

Recommended Posts

Hello, so im following this tutorial..

 

My tables are created and everything works great!!

 

But my problem is.. i want this rating script to work in my gallery for every individual photo..

every picture is /picture.php?id=23  , or id=2, etc.. anyways..

 

Ive been trying to do a lot of different things where it will save to the database under different id's so every page has its own rating count.. somebody please help me. here are the 2 pages that need to be edited.. thanks in advance..

 

//_drawrating.php

<?php

function rating_bar($id,$units='',$static='') { 

require('_config-rating.php'); // get the db connection info

//set some variables
$ip = $_SERVER['REMOTE_ADDR'];
if (!$units) {$units = 10;}
if (!$static) {$static = FALSE;}

// get votes, values, ips for the current rating bar
$query=mysql_query("SELECT total_votes, total_value, used_ips FROM $rating_dbname.$rating_tableName WHERE id='$id' ")or die(" Error: ".mysql_error());


// insert the id in the DB if it doesn't exist already
// see: http://www.masugadesign.com/the-lab/scripts/unobtrusive-ajax-star-rating-bar/#comment-121
if (mysql_num_rows($query) == 0) {
$sql = "INSERT INTO $rating_dbname.$rating_tableName (`id`,`total_votes`, `total_value`, `used_ips`) VALUES ('$id', '0', '0', '')";
$result = mysql_query($sql);
}

$numbers=mysql_fetch_assoc($query);


if ($numbers['total_votes'] < 1) {
$count = 0;
} else {
$count=$numbers['total_votes']; //how many votes total
}
$current_rating=$numbers['total_value']; //total number of rating added together and stored
$tense=($count==1) ? "vote" : "votes"; //plural form votes/vote

// determine whether the user has voted, so we know how to draw the ul/li
$voted=mysql_num_rows(mysql_query("SELECT used_ips FROM $rating_dbname.$rating_tableName WHERE used_ips LIKE '%".$ip."%' AND id='".$id."' ")); 

// now draw the rating bar
$rating_width = @number_format($current_rating/$count,2)*$rating_unitwidth;
$rating1 = @number_format($current_rating/$count,1);
$rating2 = @number_format($current_rating/$count,2);


if ($static == 'static') {

	$static_rater = array();
	$static_rater[] .= "\n".'<div class="ratingblock">';
	$static_rater[] .= '<div id="unit_long'.$id.'">';
	$static_rater[] .= '<ul id="unit_ul'.$id.'" class="unit-rating" style="width:'.$rating_unitwidth*$units.'px;">';
	$static_rater[] .= '<li class="current-rating" style="width:'.$rating_width.'px;">Currently '.$rating2.'/'.$units.'</li>';
	$static_rater[] .= '</ul>';
	$static_rater[] .= '<p class="static">'.$id.'. Rating: <strong> '.$rating1.'</strong>/'.$units.' ('.$count.' '.$tense.' cast) <em>This is \'static\'.</em></p>';
	$static_rater[] .= '</div>';
	$static_rater[] .= '</div>'."\n\n";

	return join("\n", $static_rater);


} else {

      $rater ='';
      $rater.='<div class="ratingblock">';

      $rater.='<div id="unit_long'.$id.'">';
      $rater.='  <ul id="unit_ul'.$id.'" class="unit-rating" style="width:'.$rating_unitwidth*$units.'px;">';
      $rater.='     <li class="current-rating" style="width:'.$rating_width.'px;">Currently '.$rating2.'/'.$units.'</li>';

      for ($ncount = 1; $ncount <= $units; $ncount++) { // loop from 1 to the number of units
           if(!$voted) { // if the user hasn't yet voted, draw the voting stars
              $rater.='<li><a href="db.php?j='.$ncount.'&q='.$id.'&t='.$ip.'&c='.$units.'" title="'.$ncount.' out of '.$units.'" class="r'.$ncount.'-unit rater" rel="nofollow">'.$ncount.'</a></li>';
           }
      }
      $ncount=0; // resets the count

      $rater.='  </ul>';
      $rater.='  <p';
      if($voted){ $rater.=' class="voted"'; }
      $rater.='>'.$id.' Rating: <strong> '.$rating1.'</strong>/'.$units.' ('.$count.' '.$tense.' cast)';
      $rater.='  </p>';
      $rater.='</div>';
      $rater.='</div>';
      return $rater;
}
}
?>

 

and heres the page where to display..

 

//test.php

<?php require('_drawrating.php');?>

<head>
<title>Multiple Ajax Star Rating Bars</title>

<script type="text/javascript" language="javascript" src="js/behavior.js"></script>
<script type="text/javascript" language="javascript" src="js/rating.js"></script>

<link rel="stylesheet" type="text/css" href="css/default.css" />
<link rel="stylesheet" type="text/css" href="css/rating.css" />
</head>

<body>


<?php echo rating_bar('2id',5); ?>


</body>
</html>

 

Can somebody please help me? THanks  :confused:

Link to comment
Share on other sites

so, i'm trying to understand here.

 

each page can have several pictures.  does each page have a unique ID in the database?

 

and then you give each picture an ID when they get displayed to the page, correct?

 

just set up a table where you store the page ID as well as the picture ID with the rating:

 

table: ratings
+---------+------------+----------------+
| page_id | picture_id | picture_rating |
+---------+------------+----------------+
|    5    |     2      |       7        |
+---------+------------+----------------+
|    5    |     3      |       9        |
+---------+------------+----------------+

 

and each time somebody goes to rate an image, check that table, and if the picture already has a rating in relationship to the page they're on, just update `picture_rating` (using your algorithm first of course), and that should suffice.

 

you can also add user tracking information to the table (or session/whatever), so that users cannot vote more than once on a specific picture.  you might already have that with the AJAX, i didn't look.

 

is that what you're going towards?

Link to comment
Share on other sites

dont mind the page id.. just say theres 1 picture per page and i want that 5 stars linked with the id of the picture..

 

how exactly am I going to plug this function into my existing code? im having trouble getting it to work with the code.. please help me

 

and yes i have the tracking information set up..

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.