Michael Murphy Posted October 19, 2011 Share Posted October 19, 2011 It sounds simple, but I'm having some real issues detecting if a field is empty (has no content) I have two fields : summary - textarea summary_de - textarea I want to detect if $summary_de is empty if its empty show $summary if $summary is empty show nothing I have tried the following tests : // Test A if ($page->summary_de) { echo "Test A : True"; } else { echo "Test A : False"; } echo "<hr />"; // Test B if (empty($page->summary_de)) { echo "Test B : True"; } else { echo "Test B : False"; } echo "<hr />"; // Test C if (isset($page->summary_de)) { echo "Test C : True"; } else { echo "Test C : False"; } echo "<hr />"; // Test D if (isset($page->summary_de) && $page->summary_de === '') { echo "Test D : True"; } else { echo "Test D : False"; } echo "<hr />"; // Test E if (is_null($page->summary_de)) { echo "Test E : True"; } else { echo "Test E : False"; } The results on a published page when $summary_de is empty Test A : True Test B : False Test C : True Test D : False Test E : False adding some text into $summary_de and saving : Test A : True Test B : False Test C : True Test D : False Test E : False after removing the $summary_de text and saving again (so its empty) - same results. Test A : True Test B : False Test C : True Test D : False Test E : False So all the tests are the same. Any ideas where I am going wrong? Tried deleting cache, but still the same. I'm using latest version of 2.1 from github. Thanks, Michael Link to comment Share on other sites More sharing options...
ryan Posted October 19, 2011 Share Posted October 19, 2011 Your first test (Test A) is what I would personally use, i.e. if($page->summary_de) { ... } Are these TinyMCE fields? I think you need to look at what's actually in the field. I have a feeling you've got some whitespace in there or something like one of these annoying TinyMCE droppings: "<p> </p>" or maybe just a carriage return or couple of spaces. Get the value out of an HTML context to see what's really in the field: echo htmlentities(var_dump($page->summary_de)); If you are dealing with possible whitespace in the field (as opposed to HTML tags or entities) then your if statement could account for that by doing this: if(trim($page->summary_de)) { ... } Don't bother with using isset() or is_null() -- those are really more useful in determining of the page's template carries those fields at all. But I don't think they are particularly useful for what you are trying to do. 2 Link to comment Share on other sites More sharing options...
Michael Murphy Posted October 20, 2011 Author Share Posted October 20, 2011 Thanks Ryan, you are right. It was not TinyMCE interfering but the Markdown text formatter. Removing the Markdown text formatter so it's just a plain textarea field, and the standard check : if($page->summary_de) { ... } works as normal. If using the Markdown text formatter then the php trim function clears up any extra whitespace added, and the following test works : if(trim($page->summary_de)) { ... } 1 Link to comment Share on other sites More sharing options...
ryan Posted October 20, 2011 Share Posted October 20, 2011 Ah right I forgot about Markdown, but that makes perfect sense. Glad you found a solution there and thanks for the follow-up. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now