Jump to content


Photo

Small if else code


  • Please log in to reply
16 replies to this topic

#1 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 30 July 2006 - 01:00 PM

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:

if ($ratings[votes]===0)

echo "0";
else
if ($current = $ratings[total] / $ratings[votes] === 0<
Echo "Current Rating: " .

($current) . "<br>";

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 Reading

Pudgemeister
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#2 Balmung-San

Balmung-San
  • Members
  • PipPipPip
  • Advanced Member
  • 327 posts

Posted 30 July 2006 - 01:04 PM

You need to throw in a check for 0 before the division, and make the division an else/elseif.
"Paranoia is very useful in this work. ...If your cryptographic system can survive the paranoia model, it has at least a fighting chance of surviving in the real world." - Niels Ferguson & Bruce Schneier

They say there's more then one way to skin a cat.
In programming, there's no wrong way to skin a cat, just more efficent ways of doing so.

#3 Ph0enix

Ph0enix
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 30 July 2006 - 01:23 PM

Try doing this..
if ($ratings[votes]===0) {

echo "0";
}else {
if ($current = $ratings[total] / $ratings[votes] === 0) {
echo "Current Rating: " .
($current) . "<br>";
} 
}

[url=http://www.slimphoenix.com][/url]

#4 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 30 July 2006 - 01:25 PM

What is this line meant to do?
if ($current = $ratings[total] / $ratings[votes] === 0<

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.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#5 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 30 July 2006 - 01:25 PM

if ($current = $ratings[total] / $ratings[votes] === 0)


Are you trying to mean
if ($current == $ratings[total] || $ratings[votes] === 0)

In PHP
== means equal to
|| means or
&& means and

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 30 July 2006 - 01:26 PM

echo "Current Rating: ";
if ($ratings[votes]===0) {
    echo "0";
} else {
    $current = $ratings[total] / $ratings[votes];
    echo $current;
}
echo "<br/>";

Legend has it that reading the manual never killed anyone.
My site

#7 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 30 July 2006 - 02:07 PM

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 item
Current Rating: 4.5
Rank Me: Vote 1 | Vote 2 | Vote 3 | Vote 4 | Vote 5
Name: Second item
Current Rating: 3.75
Rank Me: Vote 1 | Vote 2 | Vote 3 | Vote 4 | Vote 5

Name: Third thing
Current Rating: 3.57142857143
Rank Me: Vote 1 | Vote 2 | Vote 3 | Vote 4 | Vote 5

Name: The Forth
Current Rating: 4
Rank Me: Vote 1 | Vote 2 | Vote 3 | Vote 4 | Vote 5

Name: Fifth Thing
Current Rating:
Warning: Division by zero in /home/pudgesuk/public_html/clips_n_pics/vote.php on line 42

Rank Me: Vote 1 | Vote 2 | Vote 3 | Vote 4 | Vote 5


which 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:
echo "Current Rating: ";
if ($ratings[votes]===0 || $ratings[total]===0) {
    echo "0";
} else {
    $current = $ratings[total] / $ratings[votes];
    echo $current;
}
echo "<br/>";

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
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#8 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 30 July 2006 - 02:50 PM

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])))"
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#9 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 30 July 2006 - 03:11 PM

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.

<?php 
// Connects to your Database 
include ('dbinfo.inc.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 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>"; 
} 
?>


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 reading

Pudgemeister
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#10 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 31 July 2006 - 07:08 PM

please sum1 reply i need to get thsi code finished
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 31 July 2006 - 07:10 PM

Instead of using the triple equal sign, try using the double equal sign in your IF statements.

Ken

#12 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 31 July 2006 - 07:16 PM

I would say for starters, this line
//If the user has already voted on the particular thing, we do not allow them to vote again $cookie = "Mysite$id"; 

was actually meant to be two lines:
//If the user has already voted on the particular thing, we do not allow them to vote again
$cookie = "Mysite$id"; 

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.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#13 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 31 July 2006 - 07:18 PM

I didnt even know you could use triple equals ===, why can you, and why would you?

#14 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 31 July 2006 - 07:20 PM

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!:D

thank 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 all

Pudgemeister
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#15 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 31 July 2006 - 07:22 PM

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.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#16 akrytus

akrytus
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts

Posted 31 July 2006 - 08:26 PM

Really?  Thought that was the difference between single and double.  Shows how much I use it!

#17 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 01 August 2006 - 06:25 AM

You use = when you passing the string value, and if you want to check something euqal to use ==.

I seldom use === in my scripts. xD

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users