2014-10-05 169 views
0

我想從數據庫中提取記錄並將其顯示在數據表中,但是當我在瀏覽器中運行此PHP代碼時,它只在簡單的表中顯示記錄。我想要這個結果在jQuery數據表中進行搜索和排序。在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> 
+1

mysql_ *函數已棄用。改用mysqli_ *或PDO。 – FAS 2014-10-05 09:20:41

+0

@Fabian,mysql_ *函數在PHP 5.5.0中被棄用,它仍然只被所有PHP安裝中的約4%所使用。 OP的機會是使用小於5.5.0的PHP,因此正在使用PHP,其中mysql_ *是**而不是棄用是非常高的。 – davidkonrad 2014-10-06 07:59:51

+0

它仍然被棄用,即不鼓勵使用。當開始一個新的項目時可能會有一段時間,但不要依賴已棄用的方法。特別是在這種情況下,在你的方法調用中添加一個簡單的'i'就足夠了。 – FAS 2014-10-06 08:27:14

回答

1

儘量保持 'TBODY' 圈外:

<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調用獲取數據

+0

很抱歉地說,但我不明白如何離開''應該有所作爲。 – davidkonrad 2014-10-06 08:04:31

+1

它的原因是它應該只是一個'表'內的'tbody',並且你的循環會導致它有多個'tbody的' – 2014-10-06 08:51:18

+0

哦,是的 - 你完全正確!感謝您清除您的答案的理由! – davidkonrad 2014-10-06 08:59:50

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>