我创建了一个 ajax json 响应,它将显示在我的 dataTable jquery 插件中。表的 id 是#dataTable。
这是 dataTable 插件的代码:
$(document).ready(function() {
var productTable = $("#dataTable").DataTable({
"ajax": "../api/ajax/getProduct.php",
"order": [[ 1, "desc" ]]
});
});
这是 getProduct.php
<?php
include_once('../../components/db.php');
$sqlb = "SELECT * FROM products WHERE status='active'";
$resultb = $conn->query($sqlb);
$data = $resultb->fetch_assoc();
$result = array();
foreach ($data as $key => $value) {
$image = '<img width="50" height="40" class="rounded-circle" src="../' . $value['image'] . '; ?>">';
$buttons = '<a href="product-update.php' . $value["id"] . '" class="btn btn-info btn-sm"><i class="fa fa-edit" aria-hidden="true"></i></a><a onclick="removeProduct(' . $value["id"] . ')" class="btn btn-danger btn-sm"><i class="fa fa-trash" aria-hidden="true"></i></a>';
$result[$key] = array(
$value["description"],
$value["price"],
$image,
$value["availability"],
$buttons,
);
}// /foreach
echo json_encode($result);
?>
这是我检查 XHR 时遇到的错误
Warning: Illegal string offset 'image' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 11
Warning: Illegal string offset 'id' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 13
Warning: Illegal string offset 'id' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 13
Warning: Illegal string offset 'description' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 16
Warning: Illegal string offset 'price' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 17
Warning: Illegal string offset 'availability' in C:\xampp\htdocs\copy\api\ajax\getProduct.php on line 19
这是我在页面加载时从数据表本身获得的弹出错误。
DataTables warning: table id=dataTable - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1
似乎这里真正的问题是编码数据本身是错误的。由于此错误,我无法将数据显示到 dataTable 本身中。
最佳答案
基于 var_dump($data);
结果:
array(9) {
["id"]=>
string(1) "2"
["names"]=>
string(12) "Fruity Split"
["price"]=>
string(5) "50.00"
["qty"]=>
string(1) "1"
["image"]=>
string(26) "images/products/menu-2.jpg"
["description"]=>
string(90) "Dessert made with a split banana, ice cream, sauce, whipped cream, nuts, and a strawberry."
["category"]=>
string(7) "dessert"
["availability"]=>
string(9) "Available"
["status"]=>
string(6) "active"
}
它给你这个错误的原因:
Warning: Illegal string offset
那是因为你只是在循环 1 排 .
而是将您的代码更改为:
<?php
include_once('../../components/db.php');
$sqlb = "SELECT * FROM products WHERE status='active'";
$resultb = $conn->query($sqlb);
// This just return single row
// $data = $resultb->fetch_assoc();
$result = array();
//Use while instead of foreach
while ($value = $resultb->fetch_assoc()) {
$image = '<img width="50" height="40" class="rounded-circle" src="../' . $value['image'] . '; ?>">';
$buttons = '<a href="product-update.php' . $value["id"] . '" class="btn btn-info btn-sm"><i class="fa fa-edit" aria-hidden="true"></i></a><a onclick="removeProduct(' . $value["id"] . ')" class="btn btn-danger btn-sm"><i class="fa fa-trash" aria-hidden="true"></i></a>';
// Add Keys For DataTable column
$result[] = array(
'description' => $value["description"],
'price' => $value["price"],
'image' => $image,
'availability' => $value["availability"],
'buttons' => $buttons,
);
}
echo json_encode($result);
?>
关于php - 为dataTable jquery插件获取数组时出现"Illegal string offset"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58742475/