рейтинг система с ajax

hellfire

Registered
Някой знае ли някаква такава система да е ajax аз имам един код ама не знам как да го подкарам
Код:
<LINK href="http://vbox.bg/css/style.css" type="text/css" rel="stylesheet">

<script type="text/javascript">
var UT_RATING_IMG = 'http://vbox.bg/imgs/rate_4.gif';
var UT_RATING_IMG_HOVER = 'http://vbox.bg/imgs/rate_orange.gif';
var UT_RATING_IMG_HALF = 'http://vbox.bg/imgs/rate_2.gif';
var UT_RATING_IMG_BG = 'http://vbox.bg/imgs/rate_0.gif';
var UT_RATING_IMG_REMOVED = 'http://vbox.bg/imgs/frog_removed.gif';

function UTRating(ratingElementId, maxFrogs, objectName, formName, ratingField, ratingMessageId, componentSuffix, messages, videoid)
{
	this.ratingElementId = ratingElementId;
	this.maxFrogs = maxFrogs;
	this.objectName = objectName;
	this.formName = formName;
	this.ratingField = ratingField;
	this.ratingMessageId = ratingMessageId
	this.componentSuffix = componentSuffix
	this.messages = messages;

	this.frogTimer = null;
	this.frogCount = 0;
	
	// pre-fetch image
	(new Image()).src = UT_RATING_IMG;
	(new Image()).src = UT_RATING_IMG_HALF;

	function showFrogs(frogNum, skipMessageUpdate) {
		this.clearFrogTimer();
		this.greyFrogs();
		this.colorFrogs(frogNum, 1);
		if(!skipMessageUpdate)
			this.setMessage(frogNum, messages);
	}

	function setMessage(frogNum) {
//		messages = new Array("Rate this video", "Poor", "Nothing special", "Worth watching", "Pretty cool", "Awesome!");
		document.getElementById(this.ratingMessageId).innerHTML = this.messages[frogNum];
	}

	function colorFrogs(frogNum) {
		if(document.getElementById("israted").innerHTML == 0){
		for (var i=0; i < frogNum; i++) {
			document.getElementById('frog_'  + this.componentSuffix + "_" + (i+1)).src = UT_RATING_IMG_HOVER;
		}
		}
	}

	function greyFrogs() {
		if(document.getElementById("israted").innerHTML == 0){
		for (var i=0; i < this.maxFrogs; i++)
			if (i <= this.frogCount) {
				document.getElementById('frog_' + this.componentSuffix + "_"  + (i+1)).src = UT_RATING_IMG_BG;
			}
			else
			{
				document.getElementById('frog_' + this.componentSuffix + "_"  + (i+1)).src = UT_RATING_IMG_BG;
			}
		}
	}

	function setFrogs(frogNum) {
		this.frogCount = frogNum;
		this.drawFrogs(frogNum);
		document.forms[this.formName]['video_rating'].value = frogNum;
		var ratingElementId = this.ratingElementId;
		var url = "http://"+document.forms[this.formName]['videoid'].value+".vbox.bg/index.php";
		var query = "video_rating="+frogNum+"&moduleid=videos&action=rate&videoid="+document.forms[this.formName]['videoid'].value;
		var handler = "true";
		postAJAX(url, query, handler, true)
	}

	function drawFrogs(frogNum, skipMessageUpdate) {
		this.frogCount=frogNum;
		this.showFrogs(frogNum, skipMessageUpdate);
	}

	function clearFrogs() {
		this.frogTimer = setTimeout(this.objectName + ".resetFrogs()", 300);
	}

	function resetFrogs() {
		this.clearFrogTimer();
		if (this.frogCount)
			this.drawFrogs(this.frogCount);
		else
			this.greyFrogs();
		this.setMessage(0);
	}

	function clearFrogTimer() {
		if (this.frogTimer) {
			clearTimeout(this.frogTimer);
			this.frogTimer = null;
		}
	}

	this.clearFrogs = clearFrogs;
	this.clearFrogTimer = clearFrogTimer;
	this.greyFrogs = greyFrogs;
	this.colorFrogs = colorFrogs;
	this.resetFrogs = resetFrogs;
	this.setFrogs = setFrogs;
	this.drawFrogs = drawFrogs;
	this.showFrogs = showFrogs;
	this.setMessage = setMessage;

}

function onLoadPush()
{
	if (document.forms['rate_form'])
	{
		loadFrogs();
	}
}

function searchSet(tag)
{
	document.smplSearch.simpleSearch.value = tag;
	document.smplSearch.submit();
}

function postAJAX(url, query, handler, replaceBool)
{
    var status = false;
    var contentType = "application/x-www-form-urlencoded; charset=UTF-8";

    // Native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
        //request.onreadystatechange = handler;
        request.open("post", url, true);
        request.setRequestHeader("Content-Type", contentType);
        request.send(query);
        status = true;

    // ActiveX XMLHttpRequest object
    } else if (window.ActiveXObject) {
        request = new ActiveXObject("Microsoft.XMLHTTP");
        if (request) {
           // request.onreadystatechange = handler;
            request.open("post", url, true);
            request.setRequestHeader("Content-Type", contentType);
            request.send(query);
            status = true;
        }
    }

	if(replaceBool == true){
		request.onreadystatechange = function(){
			if (request.readyState == 4) {
        		if (request.status == 200) {
            		document.getElementById("rating").innerHTML = request.responseText;
            		document.getElementById("israted").innerHTML = 1;
        		} else {
            		alert('Eiaoa i?iaeai nun cayaeaoa Ae.');
        		}
    		}
		}
		//document.getElementById(id).innerHTML = httpRequest.responseText;
	}
    //return status;
}

function getAJAX(url)
{
    var status = false;
    var contentType = "application/x-www-form-urlencoded; charset=UTF-8";

    // Native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
        request.open("get", url, true);
        request.setRequestHeader("Content-Type", contentType);
        request.send(null);
        status = true;

    // ActiveX XMLHttpRequest object
    } else if (window.ActiveXObject) {
        request = new ActiveXObject("Microsoft.XMLHTTP");
        if (request) {
            request.open("get", url, true);
            request.setRequestHeader("Content-Type", contentType);
            request.send(null);
            status = true;
        }
    }
}


function changeCategory(videoid) 
{
    var url = "http://"+videoid+".vbox.bg/index.php";
    var query = "&moduleid=videos&action=change_category&videoid="+videoid+"&new_cat="+document.forms['category_form']['category'].value;
    var handler = "true";
    postAJAX(url, query, handler)

}

function confirmSubmit()
{
    var agree = confirm("Aia oe na?eicii ee aa ie??");
    if (agree == true)
        return true;
    else
        return false;
}

function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   sendComments();
   return false;
   }
else
   return true;
}
</script>
<div class="small" style="margin-top:20px; margin-bottom:4px;" id="ratingMessage">Гласувай за този клип:</div>
						<div id="israted" style="display: none">0</div>
			<div id="rating">
				<form style="display:none;" id="rate_form" name="rate_form" action="/index.php" method="POST">

					<input type="hidden" name="video_rating" id="video_rating" value="">
					<input type="hidden" id="moduleid" name="moduleid" value="videos" />
					<input type="hidden" id="action" name="action" value="rate" />
					<input type="hidden" id="videoid" name="videoid" value="e8k1a6k1" />
					<input type="hidden" id="showFrogs" name="showFrogs" value="5" />
				</form>
				<script src="http://vbox.bg/js/rating_init.js" language="javascript" type="text/javascript"></script>
					

									<a href="#" onclick="ratingComponent.setFrogs(1); return false;" onmouseover="ratingComponent.showFrogs(1);" onmouseout="ratingComponent.clearFrogs();">
								<img src="http://vbox.bg/imgs/rate_0.gif" id="frog__1" style="border: 0px" alt="">								</a>
					<a href="#" onclick="ratingComponent.setFrogs(2); return false;" onmouseover="ratingComponent.showFrogs(2);" onmouseout="ratingComponent.clearFrogs();">
								<img src="http://vbox.bg/imgs/rate_0.gif" id="frog__2" style="border: 0px" alt="">										</a>
					<a href="#" onclick="ratingComponent.setFrogs(3); return false;" onmouseover="ratingComponent.showFrogs(3);" onmouseout="ratingComponent.clearFrogs();">
								<img src="http://vbox.bg/imgs/rate_0.gif" id="frog__3" style="border: 0px" alt="">									</a>
					<a href="#" onclick="ratingComponent.setFrogs(4); return false;" onmouseover="ratingComponent.showFrogs(4);" onmouseout="ratingComponent.clearFrogs();">

								<img src="http://vbox.bg/imgs/rate_0.gif" id="frog__4" style="border: 0px" alt="">									</a>
					<a href="#" onclick="ratingComponent.setFrogs(5); return false;" onmouseover="ratingComponent.showFrogs(5);" onmouseout="ratingComponent.clearFrogs();">
								<img src="http://vbox.bg/imgs/rate_0.gif" id="frog__5" style="border: 0px" alt="">								</a>
				<div id="ratingvalue">0</div>
								<br />
			</div>
някой имали идея
може ли да ми помогнете плс важно е !
 
еее пич страшен си 1-вото нямаше за глас а второто бачка самоче има какво да се пипа ...
мерси много !
 
почти го пригодих към тва но нещо съм объркал ето целия код може ли ми помогнеш че нещо ме мързи (не съм спал цяла нощ амм това да не се счита като оправдание )
rpc.php
Код:
<?php
header("Cache-Control: no-cache");
header("Pragma: nocache");
$server = 'localhost';
$db_name = 'allz';
$dbusername = 'root';
$dbpassword = '';
$conn = mysql_connect($server,$dbusername,$dbpassword) or die (mysql_error());
@mysql_select_db($db_name,$conn)or die (mysql_error());
$db_ipta="ip_rat";
$vote_sent = $_REQUEST['j'];
$id_sent = $_REQUEST['q'];
$ip_num = $_REQUEST['t'];
$query=mysql_query("
	SELECT num_votes_st,rating_st
	FROM klinkove
	WHERE `id`='$id_sent'
	LIMIT 1
	")or die(" Error: ".mysql_error());
	$num = mysql_num_rows($query);
$numbers=mysql_fetch_assoc($query);
$checkIP = $numbers['ip'];
$count=$numbers['num_votes_st'];//how many votes total
$current_rating=$numbers['rating_st'];//total number of rating added together and stored
$sum=$vote_sent+$current_rating;// add together the current vote value and the total vote value
$tense=($count==1) ? "vote" : "votes";//plural form votes/vote
echo $voted;
if($voted){

$new_back = 
"<ul class=\"unit-rating\">\n".
"<li class=\"current-rating\" style=\"width:". @number_format($current_rating/$count,2)*30 ."px;\">Current rating.</li>\n".
"<li class=\"r1-unit\">1</li>\n".
"<li class=\"r2-unit\">2</li>\n".
"<li class=\"r3-unit\">3</li>\n".
"<li class=\"r4-unit\">4</li>\n".
"<li class=\"r5-unit\">5</li>\n".
"<li class=\"r6-unit\">6</li>\n".
"<li class=\"r7-unit\">7</li>\n".
"<li class=\"r8-unit\">8</li>\n".
"<li class=\"r9-unit\">9</li>\n".
"<li class=\"r10-unit\">10</li>\n".
"</ul>".
	"<p>Rating: <strong>".@number_format($current_rating/$count,1)."</strong> out of ".$count." ".$tense." cast".
	"<br />\n <span style=\"color:red;\">You have previously voted!</span></p>";//show the current value of the vote with the current numbers



//if not, echo the new changes
}else{


if($sum==0){
$added=0;
}else{
$added=$count+1;
}	
$conn = mysql_connect($server,$dbusername,$dbpassword) or die (mysql_error());
@mysql_select_db($db_name,$conn)or die (mysql_error());
mysql_query("UPDATE `klinkove` SET `num_votes_st`=num_votes_st+1, `rating_st`='$sum' WHERE `id`='$id_send'") or die (mysql_error());
mysql_query("INSERT INTO `ip_rat`(`vv_id`,`clip_id`,`ip`) 
	VALUES ('NULL', '$id_send', '".$_SERVER['REMOTE_ADDR']."');") or die ("eRR:".mysql_error());
	
$conn = mysql_connect($server,$dbusername,$dbpassword) or die (mysql_error());
@mysql_select_db($db_name,$conn)or die (mysql_error());
$query=mysql_query("	
	SELECT num_votes_st,rating_st
	FROM klinkove
	WHERE `id`='$id_sent'
	LIMIT 1")or die(" Error: ".mysql_error());
$numbers=mysql_fetch_assoc($query);
$count=$numbers['num_votes_st'];//how many votes total
$current_rating=$numbers['rating_st'];//total number of rating added together and stored


$new_back = 
"<ul class=\"unit-rating\">\n".
"<li class=\"current-rating\" style=\"width:". @number_format($current_rating/$count,2)*30 ."px;\">Current rating.</li>\n".
"<li class=\"r1-unit\">1</li>\n".
"<li class=\"r2-unit\">2</li>\n".
"<li class=\"r3-unit\">3</li>\n".
"<li class=\"r4-unit\">4</li>\n".
"<li class=\"r5-unit\">5</li>\n".
"<li class=\"r6-unit\">6</li>\n".
"<li class=\"r7-unit\">7</li>\n".
"<li class=\"r8-unit\">8</li>\n".
"<li class=\"r9-unit\">9</li>\n".
"<li class=\"r10-unit\">10</li>\n".
"</ul>".
"<br />Rating: <strong>".@number_format($sum/$added,1)."</strong> out of ".$added." ".$tense." cast".
"<br />Thank you for your vote!</p></div>";//show the updated value of the vote


}
mysql_close($conn);

//name of the div id to be updated | the html that needs to be changed
$output = "unit_long$id_sent|$new_back";
echo $output;
?>
function.php
Код:
<?php

function rating_bar($id) { 
$server = 'localhost';
$db_name = 'allz';
$dbusername = 'root';
$dbpassword = '';
$conn = mysql_connect($server,$dbusername,$dbpassword) or die (mysql_error());
@mysql_select_db($db_name,$conn)or die (mysql_error());
$query=mysql_query("	
	SELECT num_votes_st,rating_st
	FROM klinkove
	WHERE `id`='$id_sent'")or die(" Error: ".mysql_error());
$numbers=mysql_fetch_assoc($query);
$count=$numbers['num_votes_st'];//how many votes total
$current_rating=$numbers['rating_st'];//total number of rating added together and stored
$tense=($count==1) ? "vote" : "votes";//plural form votes/vote

mysql_close($conn);

$ip = $_SERVER['REMOTE_ADDR'];
$conn = mysql_connect($server,$dbusername,$dbpassword) or die (mysql_error());
@mysql_select_db($db_name,$conn)or die (mysql_error());
$result=mysql_query("SELECT count(*) FROM ip_rat WHERE `ip`='".$ip."' AND `clip_id`='$id'") or die (mysql_error()); //Pattern match ip:suggested by Bramus! http://www.bram.us/ - this variable searches through the previous ip address that have voted and returns true or false
$voted = mysql_result($result, 0, 0); 

// draw the rating bars
// still to do: need to not allow them to vote if they've already been here and voted

?>
		<?php echo $id ?>. rating system<br />
		<div id="unit_long<?php echo $id ?>">
		<ul class="unit-rating">
		<li class='current-rating' style="width:<?php echo @number_format($current_rating/$count,2)*30; ?>px;">Currently <?php echo @number_format($current_rating/$count,2); ?>/10</li>
			<?php
			for ($ncount = 1; $ncount <= 10; $ncount++) { ?>
				<li><a href="#" title="<?php echo $ncount ?> out of 10" class="r<?php echo $ncount ?>-unit" onclick="javascript:sndReq('<?php echo $ncount ?>','<?php echo $id ?>','<?php echo $ip ?>')"><?php echo $ncount ?></a></li>
		<?	}
			$ncount=0; // resets the count
			?>
		</ul>
		<p>Rating: <strong> <?php echo @number_format($current_rating/$count,1) ?></strong> out of <?php echo $count ?> <?php echo $tense ?> cast 
			<br />
			 
		</div>
		<br /><br /><br />
<?
}
?>
и index.php
Код:
<?php 
include("../../../inc/config.php");
include('functions.php'); 
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Multiple Ajax Star Rating Bars</title>
<script type="text/javascript" src="js.js"></script>
	<link rel="stylesheet" type="text/css" href="stars.css" />
</head>

<body>

<?php if($_GET['id']){ rating_bar($_GET['id']);} ?>

</body>
</html>
нещо при селектите съм объркал че да не отброява колко е рейтинга
 

Back
Горе