Jump to content

Implementing a Ratings System


FlashNinja

Recommended Posts

So I'm implementing a ratings system into my site, the problem here though is my WHILE loop for the voting part of the rating system seems to be bugged. The SQL queries that are used for this script work, the problem seems to be that the $rating variable used to start the while loop never seems to contain anything - I've tried echoing it to confirm this fact. Can anyone see what I've done wrong in this script?

 

<?php 
  
  include 'connect.php';
  
  session_start();
  $_SESSION['username'];
  $username = $_SESSION['username'];
  $member = $_GET['usr'];

  if(!(isset($_SESSION['login']) && $_SESSION['login']!= " ")){
       header("Location: login.php");
   }
   
  $tablename = 'usr_test';   
  $sql1 = "SELECT * FROM $tablename WHERE usr = '$member'";
  $result1 = mysql_query($sql1) or die (mysql_error());
  $display = mysql_fetch_row($result1);
  $newline = "\n";

?>

<html>

<h1>User Profile</h1>

<body>

<div ="userprofile">

Username: <?php echo $member; ?>
<br></br>
Email: <?php echo $display[3]; ?>
<br></br>
University: <?php echo $display[4];?>
<br></br>
Subject: <?php echo $display[5];?>
<br></br>

</div>

<div ="rating">

<?php 
while($rating = mysql_fetch_array($result1))
      {
   if ($rating['rating'] != 0)
   $curr = $rating['total'] / $rating['rating'];
       
   else
   
   $curr = 0;
   
   echo "Rating: ". round($curr, 1) . "<br>";
   
   echo "Rate this seller: "; 
       echo "<a href=".$_SERVER['PHP_SELF']."&mode = vote & voted =1&id =".$rating[usr].">Vote 1</a> | "; 
       echo "<a href=".$_SERVER['PHP_SELF']."&mode = vote & voted =2&id=".$rating[usr].">Vote 2</a> | "; 
       echo "<a href=".$_SERVER['PHP_SELF']."&mode = vote & voted =3&id=".$rating[usr].">Vote 3</a> | "; 
       echo "<a href=".$_SERVER['PHP_SELF']."&mode =vote & voted =4&id=".$rating[usr].">Vote 4</a> | "; 
       echo "<a href=".$_SERVER['PHP_SELF']."&mode = vote & voted =5&id=".$rating[usr].">Vote 5</a><p>"; 
       } 

?>

   

</div>

<div>

</html>

Link to comment
Share on other sites

It seems a little strange that you would use the same SQL result for both echoing user info and then looping through ratings. What does the information in this table look like? Is there really more than one row where usr=$member?

Link to comment
Share on other sites

I fixed that problem, allowing the voting system to be seen and used . . . but now another problem realting to this has arrived. When a user visits another users profile page, the second user''s username is kept in the url like this: localhost/profile.php?usr=FlashNinja. I then use the usr variable - now $member - to display the seconds user's profile information. The problem that's occuring now though is that I'm losing that username information in the url when I click a voting number in my voting form on the profile page. This of course makes the profile page now useless. Is there anyway I can get fix this so I can have both the user information AND the voting system on one page?

 

Full profile code:

 

<?php 
  
  include 'connect.php';
  
  session_start();
  $_SESSION['username'];
  $username = $_SESSION['username'];
  $member = $_GET['usr'];
  $cookie = $member;
  
  if(!(isset($_SESSION['login']) && $_SESSION['login']!= " ")){
       header("Location: login.php");
   }
   
   
  $tablename = 'usr_test';   
  $sql1 = "SELECT * FROM $tablename WHERE usr = '$member'";
  $result1 = mysql_query($sql1) or die (mysql_error());
  $display = mysql_fetch_row($result1);
  $newline = "\n";
  
  if ( 'mode' == "vote") 
     { if(isset($_COOKIE[$cookie])) 
		{ 
		Echo "Sorry You have already rated this user <p>"; 
		} 
	else 
		{ 
		$month = 2592000 + time(); 
		setcookie($member, Voted, $month);
        
      mysql_query ("UPDATE $tablename SET total = total+$voted, rating = votes+1 WHERE usr = $member"); 
      Echo "You have voted!"; 
		} 
} 



?>

<html>

<h1>User Profile</h1>

<body>

<div ="userprofile">

Username: <?php echo $member; ?>
<br></br>
Email: <?php echo $display[3]; ?>
<br></br>
University: <?php echo $display[4];?>
<br></br>
Subject: <?php echo $display[5];?>
<br></br>

</div>

<div ="rating">

<?php 

       $rating = mysql_fetch_array($result1);
      
   if ($rating['rating'] != 0)
   $curr = $rating['total'] / $rating['rating'];
       
   else
   
   $curr = 0;
   
   echo "Rating: ". round($curr, 1) . "<br>";
   
   echo "Rate this seller: "; 
       echo "<a href=".$_SERVER['PHP_SELF']."?mode = vote & voted =1&id =".$rating['usr'].">Vote 1</a> | "; 
       echo "<a href=".$_SERVER['PHP_SELF']."?mode = vote & voted =2&id=".$rating['usr'].">Vote 2</a> | "; 
       echo "<a href=".$_SERVER['PHP_SELF']."?mode = vote & voted =3&id=".$rating['usr'].">Vote 3</a> | "; 
       echo "<a href=".$_SERVER['PHP_SELF']."?mode = vote & voted =4&id=".$rating['usr'].">Vote 4</a> | "; 
       echo "<a href=".$_SERVER['PHP_SELF']."?mode = vote & voted =5&id=".$rating['usr'].">Vote 5</a><p>"; 
       

?>

   

</div>

<div>

</html>

Link to comment
Share on other sites

just add the information to the url

<?php
echo "Rate this seller: "; 
       echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=1&id={$rating['usr']}\">Vote 1</a> | "; 
       echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=2&id={$rating['usr']}\">Vote 2</a> | "; 
       echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=3&id={$rating['usr']}\">Vote 3</a> | "; 
       echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode vote&voted=4&id={$rating['usr']}\">Vote 4</a> | "; 
       echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=5&id={$rating['usr']}\">Vote 5</a><p>"; 
?>

Link to comment
Share on other sites

Nearly there. As far as I can tell the voting works except the votes cast aren't being updated on to the database. I'm also trying to make a cookie of the id stored in the url so the user can only vote once, but the servers keeps throowing me an UNEXPECTED T_VARIABLE error. I've updated the code and am using fetch_row instead of an array.

 

<?php 
  
  include 'connect.php';
  
  session_start();
  $_SESSION['username'];
  $username = $_SESSION['username'];
  $member = $_GET['usr'];
  $cookie = Mysite$id;
  
  if(!(isset($_SESSION['login']) && $_SESSION['login']!= " ")){
       header("Location: login.php");
   }
   
   
  $tablename = 'usr_test';   
  $sql1 = "SELECT * FROM $tablename WHERE usr = '$member'";
  $result1 = mysql_query($sql1) or die (mysql_error());
  $display = mysql_fetch_row($result1);
  $newline = "\n";
  
  if ( "mode" == "vote") 
     { if(isset($_COOKIE[$cookie])) 
		{ 
		Echo "Sorry You have already rated this user <p>"; 
		} 
	else 
		{ 
		$month = 2592000 + time(); 
		setcookie(mysite.$id, Voted, $month);
        
      mysql_query ("UPDATE $tablename SET total = total+$voted, rating = votes+1 WHERE usr = $member"); 
      Echo "You have voted!"; 
		} 
} 



?>

<html>

<h1>User Profile</h1>

<body>

<div ="userprofile">

Username: <?php echo $member; ?>
<br></br>
Email: <?php echo $display[3]; ?>
<br></br>
University: <?php echo $display[4];?>
<br></br>
Subject: <?php echo $display[5];?>
<br></br>

</div>

<div ="rating">

<?php 
   
  $curr = $display[7] / $display[6];
   
  echo "Rating: ". round($curr, 1) . "<br>";
   
  echo "Rate this seller: "; 
       
  echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=1&id={$display[0]}\">Vote 1</a> | "; 
      echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=2&id={$display[0]}\">Vote 2</a> | "; 
      echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=3&id={$display[0]}\">Vote 3</a> | "; 
      echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=4&id={$display[0]}\">Vote 4</a> | "; 
      echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=5&id={$display[0]}\">Vote 5</a><p>"; 
       

?>

   

</div>

<div>

</html>

 

 

Link to comment
Share on other sites

Sorry for the double post, but I've nearly got it, just wondering if someone can beat me to the solution to this final problem with it. I've fixed up everything, the cookies are stored correctly and all the variables seemingly work, but the script still isn't updating the database with the votes. I've tested the SQL query, that does the actual updating, on phpmyadmin and it works as it should. This obviously means there's something wrong with the php side of things. Can anyone spot the problem?

 

<?php 
  
  include 'connect.php';
  
  session_start();
  $_SESSION['username'];
  $username = $_SESSION['username'];
  $member = $_GET['usr'];
  $id = $_GET['id'];
  $mode = $_GET['mode'];
  $voted = $_GET['voted'];
  $cookie = $id;
  
  if(!(isset($_SESSION['login']) && $_SESSION['login']!= " ")){
       header("Location: login.php");
   }
   
   
  $tablename = 'usr_test';   
  $sql1 = "SELECT * FROM $tablename WHERE usr = '$member'";
  $result1 = mysql_query($sql1) or die (mysql_error());
  $display = mysql_fetch_row($result1);
  $newline = "\n";
  
  if ( $mode == "vote") 
     { if(isset($_COOKIE[$cookie])) 
		{ 
		Echo "Sorry You have already rated this user <p>"; 
		} 
	else 
		{ 
		$month = 2592000 + time(); 
		setcookie($id, $voted, $month);
        
      mysql_query ("UPDATE $tablename SET total = total+$voted, rating = rating+1 WHERE usr = $member"); 
      Echo "You have voted!"; 
		} 
} 



?>

<html>

<h1>User Profile</h1>

<body>

<div ="userprofile">

Username: <?php echo $member; ?>
<br></br>
Email: <?php echo $display[3]; ?>
<br></br>
University: <?php echo $display[4];?>
<br></br>
Subject: <?php echo $display[5];?>
<br></br>

</div>

<div ="rating">

<?php 
      $denometer = $display[6];

  if 
  
  ($denometer == 0)
  $curr = 0;
  
  else 
  
  $curr = $display[7] / $display[6];
   
  echo "Rating: ". round($curr, 1) . "<br>";
   
  echo "Rate this seller: "; 
       
  echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=1&id={$display[0]}\">Vote 1</a> | "; 
      echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=2&id={$display[0]}\">Vote 2</a> | "; 
      echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=3&id={$display[0]}\">Vote 3</a> | "; 
      echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=4&id={$display[0]}\">Vote 4</a> | "; 
      echo "<a href=\"{$_SERVER['PHP_SELF']}?usr={$member}&mode=vote&voted=5&id={$display[0]}\">Vote 5</a><p>"; 
       

?>

   

</div>

<div>

</html>

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.