Jump to content

Recommended Posts

I have a star rating system which uses this update php and js:

 

PHP

if($_GET['do']=='rate'){
// do rate
rate($show, $_GET['id']);
} else if($_GET['do']=='getrate'){
// get rating
getRating($show, $_GET['id']);
}

// function to retrieve
function getRating($show, $url){
$rs=@mysql_fetch_array($result);
// set width of star
$rating = (@round($rs[value] / $rs[counter],1)) * 20; 
echo $rating;
}

// function to insert rating
function rate($show, $url){
global $dbh;
$text = strip_tags($_GET['']);
$update = "update `" . $show . "_episodes` set counter = counter + 1, value = value + ".$_GET['rating']."  where md5(url)='".$url."'";
$result = mysql_query($update);

 

JS

// JavaScript Document
$(document).ready(function() {
	// get rating function
	function getRating(show, id){
		$.ajax({
			type: "GET",
			url: "../update.php",
			data: "do=getrate&id="+id+"&show="+show,
			cache: false,
			async: false,
			success: function(result) {
				// apply star rating to element
				$("#current-rating-"+id+"").css({ width: "" + result + "%" });
			},
			error: function(result) {
				alert("some error occured, please try again later");
			}
		});
	}
	// link handler
	$('.ratelinks li a').click(function(){
		// get the parent id
		var idStar = $(this).parent().parent().attr('id');
		var show = $(this).parent().parent().attr('show');
		$.ajax({
			type: "GET",
			url: "../update.php",
			data: "rating="+$(this).text()+"&do=rate&show="+show+"&id="+idStar,
			cache: false,
			async: false,
			success: function(result) {
				// remove #ratelinks element to prevent another rate
				$("#ratelinks").remove();
				// get rating after click
				getRating(show, idStar);
			},
			error: function(result) {
				alert("some error occured, please try again later");
			}
		});

	});
});

 

Now i decided i wanted to show the percentage so i added this to the PHP:

 

// function to retrieve Percentage
function getPercentage($show, $url){
$per=@mysql_fetch_array($result);
// set percentage
if ($per[counter] >= 1){
    $percentage = (round($per[value] * 100) / ($per[counter]*5));
    echo "( <FONT COLOR=RED>".$percentage."%</FONT>  from ".$per[counter]." votes )";
    }
    else if ($per[counter] < 1){
    echo "(No Votes Yet!)";
    }
}

 

Which does work, but requires a page reload to make it show the new percentage... (whereas the stars update automatically)

 

so how can i set $per[counter], $per[value] and $percentage? using the javascript?

 

Seany

Link to comment
https://forums.phpfreaks.com/topic/246876-how-to-set-new-values-to-a-variable/
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.