Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
PHP - JSON Question
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Object" data-source="post: 467525" data-attributes="member: 78351"><p>Just to elaborate on the answer above, so that you'll hopefully be able to understand what's going on and in the future can apply such yourself <img src="/styles/default/xenforo/smilies/emojione/smile.png" class="smilie" loading="lazy" alt=":)" title="Smile :)" data-shortname=":)" /></p><p></p><p>First, a variable "$json" is being assigned to an array with json data.</p><p>Secondly, the json data is being decoded and assigned to the "$data" variable.</p><p></p><p>This returns and array with a new instance the stdClass for each json object <a href="https://www.php.net/manual/en/language.types.object.php" target="_blank">https://www.php.net/manual/en/language.types.object.php</a></p><p></p><p>Thirdly the foreach is being used to loop through the array of data and inside the foreach the first div is being used to apply the textOn or the textOff class, which is being achieved by the use of ternary operators <a href="https://www.php.net/manual/en/language.operators.comparison.php" target="_blank">https://www.php.net/manual/en/language.operators.comparison.php</a></p><p></p><p><?= is a shorthand for "<?php echo" then it fetches the online status, the "?" means if true textOn and the ":" is else apply textOff</p><p></p><p>So:</p><p>[PHP]</p><p><?= $record['Online'] ? 'TextOn' : 'TextOff'; ?></p><p>[/PHP]</p><p></p><p>Becomes</p><p></p><p>[PHP]</p><p>if ($record['Online] == true) {</p><p> textOn</p><p>} else {</p><p> textOff</p><p>}</p><p>[/PHP]</p></blockquote><p></p>
[QUOTE="Object, post: 467525, member: 78351"] Just to elaborate on the answer above, so that you'll hopefully be able to understand what's going on and in the future can apply such yourself :) First, a variable "$json" is being assigned to an array with json data. Secondly, the json data is being decoded and assigned to the "$data" variable. This returns and array with a new instance the stdClass for each json object [URL]https://www.php.net/manual/en/language.types.object.php[/URL] Thirdly the foreach is being used to loop through the array of data and inside the foreach the first div is being used to apply the textOn or the textOff class, which is being achieved by the use of ternary operators [URL]https://www.php.net/manual/en/language.operators.comparison.php[/URL] <?= is a shorthand for "<?php echo" then it fetches the online status, the "?" means if true textOn and the ":" is else apply textOff So: [PHP] <?= $record['Online'] ? 'TextOn' : 'TextOff'; ?> [/PHP] Becomes [PHP] if ($record['Online] == true) { textOn } else { textOff } [/PHP] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
PHP - JSON Question
Top