Jump to content

IF statment woes


86Stang

Recommended Posts

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;

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@ 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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.