All pastes #394424 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-03-14 09:50 UTC
rendered paste body
<?php
/*
Page:           _drawrating.php
Created:        Aug 2006
Last Mod:       Mar 11 2007
The function that draws the rating bar.
--------------------------------------------------------- 
ryan masuga, masugadesign.com
ryan@masugadesign.com 
--------------------------------------------------------- */

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;}

$query=mysql_query("SELECT total_votes, total_value, used_ips FROM $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 $tableName (`id`,`total_votes`, `total_value`, `used_ips`) VALUES ('$id', '0', '0', '')";
$result = mysql_query($sql);
}

$numbers=mysql_fetch_assoc($query);
$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 $tableName WHERE used_ips LIKE '%".$ip."%' AND id='".$id."' ")); 

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


if ($static == 'static') {

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

echo $static_rater;
?>

		<p class="static"><?php echo $id ?>. Rating: <strong> <?php echo $rating1 ?></strong>/<?php echo $units ?> (<?php echo $count ?> <?php echo $tense ?> cast) <em>This is 'static'.</em>
		</p>
</div>
</div>



<?php
} else {
?>



<div class="ratingblock">

<div id="unit_long<?php echo $id ?>">
		<ul id="unit_ul<?php echo $id ?>" class="unit-rating" style="width:<?php echo $unitwidth*$units; ?>px;">
		<li class="current-rating" style="width:<?php echo $rating_width; ?>px;">Currently <?php echo $rating2; ?>/<?php echo $units ?></li>
<?php
	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 
		?>
<li><a href="db.php?j=<?php echo $ncount ?>&amp;q=<?php echo $id ?>&amp;t=<?php echo $ip ?>&amp;c=<?php echo $units ?>" title="<?php echo $ncount ?> out of <?php echo $units ?>" class="r<?php echo $ncount ?>-unit rater" rel="nofollow"><?php echo $ncount ?></a></li>
<?php
	 } 
  }
	$ncount=0; // resets the count
?>
		</ul>
		<p<?php if($voted){?> class="voted"<?php } ?> ><?php echo $id; ?>. Rating: <strong> <?php echo $rating1; ?></strong>/<?php echo $units ?> (<?php echo $count ?> <?php echo $tense ?> cast)
		</p>
</div>
</div>
<?
}

}
?>