I am very new to AngularJS and started to rebuild a restaurant finder app i have previously written in pure PHP. However i would now like to achieve faster database access and asynchronous sorting, hence the desire to learn AngularJS.
I have not used JSON for a while and i am literally stuck with getting basic queries back to AngularJS. Otherwise the syntax is correct but i am unable to get additional data from another table.
So first the basic controller to fetch the data from a PHP file:
var app = angular.module('Dinneri', []);
app.controller('showContent', function($scope, $http) {
$http.get("server/showcontent.php")
.success(function (response) {$scope.restaurants = response.records;});
});
And then we have the showcontent.php file to perform the queries and return the data in JSON.
To clarify: The SQL table populated with restaurants is restaurants
and the entries are populated with hashjoins
that include a reference to the restaurant and the hashtag
associated. The hashtag
table includes the actual hashtags. Any tips on shortening the SQL in this conjunction would also be welcomed.
<?php
// Set JSON for Angular
header("Content-Type: application/json; charset=UTF-8");
// Include DB credentials
include('db_con.php');
// Get Restaurants
$result = $mysqli->query("SELECT * FROM `restaurant`");
$outp = "";
while($rs = $result->fetch_array(MYSQLI_ASSOC)) {
if ($outp != "") {$outp .= ",";}
// Add restaurant to array
$outp .= '{"ID":"' . $rs["restaurant_id"] . '",';
$outp .= '"Name":"' . $rs["restaurant_title"] . '",';
$outp .= '"Description":"' . $rs["restaurant_description"] . '",';
$outp .= '"Address":"' . $rs["restaurant_address"] . '",';
$outp .= '"LocLat":"' . $rs["restaurant_loc_lat"] . '",';
$outp .= '"LocLong":"' . $rs["restaurant_loc_long"] . '",';
$outp .= '"Added":"' . $rs["restaurant_added"] . '",';
// Get current while loop restaurant
$restaurant = $rs["restaurant_id"];
// Get joined hashtags
$result_hash = $mysqli->query("SELECT * FROM `hash_join` WHERE `restaurant_hash` = $restaurant");
while($hashjoin = $result_hash->fetch_row())
{
$hashtag_id = $hashjoin[1];
// Get hashtag information
$hashtags = $mysqli->query("SELECT * FROM `hashtags` WHERE `hashtag_id` = $hashtag_id ORDER BY `restaurant_type` DESC");
while($hash = $hashtags->fetch_row())
{
// Add hashtags to array
$outp .= '"HashtagID":"' . $hash[0] . '",';
$outp .= '"HashtagName":"' . $hash[1] . '",';
$outp .= '"HashtagType":"' . $hash[2] . '"}';
}
}
}
// Finalize output array
$outp ='{"records":['.$outp.']}';
// Deliver output array
echo($outp);
// Close mysqli
$mysqli->close();
?>
The first set of results are returned to Angular like expected and all code works until i try to output the "Hashtags". The PHP page "showcontent.php" outputs the JSON code as expected but for some reason the Angular controller is being picky about the hashtags.
Deleting this section of the code where the hashtags are outputted makes everything work like a charm, but without the desired output.
Thank you in advance <3