Trying to do CRUD with AngularJS and PHP.
This is my current angular function;
$scope.initCart = function () {
$http({
method: 'post',
url: url,
data: $.param({ 'restID' : $scope.restID , 'type' : 'get_cart' }),
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).
success (function(data, status, headers, config){
if(data.success && !angular.isUndefined(data.data) ){
$scope.cart = data.data;
}else {
$scope.cart = [];
}
}).
error(function(data, status, headers, config) {
});
}
The function should send restID to PHP, get data from PHP based on logic and add to $scope.cart, which should be an array containing all the elements that I can access usingng-repeat
And my PHP:
function getCart ($connection) {
$data = array();
$rest_id = $_POST['restID'];
try {
if(isset($_COOKIE['basket'])) {
$cookie_id = $_COOKIE['basket'];
$query = "SELECT * FROM cart WHERE cookie_id = $cookie_id";
$result = mysqli_query($connection, $query);
$count = mysqli_num_rows($result);
if($count > 0) {
$row = mysqli_fetch_assoc($result);
$cart_id = $row['id'];
$cart_total = $row['total'];
$cart_subtotal = $row['subtotal'];
$query = "SELECT * FROM cart_items WHERE cart_id = $cart_id";
$result = mysqli_query($connection, $query);
while($nrow = mysqli_fetch_assoc($result)) {
$data['data'][] = $nrow;
}
$data['success'] = true;
echo json_encode($data);
exit;
}
} else {
setcookie('basket', session_id(), time()+604800, "/");
$cookie = $_COOKIE['basket'];
$cookie_id = session_id();
$timestamp = date("H:i:s");
$query = "INSERT INTO `cart`(`cookie_id`, `restaurant_id`, `timestamp`) VALUES ('$cookie_id', '$rest_id', '$timestamp')";
$result = mysqli_query($connection, $query);
if ($result) {
$data['success'] = false;
echo json_encode($data);
exit;
}
}
} catch (Exception $e) {
$data = array();
$data['success'] = false;
$data['message'] = $e->getMessage();
echo json_encode($data);
exit;
}
}
This is what I have now; Can someone explain to me if this line is correct,
while($nrow = mysqli_fetch_assoc($result)) {
$data['data'][] = $nrow;
}
I am returning items from my DB using while. What I am returning is as follows:
item1.name, item1.price, item1.qty, item1.id
item2.name, item2.price, item2.qty, item2.id
My goal is to get all these elements, add them to $scope.cartand use ng-repeatto display them. I am not very good with arrays. I do not receive any data in $scope.cart
Am I mistaken in data?