PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
blacksnday
Forum Contributor
Posts: 252 Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(
Post
by blacksnday » Mon Nov 21, 2005 1:16 am
Im trying to create a rating system, and to tally the
current rating I am dividing total score by total number of votes.
If there is 0 votes (which in turn means 0 score)
it throws error of course, since dividing by zero doesnt make since.
How can I correct the below code so that if there is 0 it wont throw errors
blacksnday
Forum Contributor
Posts: 252 Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(
Post
by blacksnday » Mon Nov 21, 2005 1:21 am
guess i could use something like
m3mn0n
PHP Evangelist
Posts: 3548 Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada
Post
by m3mn0n » Mon Nov 21, 2005 2:05 am
Yes. Use an if statement. Do not divide if there is nothing to divide by.
AKA Panama Jack
Forum Regular
Posts: 878 Joined: Mon Nov 14, 2005 4:21 pm
Post
by AKA Panama Jack » Mon Nov 21, 2005 3:48 am
Or...
Code: Select all
$rating = $tvotes / max(1, $nvotes);
This way it will always be 1 or higher for the divisor.
The MIN and MAX commands are little used in PHP and they can be used to replace alot of IF/ELSE statements.
Jenk
DevNet Master
Posts: 3587 Joined: Mon Sep 19, 2005 6:24 am
Location: London
Post
by Jenk » Mon Nov 21, 2005 4:00 am
That will give the vote a score equal to that of the total votes.. in other words, it'll give it 100%.
The if ($votes == 0) is a better solution.
sweatje
Forum Contributor
Posts: 277 Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA
Post
by sweatje » Mon Nov 21, 2005 6:56 am
Code: Select all
$rating = ($nvote) ? $tvotes / $nvote : 0;
blacksnday
Forum Contributor
Posts: 252 Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(
Post
by blacksnday » Mon Nov 21, 2005 7:56 am
sweatje wrote: Code: Select all
$rating = ($nvote) ? $tvotes / $nvote : 0;
thanks a ton! that works without needing IF statements.
I really needed solution without needing IF statements because
how it is currently being done, coding was doubled....
damn.. one day my mind will soak up classes and OOP
and all this extra coding will cease lol
Jenk
DevNet Master
Posts: 3587 Joined: Mon Sep 19, 2005 6:24 am
Location: London
Post
by Jenk » Mon Nov 21, 2005 8:28 am
That is effectively and IF statement, just in a short 'version' called a ternary operator
sweatje
Forum Contributor
Posts: 277 Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA
Post
by sweatje » Mon Nov 21, 2005 8:34 am
blacksnday wrote: sweatje wrote: Code: Select all
$rating = ($nvote) ? $tvotes / $nvote : 0;
thanks a ton! that works without needing IF statements.
I really needed solution without needing IF statements because
how it is currently being done, coding was doubled....
damn.. one day my mind will soak up classes and OOP
and all this extra coding will cease lol
No OOP is site there:
http://www.php.net/manual/en/language.o ... on.ternary