Jump to content

Rating Script Help


Dada78

Recommended Posts

I have this rating script on the page below the pictures. I am getting the error Warning: Division by zero as seen on the page. Also when you mouse over one of the links for the rating you will notice it is not putting the id of the page at the end of the URL for the voting link.  Can anyone offer some help or suggestions?

 

http://mesquitechristmas.com/local/display.php?id=81

 

 

 

<?php

// MAKE CONNECTION
   include ('db_connect.php');

//We only run this code if the user has just clicked a voting link
if ( $mode=="vote")
{

//If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id";
if(isset($_COOKIE[$cookie]))
{
Echo "Sorry You have already ranked that site <p>";
}

//Otherwise, we set a cooking telling us they have now voted
else
{
$month = 2592000 + time();
setcookie(Mysite.$id, Voted, $month); 

//Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
mysql_query ("UPDATE users SET total = total+$voted, votes = votes+1 WHERE id = $id");
Echo "Your vote has been cast <p>";
}
} 

//This calculates the sites ranking and then outputs it - rounded to 1 decimal
$current = $ratings[total] / $ratings[votes]; 
Echo "Rated " . round($current, 1). " from 0 people | <b>Rate This Display</b>: "; 

//This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item

Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$ratings[id].">1</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=2&id=".$ratings[id].">2</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=3&id=".$ratings[id].">3</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=4&id=".$ratings[id].">4</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=5&id=".$ratings[id].">5</a><p>";

?>

 

-Thanks

 

 

Link to comment
Share on other sites

On this line, one of the variables is equal to 0. That is where your division by 0 error is coming from.

$current = $ratings[total] / $ratings[votes];

 

You need to make an if statement checking if those variables are equal to 0, and if they are you already know the rating should be 0 by default.

 

As for your links

Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$ratings[id].">1</a> | ";

 

Shouldn't the $rating[id] variable be changed to $_GET['id']?

Link to comment
Share on other sites

I fixed the links issue by doing this.

 

Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$id.">1</a> | ";

 

That adds the ID now.

 

I know my problem is coming from

 

$current = $ratings[total] / $ratings[votes];

 

But how do I fix that?

 

 

Link to comment
Share on other sites

Yea that is what I thought. I am not sure why it isn't working.

 

Here is my current code.

 

<?php

// MAKE CONNECTION
   include ('db_connect.php');

if (isset($_GET['id'])) {
  $id = mysql_real_escape_string($_GET['id']);
  $sql = "SELECT * FROM users WHERE id = $id;";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
    $votes = $row['votes'];
    } else {
      die("No user found");
    }
  } else {
    die(mysql_error());
  }
}

//We only run this code if the user has just clicked a voting link
if ( $mode=="vote")
{

//If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id";
if(isset($_COOKIE[$cookie]))
{
Echo "Sorry You have already ranked that site <p>";
}

//Otherwise, we set a cooking telling us they have now voted
else
{
$month = 2592000 + time();
setcookie(Mysite.$id, Voted, $month); 

//Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
mysql_query ("UPDATE users SET total = total+$voted, votes = votes+1 WHERE id = $id");
Echo "Your vote has been cast <p>";
}
} 

//This calculates the sites ranking and then outputs it - rounded to 1 decimal
if ($rating['votes'] == 0){
   $current = 0;
} else {
   $current = $ratings[total] / $ratings[votes]; 
} 
Echo "Rated " . round($current, 1). " from $votes people | <b>Rate This Display</b>: "; 

//This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item

Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$id.">1</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=2&id=".$id.">2</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=3&id=".$id.">3</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=4&id=".$id.">4</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=5&id=".$id.">5</a><p>";

?>

 

 

 

Link to comment
Share on other sites

I added this below:

 

//Puts SQL Data into an array
$data = mysql_query("SELECT * FROM users") or die(mysql_error());

//Now we loop through all the data
while($ratings = mysql_fetch_array( $data ))
{ 

 

 

To this code

 

// MAKE CONNECTION
   include ('db_connect.php');

if (isset($_GET['id'])) {
  $id = mysql_real_escape_string($_GET['id']);
  $sql = "SELECT * FROM users WHERE id = $id;";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
    $votes = $row['votes'];
    } else {
      die("No user found");
    }
  } else {
    die(mysql_error());
  }
}

//We only run this code if the user has just clicked a voting link
if ( $mode=="vote")
{

//If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id";
if(isset($_COOKIE[$cookie]))
{
Echo "Sorry You have already ranked that site <p>";
}

//Otherwise, we set a cooking telling us they have now voted
else
{
$month = 2592000 + time();
setcookie(Mysite.$id, Voted, $month); 

//Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
mysql_query ("UPDATE users SET total = total+$voted, votes = votes+1 WHERE id = $id");
Echo "Your vote has been cast <p>";
}
} 

//Puts SQL Data into an array
$data = mysql_query("SELECT * FROM users") or die(mysql_error());

//Now we loop through all the data
while($ratings = mysql_fetch_array( $data ))
{ 

//This calculates the sites ranking and then outputs it - rounded to 1 decimal
if ($rating['votes'] == 0){
   $current = 0;
} else {
   $current = $ratings[total] / $ratings[votes]; 
} 
Echo "Rated " . round($current, 1). " from $votes people | <b>Rate This Display</b>: "; 

//This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item

Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$id.">1</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=2&id=".$id.">2</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=3&id=".$id.">3</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=4&id=".$id.">4</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=5&id=".$id.">5</a><p>";
}
?>

 

 

 

 

But now it shows like 5 lines of it and it still doesn't show the rating or update the DB. Anyone have any idea what I might be doing wrong?

 

http://www.mesquitechristmas.com/local/display.php?id=81

 

-Thanks

 

 

Link to comment
Share on other sites

I have made some changes I get this error when I click on one of the links to vote.

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

here is the code

 

<?php

// MAKE CONNECTION
   include ('db_connect.php');

if (isset($_GET['id'])) {
  $id = mysql_real_escape_string($_GET['id']);
  $sql = "SELECT * FROM users WHERE id = $id;";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
    $votes = $row['votes'];
    $total = $row['total'];
    } else {
      die("No user found");
    }
  } else {
    die(mysql_error());
  }
}

//We only run this code if the user has just clicked a voting link
if ( $mode=="vote")
{

//If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id";
if(isset($_COOKIE[$cookie]))
{
Echo "Sorry You have already ranked that site <p>";
}

//Otherwise, we set a cooking telling us they have now voted
else
{
$month = 2592000 + time();
setcookie(Mysite.$id, Voted, $month); 

//Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
mysql_query ("UPDATE users SET total = total+$voted, votes = votes+1 WHERE id = $id");
Echo "Your vote has been cast <p>";
}
} 

//This calculates the sites ranking and then outputs it - rounded to 1 decimal
if ($rating['votes'] == 0){
   $current = 0;
} else {
   $current = $ratings[total] / $ratings[votes]; 
} 
Echo "Rated " . round($current, 1). " from $votes people | <b>Rate This Display</b>: "; 

//This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item

Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$ratings[id].">1</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=2&id=".$ratings[id].">2</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=3&id=".$ratings[id].">3</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=4&id=".$ratings[id].">4</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=5&id=".$ratings[id].">5</a><p>";

?>

 

Can anyone tell me what I am doing wrong and how I can get this working?

 

 

Link to comment
Share on other sites

if ($mode=="vote")

 

is $mode ever set?

 

if (isset($_GET['mode'] && $_GET['mode']=="vote")

 

 

  $sql = "SELECT * FROM users WHERE id = $id;";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
    $votes = $row['votes'];
    $total = $row['total'];

 

u setup the query, u get number of rows, but ya never get an actual row

  $sql = "SELECT * FROM users WHERE id = $id;";
  if ($result = mysql_query($sql)) {
    if (mysql_fetch_assoc($result)) {
    $votes = $row['votes'];
    $total = $row['total'];

 

why the 2 different names for $cookie?

if(isset($_COOKIE[$cookie]))
.
.
.
setcookie(Mysite.$id, Voted, $month);

 

and finally

if ($rating['votes'] == 0){
   $current = 0;
} else {
   $current = $ratings[total] / $ratings[votes]; 
} 
Echo "Rated " . round($current, 1). " from $votes people | <b>Rate This Display</b>: "; 

 

Check both operands of the division :)

and enclose the index field in single quotes, otherwise they evaluate as false (0);

if (!$rating['votes'] || !$rating['total']){
   $current = 0;
  echo "Unrated";
} else {
   $current = $ratings['total'] / $ratings['votes']; 
   echo "Rated " . round($current, 1). " from $votes people";
} 
echo " | <b>Rate This Display</b>: "; 

 

Hope that helps

Link to comment
Share on other sites

[code]<?php

// MAKE CONNECTION
   include ('db_connect.php');

if (isset($_GET['id'])) {
  $id = mysql_real_escape_string($_GET['id']);
  $sql = "SELECT * FROM users WHERE id = $id;";
  if ($result = mysql_query($sql)) {
    if (mysql_fetch_assoc($result)) {
    $votes = $row['votes'];
    $total = $row['total'];
    } else {
      die("No user found");
    }
  } else {
    die(mysql_error());
  }
}

//We only run this code if the user has just clicked a voting link
if (isset($_GET['mode'] && $_GET['mode']=="vote")
{

//If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id";
if(isset($_COOKIE[$cookie]))
{
Echo "Sorry You have already ranked that site <p>";
}

//Otherwise, we set a cooking telling us they have now voted
else
{
$month = 2592000 + time();
setcookie(Mysite.$id, Voted, $month); 

//Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
mysql_query ("UPDATE users SET total = total+$voted, votes = votes+1 WHERE id = $id");
Echo "Your vote has been cast <p>";
}
} 

//This calculates the sites ranking and then outputs it - rounded to 1 decimal
if (!$rating['votes'] || !$rating['total']){
   $current = 0;
  echo "Unrated";
} else {
   $current = $ratings['total'] / $ratings['votes']; 
   echo "Rated " . round($current, 1). " from $votes people";
} 
echo " | <b>Rate This Display</b>: "; 

//This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item

 

Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$ratings[id].">1</a> | ";

Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=2&id=".$ratings[id].">2</a> | ";

Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=3&id=".$ratings[id].">3</a> | ";

Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=4&id=".$ratings[id].">4</a> | ";

Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=5&id=".$ratings[id].">5</a><p>";

 

?>[/code]

 

Link to comment
Share on other sites

OK and?

 

This

 

if (isset($_GET['mode'] && $_GET['mode']=="vote"));

 

and this

 

if (isset($_GET['mode'] && $_GET['mode']=="vote"))

 

Still Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ',' or ')' in /home/mesquit1/public_html/local/display.php on line 286

 

Link to comment
Share on other sites

Ok the id isn't being assigned to the link mouse over and you will see it is not there. You click on one of the links to vote and you get

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

 

 

Link to comment
Share on other sites

It's time to post the current version of code you're using. Much better than expecting us to troll through all the changes you may or may not have made based on this thread.

 

If id is not being assigned, it's because it's not being abstracted properly or you have mis-named variables. Again, let's not guess, let's see what you have.

Link to comment
Share on other sites

<?php

// MAKE CONNECTION
   include ('db_connect.php');

if (isset($_GET['id'])) {
  $id = mysql_real_escape_string($_GET['id']);
  $sql = "SELECT * FROM users WHERE id = $id;";
  if ($result = mysql_query($sql)) {
    if (mysql_fetch_assoc($result)) {
    $votes = $row['votes'];
    $total = $row['total'];
    } else {
      die("No user found");
    }
  } else {
    die(mysql_error());
  }
}

//We only run this code if the user has just clicked a voting link
if (isset($_GET['mode']) && $_GET['mode']=="vote")
{

//If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id";
if(isset($_COOKIE[$cookie]))
{
Echo "Sorry You have already ranked that site <p>";
}

//Otherwise, we set a cooking telling us they have now voted
else
{
$month = 2592000 + time();
setcookie(Mysite.$id, Voted, $month); 

//Then we update the voting information by adding 1 to the total votes and adding their vote (1,2,3,etc) to the total rating
mysql_query ("UPDATE users SET total = total+$voted, votes = votes+1 WHERE id = $id");
Echo "Your vote has been cast <p>";
}
} 

//This calculates the sites ranking and then outputs it - rounded to 1 decimal
if (!$rating['votes'] || !$rating['total']){
   $current = 0;
  echo "Unrated";
} else {
   $current = $ratings['total'] / $ratings['votes']; 
   echo "Rated " . round($current, 1). " from $votes people";
} 
echo " | <b>Rate This Display</b>: "; 

//This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item

Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$ratings[id].">1</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=2&id=".$ratings[id].">2</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=3&id=".$ratings[id].">3</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=4&id=".$ratings[id].">4</a> | ";
Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=5&id=".$ratings[id].">5</a><p>";

?>

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.