0

Essentially, the data should look as follows, but as one array for each user that contains their info. It should be packaged as a JSON after completely looping through.

Preface

Using the pre tags with print_r I got what it should look like using some humorous sample data:

User 1 is following:

Array
(
    [0] => Array
        (
            [first] => Hugh
            [last] => Janus
            [bg] => /resources/bg/2/b.png
            [prof_pic] => /resources/profilepics/2/b.jpg
            [bio] => 50% proctologist, 50% baker.
        )

)
Array
(
    [0] => Array
        (
            [first] => Andrew
            [last] => Dickens
            [bg] => /resources/bg/3.c.jpg
            [prof_pic] => /resources/profilepics/3/c.jpeg
            [bio] => Therapists are pretty cool.
        )

)
Array
(
    [0] => Array
        (
            [first] => Anita
            [last] => Dickson
            [bg] => /resources/bg/4/d.jpg
            [prof_pic] => /resources/profilepics/4/d.JPG
            [bio] => Just another barista.
        )

)

Essentially, everything needs to come back as one JSON item that someone could use for any purpose with the supplied info.

Background on the Following Code

This code selects all User IDs from the table 'followers' where the person who is following someone is equal to fB and the person who owns that friendship is fA. In this instance, userID 1 is trying to look at a page of every person they follow with their profile pic, background pic, first name, last name, and bio. The data that the JSON is outputting will be formatted in another file, but this is just being used to curate the raw data.

$dbh is the PDO database connection and the code uses prepared statements for security.

Code

$flingJSON = 0;
$userid = "1";
$result = $dbh->prepare('SELECT fA
    FROM followers
    WHERE fB=:userid');
$result->bindParam(':userid', $userid);
$result->execute();
$rawData = $result->fetchAll(PDO::FETCH_ASSOC);
foreach($rawData as $following) {
    $followingID = $following['fA'];
    $getInfoFling = $dbh->prepare('SELECT first, last, bg, prof_pic, bio
        FROM users
        WHERE id=:flingID
        ');
    $getInfoFling->bindParam(':flingID', $followingID);
    $getInfoFling->execute();
    $flingJSON = $getInfoFling->fetchAll(PDO::FETCH_ASSOC);
    echo "<pre>";
    print_r($flingJSON);
    echo "</pre>";
}

Your help is greatly appreciated in advance and as a thanks you'll get a shout-out in the documentation for this file.

1 Answer 1

1

This should return a single array with all the user's info:

//Create empty data array
$flingData = array();
$userid = "1";
$result = $dbh->prepare('SELECT fA
    FROM followers
    WHERE fB=:userid');
$result->bindParam(':userid', $userid);
$result->execute();
$rawData = $result->fetchAll(PDO::FETCH_ASSOC);
foreach($rawData as $following) {
    $followingID = $following['fA'];
    $getInfoFling = $dbh->prepare('SELECT first, last, bg, prof_pic, bio
        FROM users
        WHERE id=:flingID
        ');
    $getInfoFling->bindParam(':flingID', $followingID);
    $getInfoFling->execute();
    //Append user info to data array
    $flingData[] = $getInfoFling->fetch(PDO::FETCH_ASSOC);
}

//Create JSON string
$flingJSON = json_encode($flingData);

echo "<pre>";
print_r($flingJSON);
echo "</pre>";
Sign up to request clarification or add additional context in comments.

1 Comment

That seemed to do the trick. I'll be sure to thank you in the documentation for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.