我正在尝试为我的基础学习php项目实现购物车。我正在使用这个:Running Example工具来简化它。该工具需要一个json工作,我很难从数据库中获取我的自定义产品,并尝试将它们编码为json。这就是我目前所拥有的:
<?php
// A collection of sample products
require_once "connect.php";
$return_arr = array();
$query = "SELECT * FROM products";
$stmt = $pdo->prepare($query);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$row_array['id'] = $row['id'];
$row_array['name'] = $row['name'];
$row_array['description'] = $row['description'];
$row_array['price'] = $row['price'];
$row_array['picture'] = $row['picture'];
$row_array['quantity'] = $row['quantity'];
array_push($return_arr,$row_array);
}
json_encode($return_arr);
echo "<div>$return_arr</div>";
$products = json_decode("{$return_arr}", true);
// Page
$a = (isset($_GET['a'])) ? $_GET['a'] : 'home';
require_once 'class.Cart.php';
// Initialize cart object
$cart = new Cart([
// Maximum item can added to cart, 0 = Unlimited
'cartMaxItem' => 0,
// Maximum quantity of a item can be added to cart, 0 = Unlimited
'itemMaxQuantity' => 5,
// Do not use cookie, cart items will gone after browser closed
'useCookie' => false,
]);
// Shopping Cart Page
if ('cart' == $a) {
$cartContents = '
<div class="alert alert-warning">
<i class="fa fa-info-circle"></i> There are no items in the cart.
</div>';
// Empty the cart
if (isset($_POST['empty'])) {
$cart->clear();
}
// Add item
if (isset($_POST['add'])) {
foreach ($products as $product) {
if ($_POST['id'] == $product->id) {
break;
}
}
$cart->add($product->id, $_POST['qty'], [
'price' => $product->price,
'color' => (isset($_POST['color'])) ? $_POST['color'] : '',
]);
}
// Update item
if (isset($_POST['update'])) {
foreach ($products as $product) {
if ($_POST['id'] == $product->id) {
break;
}
}
$cart->update($product->id, $_POST['qty'], [
'price' => $product->price,
'color' => (isset($_POST['color'])) ? $_POST['color'] : '',
]);
}
// Remove item
if (isset($_POST['remove'])) {
foreach ($products as $product) {
if ($_POST['id'] == $product->id) {
break;
}
}
$cart->remove($product->id, [
'price' => $product->price,
'color' => (isset($_POST['color'])) ? $_POST['color'] : '',
]);
}
if (!$cart->isEmpty()) {
$allItems = $cart->getItems();
$cartContents = '
<table class="table table-striped table-hover">
<thead>
<tr>
<th class="col-md-7">Product</th>
<th class="col-md-3 text-center">Quantity</th>
<th class="col-md-2 text-right">Price</th>
</tr>
</thead>
<tbody>';
foreach ($allItems as $id => $items) {
foreach ($items as $item) {
foreach ($products as $product) {
if ($id == $product->id) {
break;
}
}
$cartContents .= '
<tr>
<td>'.$product->name.((isset($item['attributes']['color'])) ? ('<p><strong>Color: </strong>'.$colors[$item['attributes']['color']].'</p>') : '').'</td>
<td class="text-center"><div class="form-group"><input type="number" value="'.$item['quantity'].'" class="form-control quantity pull-left" style="width:100px"><div class="pull-right"><button class="btn btn-default btn-update" data-id="'.$id.'" data-color="'.((isset($item['attributes']['color'])) ? $item['attributes']['color'] : '').'"><i class="fa fa-refresh"></i> Update</button><button class="btn btn-danger btn-remove" data-id="'.$id.'" data-color="'.((isset($item['attributes']['color'])) ? $item['attributes']['color'] : '').'"><i class="fa fa-trash"></i></button></div></div></td>
<td class="text-right">$'.$item['attributes']['price'].'</td>
</tr>';
}
}
$cartContents .= '
</tbody>
</table>
<div class="text-right">
<h3>Total:<br />$'.number_format($cart->getAttributeTotal('price'), 2, '.', ',').'</h3>
</div>
<p>
<div class="pull-left">
<button class="btn btn-danger btn-empty-cart">Empty Cart</button>
</div>
<div class="pull-right text-right">
<a href="?a=home" class="btn btn-default">Continue Shopping</a>
<a href="#" class="btn btn-danger">Checkout</a>
</div>
</p>';
}
}
?>
当我运行代码时,它会给出错误:“在第23行的C:\ xampp \ htdocs \ BeerShop2 \ basket.php中进行字符串转换”。我试图弄清楚出了什么问题,似乎问题在于解码。如何从数据库中获取所有产品的查询,然后使用该工具解码json?