Compile and pass array - AngularJS & PHP

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) {
      //$scope.messageFailure(data.message);
   });
}

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");

        // Insert cart into DB
        $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?

+4
1

, , . .

-, data, , - :

$http({
    url: url,
    method: 'post',
    data: $.param({restID: $scope.restID , type: 'get_cart'}),
    headers: {Content-Type: 'application/x-www-form-urlencoded'}
})
.success(function(data, status, headers, config)
{
    console.log('success', data);

    // now we know what we're working with...
})
.error(function(data, status, headers, config)
{
    console.log('failure', data);

    // welp, we probably cocked up our PHP ...
});

-, php. json , , :

/**
 * Echo cart data given the connection.
 *
 * @param object $connection
 * @param array $data
 * @return void
 */
function get_cart($connection, $data = [])
{
    try
    {
        if (isset($_COOKIE['basket']))
        {
            // manipulate data array.
        }
        else
        {
            // manipulate data array.
        }
    }
    catch (Exception $e)
    {
        // manipulate data array.
    }

    echo json_encode($data);
}

, . , .

0

Source: https://habr.com/ru/post/1616534/


All Articles