我想从数据库中获取记录并将其显示在数据表中,但是当我在浏览器中运行此PHP代码时,它只在简单的表中显示记录。我希望这个结果在jquery数据表中进行搜索和排序。
数据表的javascript函数(使用jquery.dataTables.js)
<script type="text/javascript" language="javascript">
$(document).ready(function() {
('#datatable').DataTable();
})
</script>
用于获取记录的mysql函数
<?php
$con=mysql_connect("localhost","root","");
mysql_select_db("runindia",$con);
$query="select *from admin_login";
$rs=mysql_query($query,$con);
?>
<div class="container">
<table class="table table-bordered table-responsive table-hover display" id="datatable">
<thead>
<th> Admin ID</th>
<th> User Name</th>
<th> First Name</th>
<th> Last Name</th>
<th> Email</th>
</thead>
<?php
while($r=mysql_fetch_array($rs))
{ ?>
<tbody>
<tr>
<td><?php echo $r['admin_id'];?></td>
<td><?php echo $r['username'];?></td>
<td><?php echo $r['first_name'];?></td>
<td><?php echo $r['last_name'];?></td>
<td><?php echo $r['email'];?></td>
</tr>
</tbody>
<?php
} //closing while
mysql_close($con);//mysql connection close
?>
</table>
答案 0 :(得分:1)
尝试保持&#39; tbody&#39;走出循环:
<table class="table table-bordered table-responsive table-hover display" id="datatable">
<thead>
<th> Admin ID</th>
<th> User Name</th>
<th> First Name</th>
<th> Last Name</th>
<th> Email</th>
</thead>
<tbody>
<?php
while($r=mysql_fetch_array($rs))
{ ?>
<tr>
<td><?php echo $r['admin_id'];?></td>
<td><?php echo $r['username'];?></td>
<td><?php echo $r['first_name'];?></td>
<td><?php echo $r['last_name'];?></td>
<td><?php echo $r['email'];?></td>
</tr>
<?php
} //closing while
mysql_close($con);//mysql connection close
?>
</tbody>
</table>
或尝试更好的方法,例如通过JSON格式的AJAX调用获取数据
答案 1 :(得分:0)
<?php
$con=mysqli_connect("localhost","root","", "runindia");
$query="select * from admin_login";
$rs=mysqli_query($con, $query);
?>
<div class="container">
<table class="table table-bordered table-responsive table-hover display" id="datatable">
<thead>
<th> Admin ID</th>
<th> User Name</th>
<th> First Name</th>
<th> Last Name</th>
<th> Email</th>
</thead>
<?php
while($r = mysqli_fetch_array($rs, MYSQLI_ASSOC))
{ ?>
<tbody>
<tr>
<td><?php echo $r['admin_id'];?></td>
<td><?php echo $r['username'];?></td>
<td><?php echo $r['first_name'];?></td>
<td><?php echo $r['last_name'];?></td>
<td><?php echo $r['email'];?></td>
</tr>
</tbody>
<?php
} //closing while
mysqli_close($con);//mysqli connection close
?>
</table>