Dada78 Posted February 8, 2008 Share Posted February 8, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/ Share on other sites More sharing options...
pocobueno1388 Posted February 8, 2008 Share Posted February 8, 2008 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']? Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-461801 Share on other sites More sharing options...
Dada78 Posted February 8, 2008 Author Share Posted February 8, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-461817 Share on other sites More sharing options...
pocobueno1388 Posted February 8, 2008 Share Posted February 8, 2008 Try this: <?php if ($rating['votes'] == 0){ $current = 0; } else { $current = $ratings[total] / $ratings[votes]; } Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-461819 Share on other sites More sharing options...
Dada78 Posted February 8, 2008 Author Share Posted February 8, 2008 That fixed the error issue thank you. Can you can make a test vote so I can see if it is working correctly. http://www.mesquitechristmas.com/local/display.php?id=81 Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-461824 Share on other sites More sharing options...
pocobueno1388 Posted February 8, 2008 Share Posted February 8, 2008 I rated it a 4 and the output was "Rated 0 from 1 people" Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-461833 Share on other sites More sharing options...
Dada78 Posted February 8, 2008 Author Share Posted February 8, 2008 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-461835 Share on other sites More sharing options...
Dada78 Posted February 8, 2008 Author Share Posted February 8, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-461920 Share on other sites More sharing options...
pocobueno1388 Posted February 8, 2008 Share Posted February 8, 2008 Echo out the variables $ratings['total'] and $ratings['votes'] to make sure they are coming out as expected. Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-461929 Share on other sites More sharing options...
Dada78 Posted February 8, 2008 Author Share Posted February 8, 2008 I get nothing. The 1 you see is coming from the DB though. Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-461939 Share on other sites More sharing options...
Dada78 Posted February 8, 2008 Author Share Posted February 8, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-462003 Share on other sites More sharing options...
laffin Posted February 8, 2008 Share Posted February 8, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-462040 Share on other sites More sharing options...
Dada78 Posted February 8, 2008 Author Share Posted February 8, 2008 You have completely lost me. But I am selecting the row here $sql = "SELECT * FROM users WHERE id = $id;"; Also the tutorial I am going by is here. http://php.about.com/od/finishedphp1/ss/rating_script.htm Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-462066 Share on other sites More sharing options...
Dada78 Posted February 8, 2008 Author Share Posted February 8, 2008 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-462184 Share on other sites More sharing options...
laffin Posted February 8, 2008 Share Posted February 8, 2008 [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] Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-462192 Share on other sites More sharing options...
Dada78 Posted February 8, 2008 Author Share Posted February 8, 2008 Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ',' or ')' in /home/mesquit1/public_html/local/display.php on line 286 which is this line: if (isset($_GET['mode'] && $_GET['mode']=="vote") Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-462230 Share on other sites More sharing options...
AndyB Posted February 9, 2008 Share Posted February 9, 2008 ... look closely. There are two of these ( and only one of these ) Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-462236 Share on other sites More sharing options...
laffin Posted February 9, 2008 Share Posted February 9, 2008 oopsie happens if (isset($_GET['mode']) && $_GET['mode']=="vote") Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-462243 Share on other sites More sharing options...
Dada78 Posted February 9, 2008 Author Share Posted February 9, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-462245 Share on other sites More sharing options...
Dada78 Posted February 9, 2008 Author Share Posted February 9, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-462247 Share on other sites More sharing options...
Dada78 Posted February 9, 2008 Author Share Posted February 9, 2008 Anyone know what might be causing this? -Thanks Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-462524 Share on other sites More sharing options...
AndyB Posted February 9, 2008 Share Posted February 9, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-462601 Share on other sites More sharing options...
Dada78 Posted February 9, 2008 Author Share Posted February 9, 2008 <?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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-462611 Share on other sites More sharing options...
AndyB Posted February 9, 2008 Share Posted February 9, 2008 I see nothing in your code that would generate values for $ratings[id] Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-462616 Share on other sites More sharing options...
Dada78 Posted February 9, 2008 Author Share Posted February 9, 2008 The tutorial I am going by is here. http://php.about.com/od/finishedphp1/ss/rating_script.htm If I change $ratings[id] to $id it shows the id but doesn't update the DB with the vote. Quote Link to comment https://forums.phpfreaks.com/topic/90065-rating-script-help/#findComment-462631 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.