1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

IF THEN Statement in Public Function

Discussion in 'General PHP and MySQL Discussions' started by Grizzly Adam, Oct 12, 2014.

  1. Grizzly Adam

    Grizzly Adam Active Member

    I have two statements that I want to combine into one depending on the user who owns the album. If the album owner id is 1351, I want to call the second snippet of code. Can anyone help?

    Code:
        public function getBreadCrumbs(array $user, $album = array())
        {
            $breadCrumbs = array();
    
            $breadCrumbs['user_'.$user['user_id']] = array(
                'href' => XenForo_Link::buildPublicLink('full:useralbums/list', $user),
                'value' => new XenForo_Phrase('xfr_useralbums_xs_albums', array('name' => $user['username'])),
            );
    
            if (!empty($album))
            {
                $breadCrumbs[$album['album_id']] = array(
                    'href' => XenForo_Link::buildPublicLink('full:useralbums/view', $album),
                    'value' => XenForo_Helper_String::censorString($album['title'])
                );
            }
    
            return $breadCrumbs;
        }
    Code:
    public function getGalleryCrumbs(array $user, $album = array())
        {
       
    
            if (!empty($album))
            {
                $breadCrumbs[$album['album_id']] = array(
                    'href' => XenForo_Link::buildPublicLink('full:useralbums/view', $album),
                    'value' => XenForo_Helper_String::censorString($album['title'])
                );
            }
    
            return $galleryCrumbs;
        }
     
  2. LPH

    LPH Well-Known Member

    Grizzly, there really isn't enough information to help. Do you want id 1351 to run both breadCrumbs and galleryCrumbs?

    Anyway, this is one control structure to call a method in an if / else - but more may be needed to get it to work, such as a global variable.

    Code:
    if ( $user['user_id'] == '1351' ) {
         getGalleryCrumbs();
    } else {
         getBreadCrumbs();
    }
    
    Of course, one could go ternary:

    http://davidwalsh.name/php-shorthand-if-else-ternary-operators

    And of course - this might totally fail because the calls can't be made in this manner but need self::getGalleryCrumbs() etc etc .
     
  3. Grizzly Adam

    Grizzly Adam Active Member

    Thanks for the response, we got this one sorted out last week.
     
    LPH likes this.
  4. LPH

    LPH Well-Known Member

    To help others then you might share how you solved the challenge you posted. :)
     
  5. Grizzly Adam

    Grizzly Adam Active Member

    This post shows what we ended up doing.

    [XFR] User Albums
     

Share This Page