Jump to content

If database field is empty, how to show on form "empty"


Matt Ridge

Recommended Posts

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>';
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>';
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

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.