jwwceo Posted January 3, 2008 Share Posted January 3, 2008 $rating=($rs['up']/($rs['up']+$rs['down'])); Is there something wrong with this expression?? I'm just trying to get a simple ratio of up votes ( from DB) to total votes. I get only 1's or 0's as the result. best, James Quote Link to comment https://forums.phpfreaks.com/topic/84328-solved-divide-by-functions/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 3, 2008 Share Posted January 3, 2008 If all the values are integers or strings containing integer values, the math is being done using integer math. When the result is 1 you get 1. When the result is < 1 you get zero. As long as one of the values is a float, the math will be done using floating point math. This should work, but don't quote me on that - $rating=((float)$rs['up']/($rs['up']+$rs['down'])); Edit: This should work as well - $rating= 0.0 + ($rs['up']/($rs['up']+$rs['down'])); Quote Link to comment https://forums.phpfreaks.com/topic/84328-solved-divide-by-functions/#findComment-429485 Share on other sites More sharing options...
Zane Posted January 3, 2008 Share Posted January 3, 2008 if you're looking for a way to put it into a visual fraction of some sort like $rating = UP/TOTAL you can just go $rating= $rs['up'] . "/" . ($rs['up']+$rs['down']); but as long as you have it the previous way.....it'll just go ahead and do the math I get only 1's or 0's as the result. Surely it doesn't happen every time, for debugging purposes I recommend you click the "up button" and "down button" buttons you have a few times or a hundred...just to see if it's all working EDIT: nevermind sorry you're getting that because it's always going to be a fraction.... Quote Link to comment https://forums.phpfreaks.com/topic/84328-solved-divide-by-functions/#findComment-429493 Share on other sites More sharing options...
jwwceo Posted January 3, 2008 Author Share Posted January 3, 2008 The problem wasn't the expression at all. The data type in the DB was set to integer. And it needed to be float. Thanks guys for the help!! James Quote Link to comment https://forums.phpfreaks.com/topic/84328-solved-divide-by-functions/#findComment-429497 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.