Jump to content


Photo

0 acts as empty, IF statement problems


  • Please log in to reply
12 replies to this topic

#1 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 04 October 2006 - 11:01 AM

I've done a code to decide who has won/lost/drawn or not played depending on the database entries. The problem I have is if the score is "0 - 10" for example, it displays it as not played, how would I solve this problem so if no points are scored it still figures out who won/lost?

Heres my code:
(York is the team who it is for, so when it says "Won" it means York Won)
if(empty($scorehome)) {
		?><span class="notplayed">&nbsp;</span><?
		}
		else {
		if($scorehome == $scoreaway) {
		?><span class="draw">Draw</span><?
		}
		else
		{
		
		if($home == York) {
		if($scorehome > $scoreaway) {
		$home?> <span class="win">Won</span> <?
		}
		else {
		$away?> <span class="loss">Loss</span> <?
		}
		}
		
		else {
		if($away == York) {
		if($scorehome < $scoreaway) {
		$home?> <span class="win">Won</span> <?
		}
		else {
		$away?> <span class="loss">Loss</span> <?
		}
		}
		}
		}
		}

Thanks in advance. :)
~ Mutley.

#2 pedrobcabral

pedrobcabral
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 04 October 2006 - 11:10 AM

<?php
$var = 0;
if ((!$var) && ($var != "0")) echo "It is empty."; else echo "not empty"; 
?>

In your code your variable it is set to 0, that means the false, the empty.

Try to deal with the "0" as a string, and not a number 0.
String will make the var full, number will set it to empty.

#3 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 04 October 2006 - 11:24 AM

I see, but how would I implement that into my code?
~ Mutley.

#4 pedrobcabral

pedrobcabral
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 04 October 2006 - 11:36 AM

Try to put this at the begining if ((!$scorehome) && ($scorehome != "0")) instead of your "empty($score) clause.

#5 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 04 October 2006 - 12:02 PM

Didn't work. :(
~ Mutley.

#6 eric1235711

eric1235711
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts
  • LocationSão Paulo - Brazil

Posted 04 October 2006 - 02:51 PM

what about using is_null instead of empty ?
Programming is like building your own universe...
But let me ask you something:
Your world... It´s cool? Or it sucks?

#7 Hi I Am Timbo

Hi I Am Timbo
  • Members
  • PipPipPip
  • Advanced Member
  • 109 posts

Posted 04 October 2006 - 03:25 PM

<?php
$var = 0;
if ((!$var) && ($var != "0")) echo "It is empty."; else echo "not empty"; 
?>

In your code your variable it is set to 0, that means the false, the empty.

Try to deal with the "0" as a string, and not a number 0.
String will make the var full, number will set it to empty.


I would use !== instead of != because in the latter, a false value would be considered as 0.

#8 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 04 October 2006 - 03:43 PM


<?php

$value = "0";
$arr = array();
$arr["no 1"] = $value;
$arr["no 2"] = "$value";
$arr["no 3"] = (float)$value;
$arr["no 4"] = (string)$value;
$arr["no 5"] = (bool)$value;
$arr["no 6"] = (object)$value;
$arr["no 7"] = (int)$value;

foreach($arr as $key=>$value)
{
  if(empty($value)) echo $key." is empty | ";
  else echo $key." is not empty | ";
}

?>


echo -->

no 1 is empty | no 2 is empty | no 3 is empty | no 4 is empty | no 5 is empty | no 6 is not empty | no 7 is empty |



#9 ponsho

ponsho
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 04 October 2006 - 03:56 PM

Maybe you can use this

if(!is_numeric($value)){
?><span class="notplayed">&nbsp;</span><?
}
else {
if($scorehome == $scoreaway) {
?><span class="draw">Draw</span><?
}
else
{
if($home == York) {
if($scorehome > $scoreaway) {
$home?> <span class="win">Won</span> <?
}
else {
$away?> <span class="loss">Loss</span> <?
}
}
		
else {
if($away == York) {
if($scorehome < $scoreaway) {
$home?> <span class="win">Won</span> <?
}
else {
$away?> <span class="loss">Loss</span> <?
}
}
}
}
}


#10 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 04 October 2006 - 06:08 PM

Use intval

#11 Mutley

Mutley
  • Members
  • PipPipPip
  • Advanced Member
  • 765 posts

Posted 06 October 2006 - 05:02 PM

Maybe you can use this

if(!is_numeric($value)){
?><span class="notplayed">&nbsp;</span><?
}
else {
if($scorehome == $scoreaway) {
?><span class="draw">Draw</span><?
}
else
{
if($home == York) {
if($scorehome > $scoreaway) {
$home?> <span class="win">Won</span> <?
}
else {
$away?> <span class="loss">Loss</span> <?
}
}
		
else {
if($away == York) {
if($scorehome < $scoreaway) {
$home?> <span class="win">Won</span> <?
}
else {
$away?> <span class="loss">Loss</span> <?
}
}
}
}
}


Didn't work.

@ Daniel0, how?
~ Mutley.

#12 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 06 October 2006 - 07:43 PM

This will detect empty, but the number 0 will pass as not empty
<?php

if(empty($scorehome) && !preg_match("/^[0]+$/", $scorehome))
{
echo "value is empty but NOT number 0";
}

?>


#13 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 06 October 2006 - 11:38 PM

Here's another way if you don't want to deal with regular expresions:
<?php if (empty($scorehome) && strlen($scorehome) == 0) echo 'value is really empty'; ?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users