Forums

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

Home Forums Back End Condensed way to write this?

  • This topic is empty.
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #160551
    chrisburton
    Participant
    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'],
            }
        );
    
    #160557
    Alen
    Participant

    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.

    #160562
    chrisburton
    Participant

    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.

    #160570
    Alen
    Participant

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

    #160571
    chrisburton
    Participant

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

    Edit: Didn’t work.

    #160572
    Alen
    Participant

    Using double quotes?

    #160573
    chrisburton
    Participant

    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.

    #160607
    stevet
    Participant

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

    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.

    #160625
    chrisburton
    Participant

    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>";
    
    #160662
    TheDoc
    Moderator

    I like hearing progress!

    #160680
    chrisburton
    Participant

    Thanks for the support, Gray!

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