Pudgemeister Posted July 30, 2006 Share Posted July 30, 2006 hi all.just a little if/else code i cant seem to get right.wasnt sure if this was the right forum-but i need help to find out what is wrong with it.pretty simple to reilise what im trying to work out:[code]if ($ratings[votes]===0)echo "0";elseif ($current = $ratings[total] / $ratings[votes] === 0<Echo "Current Rating: " .($current) . "<br>";[/code]simply-if i dont have the if else there-when $ratings[votes] == 0, i get an error on my page saying division by zero-this is understandable.i am just trying to make a code where its like if $ratings is 0, echo Current Rating = 0 and dont do the division or echo current rating: $current. <br> etc.what am i doing wrong? (prob alot of stuff but u know)Thanx For ReadingPudgemeister Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted July 30, 2006 Share Posted July 30, 2006 You need to throw in a check for 0 before the division, and make the division an else/elseif. Quote Link to comment Share on other sites More sharing options...
Ph0enix Posted July 30, 2006 Share Posted July 30, 2006 Try doing this..[code]if ($ratings[votes]===0) {echo "0";}else {if ($current = $ratings[total] / $ratings[votes] === 0) {echo "Current Rating: " .($current) . "<br>";} }[/code] Quote Link to comment Share on other sites More sharing options...
king arthur Posted July 30, 2006 Share Posted July 30, 2006 What is this line meant to do?[code]if ($current = $ratings[total] / $ratings[votes] === 0<[/code]As it is it will set $current to true or false, depending on whether the outcome of $ratings[total] / $ratings[votes] is identical to 0 or not, and only execute the following echo statement if the outcome was true. Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 30, 2006 Share Posted July 30, 2006 [quote]if ($current [b]=[/b] $ratings[total] [b]/[/b] $ratings[votes] === 0)[/quote]Are you trying to mean[code]if ($current == $ratings[total] || $ratings[votes] === 0)[/code]In PHP[b]==[/b] means equal to[b]||[/b] means or[b]&&[/b] means and Quote Link to comment Share on other sites More sharing options...
AndyB Posted July 30, 2006 Share Posted July 30, 2006 [code]echo "Current Rating: ";if ($ratings[votes]===0) { echo "0";} else { $current = $ratings[total] / $ratings[votes]; echo $current;}echo "<br/>";[/code] Quote Link to comment Share on other sites More sharing options...
Pudgemeister Posted July 30, 2006 Author Share Posted July 30, 2006 thanx for the help there guys.though i am still getting the same error-u have tought me alot through the help you have given so thanx lol.at the moment when i try loading the page with andys solution, the page displays this:Name: First itemCurrent Rating: 4.5Rank Me: Vote 1 | Vote 2 | Vote 3 | Vote 4 | Vote 5Name: Second itemCurrent Rating: 3.75Rank Me: Vote 1 | Vote 2 | Vote 3 | Vote 4 | Vote 5Name: Third thingCurrent Rating: 3.57142857143Rank Me: Vote 1 | Vote 2 | Vote 3 | Vote 4 | Vote 5Name: The ForthCurrent Rating: 4Rank Me: Vote 1 | Vote 2 | Vote 3 | Vote 4 | Vote 5Name: Fifth ThingCurrent Rating: Warning: Division by zero in /home/pudgesuk/public_html/clips_n_pics/vote.php on line 42Rank Me: Vote 1 | Vote 2 | Vote 3 | Vote 4 | Vote 5which is exactly what ig ot in the first place hehehehe.so basically the if else code cant b right as the number is obviously 0 but the if else code isnt identifying that:[code]echo "Current Rating: ";if ($ratings[votes]===0 || $ratings[total]===0) { echo "0";} else { $current = $ratings[total] / $ratings[votes]; echo $current;}echo "<br/>";[/code]sorry bout this-its just im so close to having what i want on my site that its getting frustrating lol.cheers for reading.Pudgemeister Quote Link to comment Share on other sites More sharing options...
king arthur Posted July 30, 2006 Share Posted July 30, 2006 Where is $ratings[votes] assigned? It may not be getting set, if not then the line "if ($ratings[votes]===0 || $ratings[total]===0)" won't catch it and you will still get a division by zero. Make it "if ($ratings[votes]===0 || $ratings[total]===0 || (!isset($ratings[votes])))" Quote Link to comment Share on other sites More sharing options...
Pudgemeister Posted July 30, 2006 Author Share Posted July 30, 2006 ok that has sorted the if else prob.but now there are a few (im guessing) minor issues with this piece of code now.99.99% of it i got from a site.[code]<?php // Connects to your Database include ('dbinfo.inc.php');//We only run this code if the user has just clicked a voting linkif ( $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 vote 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 vote") or die(mysql_error()); //Now we loop through all the data while($ratings = mysql_fetch_array( $data )) { //This outputs the sites name Echo "Name: " .$ratings['name']."<br>"; //This calculates the sites ranking and then outputs it - rounded to 1 decimal echo "Current Rating: ";if ($ratings[votes]===0 || $ratings[total]===0 || (!isset($ratings[votes]))) { echo 0;} elseif ($ratings[votes]>0 || $ratings[total]>0 || (!isset($ratings[votes]))) { $current = $ratings[total] / $ratings[votes]; echo $current;}echo "<br/>";//This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item Echo "Rank Me: "; Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=1&id=".$ratings[id].">Vote 1</a> | "; Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=2&id=".$ratings[id].">Vote 2</a> | "; Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=3&id=".$ratings[id].">Vote 3</a> | "; Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=4&id=".$ratings[id].">Vote 4</a> | "; Echo "<a href=".$_SERVER['PHP_SELF']."?mode=vote&voted=5&id=".$ratings[id].">Vote 5</a><p>"; } ?>[/code]firstly-the cookie revote thing (where it stores a cookie in the persons browser saying that the person has voted if they try to vote again) isn't working.secondly-the problem tat we just sorted is good and all-but it still doesnt show "current rating: 0" when its a zero rating. Just "current rating: " which is much better than having the error there but if we could get the zero to appear-it would b helpful.cheers for readingPudgemeister Quote Link to comment Share on other sites More sharing options...
Pudgemeister Posted July 31, 2006 Author Share Posted July 31, 2006 please sum1 reply i need to get thsi code finished Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted July 31, 2006 Share Posted July 31, 2006 Instead of using the triple equal sign, try using the double equal sign in your IF statements.Ken Quote Link to comment Share on other sites More sharing options...
king arthur Posted July 31, 2006 Share Posted July 31, 2006 I would say for starters, this line[code]//If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id"; [/code]was actually meant to be two lines:[code]//If the user has already voted on the particular thing, we do not allow them to vote again$cookie = "Mysite$id"; [/code]as, at the moment, the variable $cookie doesn't appear to be defined in your script. Also, what should "Mysite$id" be? I think you need to change this to a string value that identifies your site. Quote Link to comment Share on other sites More sharing options...
akrytus Posted July 31, 2006 Share Posted July 31, 2006 I didnt even know you could use triple equals ===, why can you, and why would you? Quote Link to comment Share on other sites More sharing options...
Pudgemeister Posted July 31, 2006 Author Share Posted July 31, 2006 omg i cant believe i didnt see that!thanc for that-it sorted the prob.as far as i can tell after testing it-everything is working!:Dthank u all so much.what im going to do now is try and use this to make a picture and video rating system throughout my site.cheers allPudgemeister Quote Link to comment Share on other sites More sharing options...
king arthur Posted July 31, 2006 Share Posted July 31, 2006 Triple equals means "is identical to", not just "is equal to". For example if you have a numeric variable returned from a function, and "false" means no value returned, the problem is that zero could be interpreted either as the value zero, or false. But by using if($value === false) you can test the variable correctly. Quote Link to comment Share on other sites More sharing options...
akrytus Posted July 31, 2006 Share Posted July 31, 2006 Really? Thought that was the difference between single and double. Shows how much I use it! Quote Link to comment Share on other sites More sharing options...
hackerkts Posted August 1, 2006 Share Posted August 1, 2006 You use [b]=[/b] when you passing the string value, and if you want to check something euqal to use [b]==[/b].I seldom use [b]===[/b] in my scripts. xD Quote Link to comment 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.