Grow your CSS skills. Land your dream job.

Condensed way to write this?

  • # January 16, 2014 at 1:30 pm
    foreach ($response as $result) {
        if($result['provider'] == 'Facebook') {
            $user = 
                array(
                    'provider' => $result['provider'],
                    'id' => $result['uid'],
                    'name' => $result['info']['name'],
                    'image' => $result['info']['image'],
                    'link' => $result['info']['urls']['facebook'],
                );
            echo "<h1>".$user['provider']."</h1>";
            echo "<p>".$user['id']."</p>";
            echo '<p><img src="'.$user['image'].'" /></p>';
            echo "<p>".$user['name']."</p>";
            echo "<p>".$user['link']."</p>";
            exit;
        }elseif($result['provider'] == 'Twitter') {
            $user =
                array(
                    'provider' => $result['provider'],
                    'id' => $result['uid'],
                    'name' => $result['info']['name'],
                    'image' => $result['info']['image'],
                    'link' => $result['info']['urls']['twitter'],
                );
            echo "<h1>".$user['provider']."</h1>";
            echo "<p>".$user['id']."</p>";
            echo '<p><img src="'.$user['image'].'" /></p>';
            echo "<p>".$user['name']."</p>";
            echo "<p>".$user['link']."</p>";
            exit;
        }else{
            header('Location: http://chrisburton.me/');
            exit;
        }
    }
    

    The only difference in both if/elseif conditions are these two

    'link' => $result['info']['urls']['facebook']
    
    'link' => $result['info']['urls']['twitter']
    

    I tried combining them and creating an if like this but it didn’t work (also tried if/elseif).

    $user = 
        array(
            'provider' => $result['provider'],
            'id' => $result['uid'],
            'name' => $result['info']['name'],
            'image' => $result['info']['image'],
            if($result['provider'] == 'Facebook') {
                'link' => $result['info']['urls']['facebook'],
            }if($result['provider'] == 'Twitter') {
                'link' => $result['info']['urls']['twitter'],
            }
        );
    
    # January 16, 2014 at 2:13 pm

    I’m not able to test this at the moment but mabe someting like this, where you determine what provider is responding then just assign a variable to that field.

    foreach ($response as $result) {
        if ( $result['provider'] == 'Facebook' ) {
            $provider = 'facebook';
        } else {
            $provider = 'twitter';
        }
        $user = array(
            'provider' => $result['provider'],
            'id' => $result['uid'],
            'name' => $result['info']['name'],
            'image' => $result['info']['image'],
            'link' => $result['info']['urls'][$provider],
        );
        echo "<h1>".$user['provider']."</h1>";
        echo "<p>".$user['id']."</p>";
        echo '<p><img src="'.$user['image'].'" /></p>';
        echo "<p>".$user['name']."</p>";
        echo "<p>".$user['link']."</p>";
        exit;
    }
    

    NOTE: I haven’t tested this code at all. Just quickly rewrote it.

    Hope it helps.

    # January 16, 2014 at 2:40 pm

    Unfortunately that doesn’t work. And of course, my VPS isn’t spitting out errors like it should. Just a blank page. Thanks for trying, Alen. Much appreciated.

    # January 16, 2014 at 3:05 pm

    Try it with using quotes around $provider in $result['info']['urls'][$provider]

    # January 16, 2014 at 3:07 pm

    I don’t think that’s allowed in PHP, no? I’ll try it, though.

    Edit: Didn’t work.

    # January 16, 2014 at 3:19 pm

    Using double quotes?

    # January 16, 2014 at 3:20 pm

    Nope that wouldn’t work either. But you were right the first time. It was another piece of code below that which was causing the error. Thanks for the solution, Alen.

    # January 17, 2014 at 12:42 am

    Just my thoughts…

    foreach ($response as $result) {
        echo    "<h1>".($result['provider']=='Facebook'?'Facebook':'Twitter')."</h1>".
                "<p>".$result['uid']."</p>".
                '<p><img src="'.$result['info']['image'].'" /></p>'.
                "<p>".$result['info']['name']."</p>".
                "<p>".($result['provider']=='Facebook'?$result['info']['urls']['facebook']:$result['info']['urls']['twitter'])."</p>";
        exit; // not sure what the exit is for in a loop.
    }
    
    # January 17, 2014 at 12:46 am

    Thanks @stevet. Although your solution may work, it is beyond my understanding.

    // not sure what the exit is for in a loop.

    The reason I have exit is because my code continues to loop through, spitting out the same values multiple times.

    # January 17, 2014 at 4:40 am

    Figured out along with another person from SO that the foreach loop was throwing a fatal error if I didn’t use break; after the last echo.

    Because I am obtaining one user at a time, a foreach loop is probably not what I should be using. Instead I can just do:

    $result = $response['auth'];
    
        if ( $result['provider'] == 'Facebook' ) {
            $provider = 'facebook';
        } else {
            $provider = 'twitter';
        }
        $user = array(
            'provider' => $result['provider'],
            'id' => $result['uid'],
            'name' => $result['info']['name'],
            'image' => $result['info']['image'],
            'link' => $result['info']['urls'][$provider],
        );
        echo "<h1>".$user['provider']."</h1>";
        echo "<p>".$user['id']."</p>";
        echo '<p><img src="'.$user['image'].'" /></p>';
        echo "<p>".$user['name']."</p>";
        echo "<p>".$user['link']."</p>";
    
    # January 17, 2014 at 1:15 pm

    I like hearing progress!

    # January 17, 2014 at 4:35 pm

    Thanks for the support, Gray!

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".