- This topic is empty.
-
AuthorPosts
-
July 14, 2013 at 6:15 pm #46393
chrisburton
ParticipantCode:
if(isset($trakt->movie->title)) {
$title = $trakt->movie->title;
} elseif(isset($trakt->show->title)) {
$title = $trakt->show->title;
} else {
$title = “None”;
}How could I write it to something like this:
If `$trakt->movie->title` or `$trakt->show->title` isset(), $title = $result?
July 14, 2013 at 6:57 pm #142823chrisburton
ParticipantI think I might need to use `empty()` instead of `isset()`
July 14, 2013 at 7:18 pm #142826chrisburton
ParticipantThanks for the resource, @BenWalker
@CrocoDillon Any documentation on `?:`. Can’t seem to find it.July 14, 2013 at 7:19 pm #142827srikarg
ParticipantIt’s the ternary operator @chrisburton.
http://davidwalsh.name/php-shorthand-if-else-ternary-operators
Also @CrocoDillon, shouldn’t it be
$title = $trakt->movie->title ? $trakt->movie->title : $trakt->show->title ? $trakt->show->title : "None";
Not sure if that’s right though. I never saw the operator with the ? and the : right next to eachother.July 14, 2013 at 7:32 pm #142829srikarg
ParticipantOh, ok then @CrocoDillon. Thanks for the tip!
July 14, 2013 at 7:38 pm #142831chrisburton
ParticipantNow I’m confused.
I can just do this?:
$title = $trakt->movie->title ? $trakt->show->title : “None”;
July 14, 2013 at 7:47 pm #142834chrisburton
ParticipantOk, then it appears that’s not what I’m looking for.
If `$trakt->movie->title` evaluates to a string and `$trakt->show->title` is empty, the `$title` should equal `$trakt->movie->title`, not `$trakt->show->title` and vice versa.
####Simplified:
if $trakt->movie->title` returns a string and `$trakt->show->title` is empty
$title = `$trakt->movie->title`
if $trakt->movie->title` is empty but `$trakt->show->title` returns a string
$title = `$trakt->show->title`
July 14, 2013 at 8:25 pm #142839chrisburton
Participant@CrocoDillon I’m grabbing the last value in an array. So there will either be a `movie->title` or `show-title`. There can’t be both and neither one can be empty. One of them will return with a title string.
I’m trying to simplify an if/elseif.
####Current code:
$trakt = $data->activity[0];
if(empty($trakt->movie->title)) {
$title = $trakt->movie->title;
} elseif(empty($trakt->show->title)) {
$title = $trakt->show->title;
} else {
$title = “None”;
}Basically do this (if it was valid):
Let’s say the last thing I watched was a `movie`.
IF $trakt->movie->title OR $trakt->show->title is empty then, $title = whichever isn’t empty
The result would be the value of `$trakt->movie->title` since I watched a `movie` last. Not a `show`.
July 14, 2013 at 9:03 pm #142845chrisburton
Participant@CrocoDillon It worked when the last thing I watched was a movie however, if the last thing I watched was a show, I get this:
Notice: Undefined property: stdClass::$movie in trakt.php on line 10
10. if($trakt->movie) {
11. $title = $trakt->movie->title;
12. } elseif($trakt->show) {
13. $title = $trakt->show->title;
14. } else {
15. $title = “None”;
16. }July 14, 2013 at 9:06 pm #142846chrisburton
Participant@traq THAT WORKED!
…doing some testing to make sure.
July 14, 2013 at 9:15 pm #142849chrisburton
ParticipantThanks @CrocoDillon, @traq, @BenWalker and @srig99.
Solution (by @traq):
$title = @$trakt->movie->title
or $title = @$trakt->show->title
or $title = “None”;July 14, 2013 at 9:35 pm #142854chrisburton
Participant@BenWalker I still like to thank those who contribute their time.
July 14, 2013 at 10:06 pm #142865July 14, 2013 at 10:31 pm #142872jurotek
ParticipantI have a good knowledge of DB Dev, as you prolly know, doing it for over 10 years (Access, MySql and SQL Server) but I am lately getting intrigued with PHP and how it works in conjunction with DB. Is it like a scripting language to retrieve attribute values from predefined Transact query and place it in html? Or can you actually query tbl with it?
July 14, 2013 at 11:02 pm #142883 -
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.