Texan78 Posted December 17, 2014 Share Posted December 17, 2014 (edited) Hello, I have done this with SimpleXML variables many times. I seem to be struggling with something really simple as I am venturing out with some JSON. Basically there is a variable $nowcast. Sometimes that node may be empty. So I want to display an alternative message if it is empty like it is now. Unfortunately I am having some struggles and can't get the message to display when the node is empty. What am I missing? -Thanks // Lets get parse the data and create the variables $json_string = file_get_contents("http://api.wunderground.com/api/REMOVED_KEY/geolookup/conditions/q/TX/Mesquite.json"); $parsed_json = json_decode($json_string); $Updated = $parsed_json->{'current_observation'}->{'observation_time'}; $location = $parsed_json->{'location'}->{'city'}; $nowcast = $parsed_json->{'current_observation'}->{'nowcast'}; // Lets make some style classes for your divs $divContainer = "width:{$divWidth};background-color:{$bkgColor};color:{$txtColor};border:{$border} 1px solid;font-size{$fontSize};"; $divHeader = "width:100%;background-color:{$titleBar};"; $divWrapper = "padding: 5px 5px 5px 5px;"; $pStyle = "color:{$titleTxt};margin:0 0 2px 2px;"; // If there is an available message display it $nowCast = "<h3 class='title'>NWS NowCast<span style='float:right'> {$Updated}</span></h3>"; $nowCast = "<hr />"; $nowCast = "<div class='NWSRound'>"; $nowCast = "<div style='{$divWrapper}'> {$nowcast}</div>"; $nowCast = "</div>"; $nowCast = "<table border='0' cellpadding='0' cellspacing='0' width='100%'><tbody><tr><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td><td class='shadow-mid' width='100%'><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td></tr></tbody></table>"; // If there is no available message then lets display the following $noMessage = "<h3 class='title'>NWS NowCast<span style='float:right'> {$Updated}</span></h3>"; $noMessage = "<hr />"; $noMessage = "<div class='NWSRound'>"; $noMessage = "<div style='{$divWrapper}'> No valid short term forecast for {$location} currently available at this time</div>"; $noMessage = "</div>"; $noMessage = "<table border='0' cellpadding='0' cellspacing='0' width='100%'><tbody><tr><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td><td class='shadow-mid' width='100%'><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td></tr></tbody></table>"; if(count(json_decode($nowcast,1))==0) { echo $nowCast; } else { echo $noMessage; } Edited December 17, 2014 by Texan78 Quote Link to comment https://forums.phpfreaks.com/topic/293142-showing-alternative-message-when-no-data/ Share on other sites More sharing options...
requinix Posted December 17, 2014 Share Posted December 17, 2014 Unless $nowcast (which is distressingly close to $nowCast) is, itself, JSON data - thus an array encoded into JSON twice - you should only be looking at $nowcast by itself. No json_decode(). Or you could dump the value of $nowcast and see what you're actually working with. Quote Link to comment https://forums.phpfreaks.com/topic/293142-showing-alternative-message-when-no-data/#findComment-1499817 Share on other sites More sharing options...
Texan78 Posted December 17, 2014 Author Share Posted December 17, 2014 I am not quite sure what you mean. The variable name in this case is irrelevant though as one is cast and the other is Cast but, for sake of simplicity as it was only for testing I have changed it even though it had no bearing over what I was trying to do. Could you please give me an example as I am not sure what you are talking about. Here are the changes I have made. <?php // Lets get parse the data and create the variables $json_string = file_get_contents("http://api.wunderground.com/api/74a4b7a0d0663fd2/geolookup/conditions/q/TX/Mesquite.json"); $parsed_json = json_decode($json_string); $Updated = $parsed_json->{'current_observation'}->{'observation_time'}; $location = $parsed_json->{'location'}->{'city'}; $nowcast = $parsed_json->{'current_observation'}->{'nowcast'}; // Lets make some style classes for your divs $divContainer = "width:{$divWidth};background-color:{$bkgColor};color:{$txtColor};border:{$border} 1px solid;font-size{$fontSize};"; $divHeader = "width:100%;background-color:{$titleBar};"; $divWrapper = "padding: 5px 5px 5px 5px;"; $pStyle = "color:{$titleTxt};margin:0 0 2px 2px;"; // If there is an available message display it $shortTerm = "<h3 class='title'>NWS NowCast<span style='float:right'> {$Updated}</span></h3>"; $shortTerm = "<hr />"; $shortTerm = "<div class='NWSRound'>"; $shortTerm = "<div style='{$divWrapper}'> {$nowcast}</div>"; $shortTerm = "</div>"; $shortTerm = "<table border='0' cellpadding='0' cellspacing='0' width='100%'><tbody><tr><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td><td class='shadow-mid' width='100%'><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td></tr></tbody></table>"; // If there is no available message then lets display the following $noMessage = "<h3 class='title'>NWS NowCast<span style='float:right'> {$Updated}</span></h3>"; $noMessage = "<hr />"; $noMessage = "<div class='NWSRound'>"; $noMessage = "<div style='{$divWrapper}'> No valid short term forecast for {$location} currently available at this time</div>"; $noMessage = "</div>"; $noMessage = "<table border='0' cellpadding='0' cellspacing='0' width='100%'><tbody><tr><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td><td class='shadow-mid' width='100%'><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td><td><img alt='' src='images/1pixel.gif' border='0' height='7' width='7' /></td></tr></tbody></table>"; if (json != null && json.getString($nowcast) != null){ echo $shortTerm; }else{ echo $noMessage; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/293142-showing-alternative-message-when-no-data/#findComment-1499829 Share on other sites More sharing options...
requinix Posted December 17, 2014 Share Posted December 17, 2014 Now the "json != null" and "json.getString" is completely wrong. You've already decoded the JSON. Up at the top. You don't have to do any more decoding. Use var_dump($nowcast) to see the value(s) you have to work with. Then you can decide what you should check for in your code. While I'm here, $Updated = $parsed_json->{'current_observation'}->{'observation_time'}; $location = $parsed_json->{'location'}->{'city'}; $nowcast = $parsed_json->{'current_observation'}->{'nowcast'};Please don't use the {} syntax when it's not needed. Regular property access will work just fine. $Updated = $parsed_json->current_observation->observation_time; $location = $parsed_json->location->city; $nowcast = $parsed_json->current_observation->nowcast; Quote Link to comment https://forums.phpfreaks.com/topic/293142-showing-alternative-message-when-no-data/#findComment-1499888 Share on other sites More sharing options...
Texan78 Posted December 17, 2014 Author Share Posted December 17, 2014 Use var_dump($nowcast) to see the value(s) you have to work with. Then you can decide what you should check for in your code. I just need to check if it is empty, I don't need to know the values. If empty show this, if not then show that. I don't remember it being this difficult with SimpleXML. Could you please give me an example. While I'm here, $Updated = $parsed_json->{'current_observation'}->{'observation_time'};$location = $parsed_json->{'location'}->{'city'}; $nowcast = $parsed_json->{'current_observation'}->{'nowcast'};Please don't use the {} syntax when it's not needed. Regular property access will work just fine. $Updated = $parsed_json->current_observation->observation_time; $location = $parsed_json->location->city; $nowcast = $parsed_json->current_observation->nowcast; I will keep that in mind as I like that better and I think it is cleaner. It is like I do for SimpleXML. It was only done that way as that is how the tutorial I was reading was showing how to do it. Keep in mind, I am just learning this. Quote Link to comment https://forums.phpfreaks.com/topic/293142-showing-alternative-message-when-no-data/#findComment-1499900 Share on other sites More sharing options...
requinix Posted December 17, 2014 Share Posted December 17, 2014 (edited) I just need to check if it is empty, I don't need to know the values.How can you tell if it's empty without knowing what its value is? Does empty mean null? Array with no items? Object with no properties? You only have to check the value once for yourself. When you know exactly what "empty" means then you can make sure your code checks for that. It was only done that way as that is how the tutorial I was reading was showing how to do it. Keep in mind, I am just learning this.The reason they used that is because sometimes SimpleXML and json_decode() will produce objects with invalid property names. Like Naturally you can't do $xml->the-node-name. PHP has a way where you can bend the rules a bit: the {} syntax you had. $xml->{"the-node-name"}While it works for valid property names too, like "current_observation", there really isn't any reason to use it then. And as you said, it sure looks cleaner without. Edited December 17, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/293142-showing-alternative-message-when-no-data/#findComment-1499910 Share on other sites More sharing options...
Texan78 Posted December 18, 2014 Author Share Posted December 18, 2014 How can you tell if it's empty without knowing what its value is? By value, do you mean if the node has content? Not always will it have content. Hence the reason I want to display an alternative message so there is not just an empty div with there is no content I.E. For example, with content..... "nowcast":"This is a test of the script caching. When this message updates or goes away then all is good. So far all is good since this is currently displaying. It is always sunny in Philideplhia" Without content..... "nowcast": That is how the response shows when it is decoded. So if "nowcast": is empty and there is no content then I want to show an alternative message. This shouldn't be THAT hard or difficult of a question. I have done this numerous times with SimpleXML but, this is the first time I have tried JSON and everything I have tried has failed and every example I have found doesn't work or I am told it is wrong. How am I suppose to know any better. Again, keep in mind. I am just learning, you have to keep it simple and give me examples to work with. I don't see how a var_dump would accomplish that since all it returns is the same thing as an echo would except it has string(184) before the content if there is content and nothing if there isn't. Quote Link to comment https://forums.phpfreaks.com/topic/293142-showing-alternative-message-when-no-data/#findComment-1499922 Share on other sites More sharing options...
requinix Posted December 18, 2014 Share Posted December 18, 2014 Literally just nowcast: is invalid. There must be something after the colon. How about this. It looks like the value will be a string. Maybe an empty string. Or maybe even null. But probably not an array or object. You already have the fully-decoded JSON as $parsed_json, and you have nowcast as the variable $nowcast. The easiest way to check for something "being empty" is if (empty($nowcast)) {empty() would also consider the string "0" to be empty, but I doubt that the "nowcast" thing would ever be just the number 0. What's more, even if it was, that's not a very useful value so you might as well pretend it was empty. All my questions up until now were because I wanted to find out - no, to have you tell me - what kind of values nowcast could be. And the answer was just what I said: about how it "will be a string". That's all. I didn't expect it to be so difficult... Quote Link to comment https://forums.phpfreaks.com/topic/293142-showing-alternative-message-when-no-data/#findComment-1499925 Share on other sites More sharing options...
Texan78 Posted December 18, 2014 Author Share Posted December 18, 2014 Literally just nowcast: is invalid. There must be something after the colon. If there is no content for that then the response from the API for that node just shows "nowcast": and that's it. I am back to square one now. All I need to know which I am struggling with after days and days of reading and research for how to do something which I have done many times before with SimpleXML is how to display an alternate message which I already have formatted and just need it to show when the above node response from the API has no content. Can someone PLEASE show me how I can accomplish this? Quote Link to comment https://forums.phpfreaks.com/topic/293142-showing-alternative-message-when-no-data/#findComment-1499933 Share on other sites More sharing options...
Texan78 Posted December 18, 2014 Author Share Posted December 18, 2014 So basically if the node nowcast = string(0) which means no content I need it to display $noMessage otherwise $shortTerm. Quote Link to comment https://forums.phpfreaks.com/topic/293142-showing-alternative-message-when-no-data/#findComment-1499934 Share on other sites More sharing options...
Solution Texan78 Posted December 18, 2014 Author Solution Share Posted December 18, 2014 Solved, this works. if (empty($nowcast)) { echo $noMessage; } else { echo $nowCast; } Also instead of... $shortTerm = It needed to be this. Notice the . before the equal. That was what was missing the whole time. $shortTerm .= Quote Link to comment https://forums.phpfreaks.com/topic/293142-showing-alternative-message-when-no-data/#findComment-1499936 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.