moh*_*abk 5 javascript php arrays angularjs
尝试使用 AngularJS 和 PHP 进行 CRUD。
这是我目前的角度函数;
$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);
});
}
Run Code Online (Sandbox Code Playgroud)
该函数应该将 restID 发布到 PHP,根据逻辑从 PHP 获取数据并添加到$scope.cart
其中应该是包含我可以使用的所有项目的数组ng-repeat
我的 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;
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我目前拥有的;有人可以向我解释这一行是否正确;
while($nrow = mysqli_fetch_assoc($result)) {
$data['data'][] = $nrow;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用while
. 我要返回的内容是这样的;
item1.name, item1.price, item1.qty, item1.id
item2.name, item2.price, item2.qty, item2.id
Run Code Online (Sandbox Code Playgroud)
我的目标是获取所有这些项目,将它们添加到$scope.cart
并用于ng-repeat
显示它们。我不太擅长数组。我没有得到任何数据$scope.cart
I'm I data to it wrong?
小智 0
我认为你让事情变得比他们需要的更复杂。少即是多。
首先,为什么不通过控制台日志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 script...
});
Run Code Online (Sandbox Code Playgroud)
其次,你的 php.ini 中的重复代码让你很头疼。为什么不在一行中渲染 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);
}
Run Code Online (Sandbox Code Playgroud)
如果你保持事情的精致化,你就会对解决问题有更多的控制力。希望这能引导您走向正确的方向。
归档时间: |
|
查看次数: |
494 次 |
最近记录: |