Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End How can I write an if statement if one value is null?

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #46393
    chrisburton
    Participant

    Code:

    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?

    #142823
    chrisburton
    Participant

    I think I might need to use `empty()` instead of `isset()`

    #142826
    chrisburton
    Participant

    Thanks for the resource, @BenWalker


    @CrocoDillon
    Any documentation on `?:`. Can’t seem to find it.

    #142827
    srikarg
    Participant

    It’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.

    #142829
    srikarg
    Participant

    Oh, ok then @CrocoDillon. Thanks for the tip!

    #142831
    chrisburton
    Participant

    Now I’m confused.

    I can just do this?:

    $title = $trakt->movie->title ? $trakt->show->title : “None”;

    #142834
    chrisburton
    Participant

    Ok, 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`

    #142839
    chrisburton
    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`.

    #142845
    chrisburton
    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. }

    #142846
    chrisburton
    Participant

    @traq THAT WORKED!

    …doing some testing to make sure.

    #142849
    chrisburton
    Participant

    Thanks @CrocoDillon, @traq, @BenWalker and @srig99.

    Solution (by @traq):

    $title = @$trakt->movie->title
    or $title = @$trakt->show->title
    or $title = “None”;

    #142854
    chrisburton
    Participant

    @BenWalker I still like to thank those who contribute their time.

    #142865
    jurotek
    Participant

    @traq, Hey buddy, the ? and none is comparable to sql or and null criteria?

    #142872
    jurotek
    Participant

    I 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?

    #142883
    jurotek
    Participant

    @traq, Thanx, I was just thinking lately that I should start learning PHP. I know it will come handy. Thanx again.

Viewing 15 posts - 1 through 15 (of 15 total)
  • The forum ‘Back End’ is closed to new topics and replies.