Forums

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

Home Forums Back End case issue

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #168894
    Jamie
    Participant

    I am working in WordPress. I am getting some values from option tree. I put them in an array. If I do a foreach loop on the variable, I do get the content. I tried putting the data in a case structure and when I run the code, I get nothing. What am I doing wrong?

    $facebook = ot_get_option('facebook');
    
    $twitter = ot_get_option('twitter');
    $flickr = ot_get_option('flickr');
    $vimeo = ot_get_option('vimeo');
    $delicious = ot_get_option('delicious');
    $google = ot_get_option('google');
    $email = ot_get_option('email');
    
    $social = array($facebook, $twitter, $flickr, $vimeo, $delicious, $google, $email);
    
    switch($social){
        case 'facebook':
            return 'this is facebook';
            break;
        case 'twitter':
            return 'this is twitter';
            break;
        case 'flickr':
            return 'this is flickr';
            break;
        case 'vimeo':
            return 'this is vimeo';
            break;
        case 'delicious':
            return 'this is delicious';
            break;
        case 'google':
            return 'this is google';
            break;
        case 'email':
            return 'this is email';
            break;
    
    }
    #168899
    __
    Participant

    $social, as you explained, is an array. It will never match a string (which is what "facebook", "twitter", etc. are).

    I don’t know your specific requirements, but it seems you only need to know which of your calls to ot_get_option returned true. Is this correct? If so,

    <?php
    
    function findSocial(){
        // list of social site names
        $social = array( "facebook","twitter","vimeo" /* etc. */ );
    
        // loop through each
        foreach( $social as $site ){
    
            // if ot_get_option matches, return that site name
            if( ot_get_option( $site ) ){
                return "this is $site";
            }
        }
        // no matches
        return false;
    }
    

    Usage:

    echo findSocial();
    /* prints something like:
    
        this is css-tricks
    
    */
    

    You could also pass $social as a function parameter, which would make it much easier to add or remove names from the list.

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