First off, sorry if the title is off...it's the best thing I could think of as to what issue I'm facing.
In my database, I have a list of apps. In PHP, I'm pulling their data from their key, and encoding that data into JSON to be printed out (I'm making an API).
However, only the last app gets printed out in JSON. What I need is a JSON array with ANY apps that have the same key so I can loop through them later, and print out their data.
My code:
$getApp = "SELECT * FROM tundra.apps WHERE app_key = '" . $api_key . "'";
$appData = $dbConnection->query($getApp);
if ($appData->num_rows > 0){ // Check if app_key exists
while($row = $appData->fetch_assoc()) { // While retrieving rows
$jsonApp = json_encode(array( // Encode the row data
"app_name" => $row['app_name'],
"app_theme" => array(
"primary_color" => $row['app_primary_color'],
"primary_color_dark" => $row['app_primary_dark_color'],
"accent_color" => $row['app_accent_color'],
),
"app_navigation" => $row['app_navigation'],
"author_info" => array(
"author_name" => $row['app_author_name'],
"author_bio" => $row['app_author_bio'],
"links" => array(
"website" => $row['app_author_website'],
"googleplus" => $row['app_author_gplus'],
"twitter" => $row['app_author_twitter'],
"dribble" => $row['app_author_dribble'],
"behance" => $row['app_author_behance'],
)
),
"app_status" => $row['app_status']
), JSON_UNESCAPED_SLASHES);
}
// Format and print JSON data
echo "<pre>" . prettyPrint($jsonApp) . "</pre>";
} else { // No key found, encode error
$jsonError = json_encode(
array(
array(
"error" => array(
"code" => "8",
"message" => "Invalid API key specified"
)
)
), JSON_UNESCAPED_SLASHES);
echo "<pre>" . prettyPrint($jsonError) . "</pre>";
}
For the above, I tried echoing the JSON data in the while loop, but that (as expected) printed <pre>s for every row.
Here's the JSON output from the above code:
{
"app_name":"Andrew's Second App",
"app_theme":{
"primary_color":"#FFFFFF",
"primary_color_dark":"#E0E0E0",
"accent_color":"#E91E63"
},
"app_navigation":"0",
"author_info":{
"author_name":"Andrew Quebe",
"author_bio":"I'm a developer of stuff.",
"links":{
"website":"http://www.andrewquebe.com",
"googleplus":"https://plus.google.com/+AndrewQuebe",
"twitter":"https://twitter.com/andrew_quebe",
"dribble":"None",
"behance":"None"
}
},
"app_status":"1"
}
Note: the data is perfectly formatted, but I need the data to be in an array and I'm unsure as to how to do this.
$jsonAppon each loop, rather than adding to it