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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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]

 

 

Link to comment
Share on other sites

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){

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

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.