我正处于一片混乱之中,因为我正以非正统的方式工作。我正在使用DataTables进行搜索和分页。我有一张桌子,其中我收到供应商的详细信息,如姓名,电子邮件,地址,国家等,以及供应商处理的材料。我使用两个表的内连接获得这些结果。在我的场景中,供应商可以处理多种材料,如屏幕截图所示。
以下是我的表格视图的屏幕截图:
现在问题是我想要每个td中每个供应商的材料,但在我的情况下,每个td中显示的每个材料显然都在破坏我的桌面设计。
这是我的HTML和JavaScript:
<table id="employee-grid" class="display" width="100%">
<thead>
<tr>
<th>Email</th>
<th>Country</th>
<th>Region</th>
<th>Material</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
</table>
<script type="text/javascript" language="javascript" src="js/jquery.js"</script>
<script type="text/javascript" language="javascript" src="js/jquery.dataTables.js"></script>
<script type="text/javascript" language="javascript" >
$(document).ready(function() {
var dataTable = $('#employee-grid').DataTable( {
"processing": true,
"serverSide": true,
"ajax":{
url :"employee-grid-data.php",
}
} );
} );
</script>
这是我的ajax文件:
<?php
/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "phwdata";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
/* Database connection end */
// storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;
//print_r($_REQUEST);
$columns = array(
// datatable column index => database column name
0 =>'name',
1 => 'email',
2=> 'country',
3=> 'region',
4=> 'material'
);
// getting total number records without any search
$sql = "SELECT *";
$sql.=" FROM bp";
$sql.=" INNER JOIN pdbp";
$sql.=" ON (bp.id=pdbp.bp_id)";
$query=mysqli_query($conn, $sql) ;
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.
$query=mysqli_query($conn, $sql) ;
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$data = array();
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$nestedData=array();
$nestedData[] = $row["name"];
$nestedData[] = $row["email"];
$nestedData[] = $row["country"];
$nestedData[] = $row["region"];
$mat = $row['material_cod'];
$material=explode(",",$mat);
foreach($material as $materials){
$sql2 = "SELECT material";
$sql2.=" FROM product_material";
$sql2.=" WHERE material_cod = '$material'";
$query2=mysqli_query($conn, $sql2);
while( $row=mysqli_fetch_array($query2)) {
$nestedData[] = $row['material'];
}
}
$data[] = $nestedData;
}
我也在DataTables nested object中浏览嵌套对象,但我无法找到解决方案。
答案 0 :(得分:0)
最后我得到了解决方案,我在foreach循环中声明了数组。在循环外声明数组修复我的问题。 :)