86Stang Posted August 21, 2008 Share Posted August 21, 2008 I'm trying to show a win/loss percentage for a sport with a few extra requirements given to me from the customer. I would think that the following code would work but I'm always getting a ".500" result for all records that this processes in the loop. Any ideas on what I could be doing wrong? <? // if no games played yet, give the team a .500 percentage if ($Wins == 0 && $Losses == 0) { $Perc = .500; } // if any wins AND no losses, give the team a 1.00 percentage elseif ($Wins > 0 && $Losses == 0) { $Perc = 1.00; } // if any losses AND no wins, give the team a 0.00 percentage elseif ($Wins == 0 && $Losses > 0) { $Perc = 0.00; } // otherwise figure the win to loss percentage else { $Perc = $Wins / ($Wins + $Losses); } // show results echo $Perc; ?> Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/ Share on other sites More sharing options...
mbeals Posted August 21, 2008 Share Posted August 21, 2008 first off, do you realize that your formula works for all cases except for wins & losses = 0? <?php if(!$Wins && !$Losses){ $Perc = 0.5; }else{ $Perc = $Wins /(float)($Wins + $Losses); } echo $Perc; ?> but.... your original code should work. You might want to check $Wins and $Losses just before the IF and make sure they are what you think they are Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622357 Share on other sites More sharing options...
akitchin Posted August 21, 2008 Share Posted August 21, 2008 i would assume that it's because you're assigning your variables incorrectly. in what context are you using this block of code? Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622358 Share on other sites More sharing options...
Maq Posted August 21, 2008 Share Posted August 21, 2008 Can you show the code where you get the values from $Wins and $Losses? Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622366 Share on other sites More sharing options...
86Stang Posted August 21, 2008 Author Share Posted August 21, 2008 @ mbeals - I thought my first IF (if ($Wins == 0 && $Losses == 0)) would handle the case of wins and losses = 0, I could be wrong though - I'm still deep inside my learning curve with this stuff. I tried using your code but I'm getting a "Division by zero" error on all results. By the way, the win/loss records are: W - L 1 - 0 0 - 0 0 - 1 Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622436 Share on other sites More sharing options...
akitchin Posted August 21, 2008 Share Posted August 21, 2008 we still need to see how you're assigning $Wins and $Losses, but in the end the if() you want is: if ($Wins == 0 && $Losses == 0) { $ratio = 0; } else { $ratio = round($Wins / ($Wins + $Losses), 2); } there's no need to cast it as a float, since you'll always be dealing with integers. Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622445 Share on other sites More sharing options...
86Stang Posted August 21, 2008 Author Share Posted August 21, 2008 I'm pulling the data from a content management system specific query function (yeah, fun stuff). Here's the entire thing. Mind you that I'm posting a small bit of the CMS code but all it does is loops from ("{query sql=") to ("{/query}") and executes. I also changed the variable names before to try and make things simpler for everyone to help me out so things might seem a bit different. I know. the query is a beast. It's not mine -- the database is not normalized at all and I'm stuck dealing with what the customer has. {query sql="SELECT `team`, SUM(IF(`result`='Win',1,0)) AS `wins`, SUM(IF(`result`='Loss',1,0)) AS `losses`, FROM ( SELECT `home_team` AS `team`, `home_results` AS `result`, FROM `database` UNION ALL SELECT `away_team` AS `team`, `away_results` AS `result`, FROM `database` ) AS `t` GROUP BY `team` "} <tr> <td width="150px" valign="top"> {team} </td> <td width="50px" valign="top"> <? $Wins = '{wins}'; $Losses = '{losses}'; if ($Wins == 0 && $Losses == 0) { $Perc = .500; } elseif ($Wins > 0 && $Losses == 0) { $Perc = 1.000; } elseif ($Wins == 0 && $Losses > 0) { $Perc = 0; } else { $Perc = $Wins / ($Wins + $Losses); } echo $Perc; ?> <td> <td width="50px" valign="top"> {wins} </td> <td width="50px" valign="top"> {losses} </td> </tr> {/query} Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622497 Share on other sites More sharing options...
akitchin Posted August 21, 2008 Share Posted August 21, 2008 seemingly dumb question, but isn't 1 win and 1 loss 50% anyway? try echoing $Wins and $Losses to ensure they have what they should, and we can narrow it down to whether it's the assignment or the conditional block that's causing the issue. Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622502 Share on other sites More sharing options...
86Stang Posted August 21, 2008 Author Share Posted August 21, 2008 Yeah 1 loss and 1 win would = 50%. Did I hose that up somewhere? Plus the way it was described to me by the customer is that a 0 and 0 record equals a 50% standing so that they remain above the other teams that have loss(es). Beyond that, another major reason for this IF statement was to avoid division by zero errors. I echoed here: $Wins = '{wins}'; $Losses = '{losses}'; echo $Wins . " - " . $Losses . "<br>"; and the results are what they should be: 1 - 0, 0 - 0, 0 - 1 Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622524 Share on other sites More sharing options...
akitchin Posted August 21, 2008 Share Posted August 21, 2008 do you get 0.500 for all three (1-0, 0-0, and 0-1)? if so, it could be an issue with your type, so it might be wise to intval() your $Wins and $Losses or remove the single quotes from around the {wins} and {losses} when assigning. Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622534 Share on other sites More sharing options...
86Stang Posted August 21, 2008 Author Share Posted August 21, 2008 I assume you mean remove the single quotes here: $Wins = '{wins}'; $Losses = '{losses}'; I tried and it kicked out an error "syntax error, unexpected '{' in...". Could you explain where I would put that intval()? Thanks for the help so far!! Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622551 Share on other sites More sharing options...
akitchin Posted August 21, 2008 Share Posted August 21, 2008 i guess leave the single quotes in there. i should have realized the CMS syntax would gunk up the works. if (intval($Wins) == 0 && intval($Losses) == 0) { // stuff! } else { // other stuff! } Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622552 Share on other sites More sharing options...
86Stang Posted August 21, 2008 Author Share Posted August 21, 2008 I tried .. it failed .. I cried. Any other thoughts on this stupid of a hodgepodge of a hybrid crazy-glued code? Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622572 Share on other sites More sharing options...
akitchin Posted August 21, 2008 Share Posted August 21, 2008 could it be that your CMS is screwing up the if-elseif block? check if putting: $i_dont_know = 7; if ($i_dont_know == 0) { $Perc = 'something ridiculous'; } (in place of the original block) makes it output 'something ridiculous'. Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622580 Share on other sites More sharing options...
86Stang Posted August 21, 2008 Author Share Posted August 21, 2008 The output was empty. Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622589 Share on other sites More sharing options...
akitchin Posted August 21, 2008 Share Posted August 21, 2008 peculiar! what exactly is the output by the script (the entire HTML source)? obviously, plug back in your original conditional block. Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622591 Share on other sites More sharing options...
86Stang Posted August 22, 2008 Author Share Posted August 22, 2008 Here's the relevent section of the source that was output: <td width="150px" valign="top"> Team1 </td> <td width="50px" valign="top"> 1 </td> <td width="50px" valign="top"> 0 </td> <td width="50px" valign="top"> </td> totally blank... Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622602 Share on other sites More sharing options...
akitchin Posted August 22, 2008 Share Posted August 22, 2008 and if you change the {wins} to <?php echo $Wins; ?> (as well with the losses)? do the wins and losses totals come up? this is assuming your conditional block is still where it was originally. Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-622604 Share on other sites More sharing options...
86Stang Posted August 22, 2008 Author Share Posted August 22, 2008 Yep, the wins show up. Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-623040 Share on other sites More sharing options...
Maq Posted August 22, 2008 Share Posted August 22, 2008 Do you have a web app like phpMyAdmin to check and see if there are actually values in your table? Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-623058 Share on other sites More sharing options...
akitchin Posted August 22, 2008 Share Posted August 22, 2008 i'm going to take a shot in the dark here and say that the braces are screwing your script up, since the CMS's syntax is interpreting them as commands? try this: if ($Wins == 0 && $Losses == 0) $Perc = 0.500; else $Perc = round($Wins / ($Wins + $Losses), 2); echo $Perc; another option to try might be to use <?php instead of the shorthand <?. this is just good practice anyway, since servers may sometime (if they haven't already) started to default to disabling short tags. Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-623081 Share on other sites More sharing options...
86Stang Posted August 22, 2008 Author Share Posted August 22, 2008 I tried it and same results. I spent some time doing this outside of the CMS and it works like a champ so I'm going to let the customer know that it's just not possible with his CMS. Thank you soooo much akitchin for hanging in there with me!! Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-623107 Share on other sites More sharing options...
akitchin Posted August 22, 2008 Share Posted August 22, 2008 well, before you go back to the client (and i'm surprised i didn't think of this earlier), you might be able to put this into the query: {query sql="SELECT `team`, SUM(IF(`result`='Win',1,0)) AS `wins`, SUM(IF(`result`='Loss',1,0)) AS `losses`, CASE WHEN `wins` = 0 AND `losses` = 0 THEN 0.500 ELSE ROUND(`wins` / (`wins` + `losses`), 2) END AS `percentage` FROM ( SELECT `home_team` AS `team`, `home_results` AS `result`, FROM `database` UNION ALL SELECT `away_team` AS `team`, `away_results` AS `result`, FROM `database` ) AS `t` GROUP BY `team` "} i'm not sure if you can use aggregate functions in the SELECT portion, but it's worth a shot. if you can't get this to work, THEN i'd go to the client and capitulate. Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-623112 Share on other sites More sharing options...
86Stang Posted August 22, 2008 Author Share Posted August 22, 2008 I get an error: "Unknown column 'wins' in 'field list'" Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-623132 Share on other sites More sharing options...
akitchin Posted August 22, 2008 Share Posted August 22, 2008 ah, that's what i was afraid of. alright, well unless a MySQL guru can help you out with the query, i guess the CMS syntax triumphs. perhaps try posting the query into the MySQL forum and see if someone can help you put it into the query - i'm certain someone who trawls that forum will be able to help you. Quote Link to comment https://forums.phpfreaks.com/topic/120755-if-statment-woes/#findComment-623135 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.