twilitegxa Posted August 17, 2009 Share Posted August 17, 2009 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 https://forums.phpfreaks.com/topic/170589-solved-if-statement-display-nothing-if-field-is-blank-or-zero/ Share on other sites More sharing options...
oni-kun Posted August 17, 2009 Share Posted August 17, 2009 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 https://forums.phpfreaks.com/topic/170589-solved-if-statement-display-nothing-if-field-is-blank-or-zero/#findComment-899787 Share on other sites More sharing options...
twilitegxa Posted August 17, 2009 Author Share Posted August 17, 2009 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. :-( Link to comment https://forums.phpfreaks.com/topic/170589-solved-if-statement-display-nothing-if-field-is-blank-or-zero/#findComment-899790 Share on other sites More sharing options...
oni-kun Posted August 17, 2009 Share Posted August 17, 2009 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 https://forums.phpfreaks.com/topic/170589-solved-if-statement-display-nothing-if-field-is-blank-or-zero/#findComment-899794 Share on other sites More sharing options...
twilitegxa Posted August 17, 2009 Author Share Posted August 17, 2009 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 https://forums.phpfreaks.com/topic/170589-solved-if-statement-display-nothing-if-field-is-blank-or-zero/#findComment-899797 Share on other sites More sharing options...
oni-kun Posted August 17, 2009 Share Posted August 17, 2009 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 https://forums.phpfreaks.com/topic/170589-solved-if-statement-display-nothing-if-field-is-blank-or-zero/#findComment-899798 Share on other sites More sharing options...
twilitegxa Posted August 17, 2009 Author Share Posted August 17, 2009 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 https://forums.phpfreaks.com/topic/170589-solved-if-statement-display-nothing-if-field-is-blank-or-zero/#findComment-899801 Share on other sites More sharing options...
oni-kun Posted August 17, 2009 Share Posted August 17, 2009 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 https://forums.phpfreaks.com/topic/170589-solved-if-statement-display-nothing-if-field-is-blank-or-zero/#findComment-899805 Share on other sites More sharing options...
twilitegxa Posted August 17, 2009 Author Share Posted August 17, 2009 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 https://forums.phpfreaks.com/topic/170589-solved-if-statement-display-nothing-if-field-is-blank-or-zero/#findComment-899808 Share on other sites More sharing options...
oni-kun Posted August 17, 2009 Share Posted August 17, 2009 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 https://forums.phpfreaks.com/topic/170589-solved-if-statement-display-nothing-if-field-is-blank-or-zero/#findComment-899809 Share on other sites More sharing options...
oni-kun Posted August 17, 2009 Share Posted August 17, 2009 OMG, I said == "" and not ="" ....... fixed up there..I'm so tired..Rofl. Link to comment https://forums.phpfreaks.com/topic/170589-solved-if-statement-display-nothing-if-field-is-blank-or-zero/#findComment-899810 Share on other sites More sharing options...
twilitegxa Posted August 17, 2009 Author Share Posted August 17, 2009 Thank you so much!!!!! Link to comment https://forums.phpfreaks.com/topic/170589-solved-if-statement-display-nothing-if-field-is-blank-or-zero/#findComment-899814 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.