如何在javascript中声明php数组变量并查看数据表
Posted
技术标签:
【中文标题】如何在javascript中声明php数组变量并查看数据表【英文标题】:how to declare php array variable in javascript and view on datatable 【发布时间】:2017-01-29 05:22:15 【问题描述】:我在 $dispatch
变量中获取数组值,并在 javascript var dataSet3=<?php echo json_encode($dispatch); ?>;
中传递它,但是当数组包含两行时,我的数据表只显示一行。
SQL 查询:
$userSQL3 = "SELECT magazine.name_txt, dispatch.dispatch_id, dispatch.dispatch_dt, dispatch.notes_txt, dispatch_details.created_dt FROM dispatch_details left join dispatch on dispatch_id=fk_dispatch_id LEFT JOIN magazine ON magazine.magazine_id = dispatch.fk_magazine_id WHERE fk_subscriber_id = ". $subscriber_id;
$usersResult3 = mysqli_query($dbConn, $userSQL3);
while($userResult3 = mysqli_fetch_array($usersResult3))
$dispatch=array($userResult3) ;
echo"<pre>";
print_r($dispatch);
echo"</pre>";
JavaScript 代码:
var dataSet3=<?php echo json_encode($dispatch); ?>;
$(document).ready(function()
$('#dispatch').DataTable(
searching: false,
paging:false,
bLengthChange:false,
data: dataSet3,
columns: [
data: "fk_dispatch_id" ,"visible": false,
data: "fk_subscriber_id" ,"visible": false ,
data: "created_dt" ,
data: "updated_dt" ,"visible": false,
data: "dispatch_id" ,
data: "dispatch_dt" ,
data: "fk_publisher_id","visible": false ,
data: "name_txt" ,
data: "notes_txt" ,
data: "created_dt" ,"visible": false ,
data: "updated_dt" ,"visible": false
],
);
);
【问题讨论】:
@Anant 在数组视图中获取值但数据表未显示值! var dataset3 是否包含所需的 json? @Anant 使用 ajax 的链接,但我直接将值 js 传递给数据表:- datatables.net/examples/data_sources/js_array.html @ArunKumaresh 对不起,我是初学者,所以更了解这样的 json @Anant 是的,我收到以下错误:-*sub.php:111 GET https:cdn.datatables.net/1.10.12/css/jquery.dataTa%3Chead%3Ebles.min.css sub .php 是我的文件名 【参考方案1】:使用下面的更正代码。
PHP
$userSQL3 = "SELECT magazine.name_txt, dispatch.dispatch_id, dispatch.dispatch_dt, dispatch.notes_txt, dispatch_details.created_dt FROM dispatch_details left join dispatch on dispatch_id=fk_dispatch_id LEFT JOIN magazine ON magazine.magazine_id = dispatch.fk_magazine_id WHERE fk_subscriber_id = ". $subscriber_id;
$usersResult3 = mysqli_query($dbConn, $userSQL3);
$dispatch = array();
while($userResult3 = mysqli_fetch_array($usersResult3, MYSQLI_ASSOC))
array_push($dispatch, $userResult3);
【讨论】:
以上是关于如何在javascript中声明php数组变量并查看数据表的主要内容,如果未能解决你的问题,请参考以下文章
javascript中是不是有相当于“新数组(数字)”的PHP?
如何在函数外部使用函数的声明变量但将它们的值保留在 javascript 中?
如何在javascript中插入带有innerHTML的php数组