Jump to content

[SOLVED] If statement - display nothing if field is blank or zero


twilitegxa

Recommended Posts

In the following statement, how do I make the if statement echo nothing if the field is null or has a value of 0 or display the field's value if there is one? I currently have:

 

//gather derived values; fix where statement
$get_derived = "select * from derived_values where identity = 'Sailor Moon'";

$get_derived_res = mysql_query($get_derived, $conn) or die(mysql_error());

while ($derived_info = mysql_fetch_array($get_derived_res)) {
$health = $derived_info['health'];
$energy = $derived_info['energy'];
$acv1 = $derived_info['acv1'];
$acv2 = $derived_info['acv2'];
$dcv1 = $derived_info['dcv1'];
$dcv2 = $derived_info['dcv2'];
$total_cp = $derived_info['total_cp'];

if ($acv2 == '0') {
echo '';
} else {
echo $acv2;
}

$display_block .= "
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td><b>Health Points</b></td>
<td>$health</td>
<td> </td>
</tr>
<tr>
<td><b>Energy Points</b></td>
<td>$energy</td>
<td> </td>
</tr>
<tr>
<td><b>Attack Combat Value</b></td>
<td>$acv1</td>
<td>$acv2</td>
</tr>
<tr>
<td><b>Defense Combat Value</b></td>
<td>$dcv1</td>
<td>$dcv2</td>
</tr>
<tr>
<td><b>Total Character Points</b></td>
<td>$total_cp</td>
<td> </td>
</tr>
</table><br>";

}

 

But the zeros are being output and what I want to be output is nothing is the field is blank or has a zero, or the value if there is one (as long as it's not zero). What have I done wrong?

Your code:

if ($acv2 == '0') {
   echo '';
} else {
   echo $acv2;
}

That will quite simply echo '' before the tables are even written! You may want to check if it is null with isset() or use a global scope:

if ($acv2 == false || $acv2 == null {
   $GLOBALS['acv2'] == "";
}

That'll check if $acv2 is 0 or null, and if it is it'll REWRITE $acv2 as nothing, so in the table it will not display a 0.

 

Do I just put it in place of the if statement I have now? Because I tried that and the zeros are still being output. :-( I'm not too good at these if statements. :-(

 

This should work..

  $health = $derived_info['health'];
   $energy = $derived_info['energy'];
   $acv1 = $derived_info['acv1'];
   $acv2 = $derived_info['acv2'];
   $dcv1 = $derived_info['dcv1'];
   $dcv2 = $derived_info['dcv2'];
   $total_cp = $derived_info['total_cp'];

foreach( $derived_info as $key => $value){
if ($value == 0 || $value == null || !isset($value) {
              $GLOBALS['derived_info[$key]'] == "";
        }
}

 

 

[/code]

 

 

I tried that, and it said:

 

Parse error: parse error on line 224

 

So I added another ) because it seemed to be missing one:

 

foreach( $derived_info as $key => $value){



if ($value == 0 || $value == null || !isset($value)) {
              $GLOBALS['derived_info[$key]'] == "";
        }
}

 

But the zero's are still there AND I guess this error message:

 

Notice: Undefined index: derived_info[$key] on line 225

 

I don't understand the problem. I thought it was defined here:

 

foreach( $derived_info as $key => $value){

foreach( $derived_info as $key => $value){
if ($value == '0' || $value == null || !isset($value)) {
              $GLOBALS['derived_info['.$key.']'] == "";
        }
}

Blah, didn't know php was so picky about those things..

Okay, so now the zeros are showing AND these errors:

 

 

Notice: Undefined index: derived_info[1] in C:\wamp\www\showprofile_scouts.php on line 222

 

Notice: Undefined index: derived_info[identity] in C:\wamp\www\showprofile_scouts.php on line 222

 

Notice: Undefined index: derived_info[5] in C:\wamp\www\showprofile_scouts.php on line 222

 

Notice: Undefined index: derived_info[acv2] in C:\wamp\www\showprofile_scouts.php on line 222

 

Notice: Undefined index: derived_info[7] in C:\wamp\www\showprofile_scouts.php on line 222

 

 

Which make no sense to me. Did it forget my $derived_info var?

Notice: Undefined index: derived_info[dcv2] in C:\wamp\www\showprofile_scouts.php on line 222

Wow, I guess this is just to show if it looks easy it won't work, I hadn't a clue PHP wouldn't support something structured like that.

if($health == 0 || $health == '0' || $health == null){$GLOBALS['health'] = "";}
if($energy == 0 || $energy== '0' || $energy == null){$GLOBALS['energy'] = "";}
if($acv1 == 0 || $acv1 == '0' || $acv1 == null){$GLOBALS['acv1'] = "";}
if($acv2 == 0 || $acv2 == '0' || $acv2 == null){$GLOBALS['acv2'] = "";}
if($dcv1 == 0 || $dcv1 == '0' || $dcv1 == null){$GLOBALS['dcv1'] = "";}
if($dcv2 == 0 || $dcv2 == '0' || $dcv2 == null){$GLOBALS['dcv2'] = "";}
if($total_cp == 0 || $total_cp == '0' || $total_cp== null){$GLOBALS['total_cp'] = "";}

Bleh.

Okay, i don't know what I'm doing wrong, but it' still showing the zeros! Could the problem be in how I structured my tables? I have those fields set to not null and the default is set to none. Is there anything else I could be doing wrong?

Okay, i don't know what I'm doing wrong, but it' still showing the zeros! Could the problem be in how I structured my tables? I have those fields set to not null and the default is set to none. Is there anything else I could be doing wrong?

 

Just try my code above I edited.. it should work .. it won't display a 0 since the code implicity implies it to not display a 0.. I don't think it's in your tables..

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.