Jump to content

Help adding variable to URL and then processing it into my db


jacko_162

Recommended Posts

I have 2 pages which is for a rating system with stars.

 

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Ajax Rating System: Create Simple Ajax Rating System using jQuery AJAX and PHP : 99Points.Info</title>
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<link href="99points.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
// <![CDATA[	
$(document).ready(function(){	
$('#loader').hide(); 
$('#inner').children().click(function(){
	var a = $(this).attr("id");
	$.post("rating.php?value="+a, {
	}, function(response){
		$('#inner').fadeOut();
		$('#inner').html(unescape(response));
		$('#inner').fadeIn();
		setTimeout("hideMesg();", 2000);
	});
});	
});	

function hideMesg(){

$('.rating_message').fadeOut();
$.post("rating.php?show=1", {
}, function(response){
	$('#inner').html(unescape(response));
	$('#inner').fadeIn('slow');
});
}	
// ]]>
</script>
</head>
<body>
<?php
include("dbcon.php");

$result=mysql_query("select sum(rating) as rating from ratings");
$row=mysql_fetch_array($result);

$rating=$row['rating'];
$product_id = $_get[iD];

$quer = mysql_query("select rating from ratings");
$all_result = mysql_fetch_assoc($quer);
$rows_num = mysql_num_rows($quer);

if($rows_num > 0){
$get_rating = floor($rating/$rows_num);
$rem =  5 - $get_rating;
}
else
{
$rem = 5;
}?>
<h1>Ajax Test Rate System.</h1>

<div align="center" style="margin:30px;">
<div id="container">
	<img src="2.gif" alt="" height="300" />
	<div id="outer">
		<div id="inner">
			<?php
			for($k=1;$k<=$get_rating;$k++){?>
			<div class="rating_enb" id="<?php echo $k?>"> </div>
			<?php
			}?>
			<?php
			for($i=$rem;$i>=1;$i--){?>
			<div class="rating_dis" id="<?php echo $k?>"> </div>
			<?php
			$k++;
			}?>	
			<div class="rating_value"><?php echo ((@$get_rating) ? @$get_rating : '0')?>.0 (<?php echo $rows_num?>)</div>
		</div>
	</div>

</div>
</div>

</body>
</html>

 

then the rating.php (does the functions)

<?php
include("dbcon.php");
//echo $_SERVER['REMOTE_ADDR'].'--' . $_REQUEST['value'];
echo $_REQUEST['id'];
$users_ip = $_SERVER['REMOTE_ADDR'];
if($_REQUEST['value'])
{
$id = $_REQUEST['value'];

$result = mysql_query("select users_ip from ratings where users_ip='$users_ip'");
$num = mysql_num_rows($result);

if($num==0)
{
	$query = "insert into ratings (rating,users_ip) values ('$id','$users_ip')";
	mysql_query( $query);

	$result=mysql_query("select sum(rating) as rating from ratings");
	$row=mysql_fetch_array($result);

	$rating=$row['rating'];

	$quer = mysql_query("select rating from ratings");
	$all_result = mysql_fetch_assoc($quer);
	$rows_num = mysql_num_rows($quer);
	if($rows_num > 0){
	$get_rating = floor($rating/$rows_num);
	$rem =  5 - $get_rating;
	}

	?>
	<?php echo $_get['id']; ?>
<?php
	for($k=1;$k<=$get_rating;$k++){?>
	<div class="rating_enb" id="<?php echo $k?>"> </div>
	<?php
	}?>
	<?php
	for($i=$rem;$i>=1;$i--){?>
	<div class="rating_dis" id="<?php echo $k?>"> </div>
	<?php
	$k++;
	}?>	
	<div class="rating_value"><?php echo ((@$get_rating) ? @$get_rating : '0')?>.0 (<?php echo $rows_num?>)</div>

<?php
}
else
{
	echo '<div class="rating_message">You have already Voted for this product!!</div>';
}
}
if(@$_REQUEST['show']) // show rating again after showing message
{
$result=mysql_query("select sum(rating) as rating from ratings");
$row=mysql_fetch_array($result);

$rating=$row['rating'];

$quer = mysql_query("select rating from ratings");
$all_result = mysql_fetch_assoc($quer);
$rows_num = mysql_num_rows($quer);
if($rows_num > 0){
$get_rating = floor($rating/$rows_num);
$rem =  5 - $get_rating;
}?>
<?php
for($k=1;$k<=$get_rating;$k++){?>
<div class="rating_enb" id="<?php echo $k?>"> </div>
<?php
}?>
<?php
for($i=$rem;$i>=1;$i--){?>
<div class="rating_dis" id="<?php echo $k?>"> </div>
<?php
$k++;
}?>	
<div class="rating_value"><?php echo ((@$get_rating) ? @$get_rating : '0')?>.0 (<?php echo $rows_num?>)</div>
<?php
}?>

 

the script finds my ip address fine, and the value is forwarded and both added to my database.

 

 

what i wanted to add onto this is an id number in the URL.

 

namely "rating.php?ID=47" this is then going to be inserted into the db in the rating.php file.

 

can someone help me code this please?

Link to comment
Share on other sites

The Script works fine, i wanted to make the stars script work on multiple products, so i added "product_id" column to my database.

 

i can pass a url such as index.php?ID=XX but i am not sure how to alter the above code to pull the id from URL, pass it through the script and eventually add the product_id to the database.

Link to comment
Share on other sites

if you see my first post i have the 2 pages needed "index.php" and "ratings.php"

 

i then typed index.php?ID=47

 

and in index .php i have the following:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Ajax Rating System: Create Simple Ajax Rating System using jQuery AJAX and PHP : 99Points.Info</title>
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<link href="99points.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
// <![CDATA[	
$(document).ready(function(){	
$('#loader').hide(); 
$('#inner').children().click(function(){
	var a = $(this).attr("id");
	$.post("rating.php?value="+a, {
	}, function(response){
		$('#inner').fadeOut();
		$('#inner').html(unescape(response));
		$('#inner').fadeIn();
		setTimeout("hideMesg();", 2000);
	});
});	
});	

function hideMesg(){

$('.rating_message').fadeOut();
$.post("rating.php?show=1", {
}, function(response){
	$('#inner').html(unescape(response));
	$('#inner').fadeIn('slow');
});
}	
// ]]>
</script>
</head>
<body>
<?php
include("dbcon.php");

$result=mysql_query("select sum(rating) as rating from ratings");
$row=mysql_fetch_array($result);

$rating=$row['rating'];
$pid = $_get['ID'];

$quer = mysql_query("select rating from ratings");
$all_result = mysql_fetch_assoc($quer);
$rows_num = mysql_num_rows($quer);

if($rows_num > 0){
$get_rating = floor($rating/$rows_num);
$rem =  5 - $get_rating;
}
else
{
$rem = 5;
}?>
<h1>Ajax Test Rate System.</h1>

<div align="center" style="margin:30px;">
<div id="container">
	<img src="2.gif" alt="" height="300" />
	<div id="outer">
		<div id="inner">
			<?php
			for($k=1;$k<=$get_rating;$k++){?>
			<div class="rating_enb" id="<?php echo $k?>"> </div>
			<?php
			}?>
			<?php
			for($i=$rem;$i>=1;$i--){?>
			<div class="rating_dis" id="<?php echo $k?>"> </div>
			<?php
			$k++;
			}?>	
			<div class="rating_value"><?php echo ((@$get_rating) ? @$get_rating : '0')?>.0 (<?php echo $rows_num?>)</div>
		</div>
	</div>

</div>
</div>

</body>
</html>

 

im actually not sure where and how it calls rating.php i assume its in the javascript, of which i have NO clue..

 

i need to pass the ?ID=47 to rating.php and from there i assume i need to use the $_GET and insert the result into the "product_id" column in Database.

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.