Matt Ridge Posted November 22, 2011 Share Posted November 22, 2011 What I am looking for is that if any field in a database is empty, the form comes back and puts in the word "empty" into the web page, but not the database. I got this right now, it shows the first part fine, but the second part it doesn't show the "Empty" comment... I don't understand why... as far as I can tell the code is fine... <?php //Nonconformity, Disposition, Comments and Comments & Additional Details echo '<div id="box3">'; if (!empty($row['Nonconformity']) || !empty($row['Disposition']) || !empty($row['Comments']) || !empty($row['CommentsAdditional_Details'])) { echo '<div id="non"><span class="b">Nonconformity: </span><br />' . $row['Nonconformity'] . '</div>'; echo '<div id="dis"><span class="b">Disposition: </span><br />' . $row['Disposition'] . '</div>'; echo '<div id="comm"><span class="b">Comments: </span><br />' . $row['Comments'] . '</div>'; echo '<div id="comma"><span class="b">Comments and/or Additional Details: </span><br />' . $row['CommentsAdditional_Details'] . '</div>';} else if (empty($row['Nonconformity']) || empty($row['Disposition']) || empty($row['Comments']) || empty($row['CommentsAdditional_Details'])) { echo '<div id="non"><span class="b">Nonconformity: </span><br />Empty</div>'; echo '<div id="dis"><span class="b">Disposition: <br /></span>Empty</div>'; echo '<div id="comm"><span class="b">Comments: <br /></span>Empty</div>'; echo '<div id="comma"><span class="b">Comments and/or Additional Details: </span><br />Empty</div>';} echo '</div>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/251583-if-database-field-is-empty-how-to-show-on-form-empty/ Share on other sites More sharing options...
teynon Posted November 22, 2011 Share Posted November 22, 2011 You can do this a couple of ways. One way is to check each variable at the beginning like: if (empty($row['Nonconformity'])) $row['Nonconformity'] = "Empty"; The reason your current code isn't working is because if any of the conditions evaluates true in in the first condition, then all of the results are printed out. Quote Link to comment https://forums.phpfreaks.com/topic/251583-if-database-field-is-empty-how-to-show-on-form-empty/#findComment-1290243 Share on other sites More sharing options...
Matt Ridge Posted November 22, 2011 Author Share Posted November 22, 2011 You can do this a couple of ways. One way is to check each variable at the beginning like: if (empty($row['Nonconformity'])) $row['Nonconformity'] = "Empty"; The reason your current code isn't working is because if any of the conditions evaluates true in in the first condition, then all of the results are printed out. How will that work with the code I posted though? Quote Link to comment https://forums.phpfreaks.com/topic/251583-if-database-field-is-empty-how-to-show-on-form-empty/#findComment-1290245 Share on other sites More sharing options...
teynon Posted November 22, 2011 Share Posted November 22, 2011 What I am looking for is that if any field in a database is empty, the form comes back and puts in the word "empty" into the web page, but not the database. I got this right now, it shows the first part fine, but the second part it doesn't show the "Empty" comment... I don't understand why... as far as I can tell the code is fine... <?php //Nonconformity, Disposition, Comments and Comments & Additional Details echo '<div id="box3">'; if (!empty($row['Nonconformity']) || !empty($row['Disposition']) || !empty($row['Comments']) || !empty($row['CommentsAdditional_Details'])) { echo '<div id="non"><span class="b">Nonconformity: </span><br />' . $row['Nonconformity'] . '</div>'; echo '<div id="dis"><span class="b">Disposition: </span><br />' . $row['Disposition'] . '</div>'; echo '<div id="comm"><span class="b">Comments: </span><br />' . $row['Comments'] . '</div>'; echo '<div id="comma"><span class="b">Comments and/or Additional Details: </span><br />' . $row['CommentsAdditional_Details'] . '</div>';} else if (empty($row['Nonconformity']) || empty($row['Disposition']) || empty($row['Comments']) || empty($row['CommentsAdditional_Details'])) { echo '<div id="non"><span class="b">Nonconformity: </span><br />Empty</div>'; echo '<div id="dis"><span class="b">Disposition: <br /></span>Empty</div>'; echo '<div id="comm"><span class="b">Comments: <br /></span>Empty</div>'; echo '<div id="comma"><span class="b">Comments and/or Additional Details: </span><br />Empty</div>';} echo '</div>'; ?> <?php //Nonconformity, Disposition, Comments and Comments & Additional Details if (empty($row['Nonconformity'])) $row['Nonconformity'] = "Empty"; if (empty($row['Disposition'])) $row['Disposition'] = "Empty"; if (empty($row['Comments'])) $row['Comments'] = "Empty"; if (empty($row['CommentsAdditional_Details'])) $row['CommentsAdditional_Details'] = "Empty"; echo '<div id="box3">'; echo '<div id="non"><span class="b">Nonconformity: </span><br />' . $row['Nonconformity'] . '</div>'; echo '<div id="dis"><span class="b">Disposition: </span><br />' . $row['Disposition'] . '</div>'; echo '<div id="comm"><span class="b">Comments: </span><br />' . $row['Comments'] . '</div>'; echo '<div id="comma"><span class="b">Comments and/or Additional Details: </span><br />' . $row['CommentsAdditional_Details'] . '</div>'; echo '</div>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/251583-if-database-field-is-empty-how-to-show-on-form-empty/#findComment-1290248 Share on other sites More sharing options...
Matt Ridge Posted November 22, 2011 Author Share Posted November 22, 2011 Why does that work? It looks as if it would double post... Is this considered a sanitation? Quote Link to comment https://forums.phpfreaks.com/topic/251583-if-database-field-is-empty-how-to-show-on-form-empty/#findComment-1290253 Share on other sites More sharing options...
teynon Posted November 22, 2011 Share Posted November 22, 2011 I'm not sure what you mean. You're outputting the variables in this section of code into a div. Not a textbox. It looks like it's already submitted to the database, but I don't know what the rest of your code looks like. Quote Link to comment https://forums.phpfreaks.com/topic/251583-if-database-field-is-empty-how-to-show-on-form-empty/#findComment-1290256 Share on other sites More sharing options...
Drummin Posted November 22, 2011 Share Posted November 22, 2011 Looks like you're missing the brackets after your else statement surrounding your second IF statement. Also, shouldn't your first IF be using && so first set shows if all values are not empty and the second section shows if one of the fields is empty? Just IF statements shown below. if (!empty($row['Nonconformity']) && !empty($row['Disposition']) && !empty($row['Comments']) && !empty($row['CommentsAdditional_Details'])) { //Show first section }else{ if (empty($row['Nonconformity']) || empty($row['Disposition']) || empty($row['Comments']) || empty($row['CommentsAdditional_Details'])) { //Show second section } } Quote Link to comment https://forums.phpfreaks.com/topic/251583-if-database-field-is-empty-how-to-show-on-form-empty/#findComment-1290275 Share on other sites More sharing options...
Fadion Posted November 22, 2011 Share Posted November 22, 2011 teynon solution looks fine to me, but I think you're not getting it quite right or you're aiming for something completely different (wich I'm not understanding). The code you have checks if any of the database fields is empty and if only one of them is empty (Logical OR), the code will fallback to the "else" part, showing everything as "empty". I guess what you want is: if one of the database fields is empty, it will show as "empty" without affecting all the others. And that's what tenyon's solution does! Basically, you check all the fields individually if they're empty or not: if yes, give them a value of "empty"; if not, leave them as their are. It makes total sense! Quote Link to comment https://forums.phpfreaks.com/topic/251583-if-database-field-is-empty-how-to-show-on-form-empty/#findComment-1290297 Share on other sites More sharing options...
Matt Ridge Posted November 22, 2011 Author Share Posted November 22, 2011 I'm not sure what you mean. You're outputting the variables in this section of code into a div. Not a textbox. It looks like it's already submitted to the database, but I don't know what the rest of your code looks like. No your idea works perfectly, I just wanted to know why it worked, so I can understand for later use... I don't like copy and pasting anything without understanding why or how it works, that's all Quote Link to comment https://forums.phpfreaks.com/topic/251583-if-database-field-is-empty-how-to-show-on-form-empty/#findComment-1290386 Share on other sites More sharing options...
Nodral Posted November 22, 2011 Share Posted November 22, 2011 The way I tend to do this is even more straight forward. if(strlen($row) <1) { $row = "Empty" } Simples!! Quote Link to comment https://forums.phpfreaks.com/topic/251583-if-database-field-is-empty-how-to-show-on-form-empty/#findComment-1290398 Share on other sites More sharing options...
Matt Ridge Posted November 22, 2011 Author Share Posted November 22, 2011 The way I tend to do this is even more straight forward. if(strlen($row) <1) { $row = "Empty" } Simples!! May be simple, but why does it work? Quote Link to comment https://forums.phpfreaks.com/topic/251583-if-database-field-is-empty-how-to-show-on-form-empty/#findComment-1290399 Share on other sites More sharing options...
Nodral Posted November 22, 2011 Share Posted November 22, 2011 strlen() test the number of charachters in the returned value. If it is not empty, the length will be greater than 0. If it is empty, the length will be 0, and therefore you set the value of the variable to be "Empty" Quote Link to comment https://forums.phpfreaks.com/topic/251583-if-database-field-is-empty-how-to-show-on-form-empty/#findComment-1290400 Share on other sites More sharing options...
teynon Posted November 22, 2011 Share Posted November 22, 2011 Nodral, I don't see how this is more straight forward... PHP has a built in function for is_empty. No reason to beat around the bush. That just makes it harder for someone reading the code. Quote Link to comment https://forums.phpfreaks.com/topic/251583-if-database-field-is-empty-how-to-show-on-form-empty/#findComment-1290415 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.