#### Archived

This topic is now archived and is closed to further replies.

# Small if else code

## Recommended Posts

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:

echo "0";
else
if (\$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)

Pudgemeister

##### Share on other sites
You need to throw in a check for 0 before the division, and make the division an else/elseif.

##### Share on other sites
Try doing this..
[code]

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

##### Share on other sites
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.

##### Share on other sites
[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

##### Share on other sites
[code]echo "Current Rating: ";
echo "0";
} else {
echo \$current;
}
echo "<br/>";[/code]

##### Share on other sites
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:
[code]
echo "Current Rating: ";
echo "0";
} else {
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.

Pudgemeister

##### Share on other sites
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])))"

##### Share on other sites
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
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";
{
Echo "Sorry You have already ranked that site <p>";
}

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

//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: ";
echo 0;
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.

Pudgemeister

Ken

##### Share on other sites
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
[/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.

##### Share on other sites
I didnt even know you could use triple equals ===, why can you, and why would you?

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
Really?  Thought that was the difference between single and double.  Shows how much I use it!

##### Share on other sites
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