doudao9915 2017-03-30 15:00
浏览 34
已采纳

将Datatables排序添加到PHP页面

I have tried to add datatables to my PHP web apps table. I have followed the instructions and add a thead and tbody tag, also the link at the top and the document.ready in the script tags at the bottom. Nothing seems to be working. any ideas?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>

<head>
    <link rel="stylesheet" type="text/css" href="css/style.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script type="text/javascript" src="jquery-1.4.2.min.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    <script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.13/js/jquery.dataTables.js"></script>

<title>View Records</title>

</head>

<body>
    
    <div class="page-wrap">
    
    <center><a href="https://repo-project-danielmurran.c9users.io/view.php"><img src="/images/logo.png" alt="what image shows" height="90" width="300"></a></center>
    
    <h1>GCS Reproduction Environment</h1></br>
    
    <hr/>

    <h2>All Reproductions</h2>

<?php

/*

VIEW.PHP

Displays all data from 'players' table

*/


// connect to the database

include('php/connect-db.php');

// ordering the ID function

$ascdesc = "";
$order = isset($_GET['sort'])?$_GET['sort']:'id';

$ascdesc = isset($_GET['ascdesc'])?$_GET['ascdesc']:'DESC';
switch(strtoupper($ascdesc))
  {
  case 'DESC': $ascdesc = 'ASC'; break;
  case 'ASC': $ascdesc = 'DESC'; break;
  default: $ascdesc = 'DESC'; break;
  }


// get results from database

$result = mysql_query("SELECT * FROM players ORDER BY $order $ascdesc")

or die(mysql_error());



// display data in table


echo "<table id='#myTable' align='center' border='1' cellpadding='2'>";
echo "<thead> <tr> <th>ID</th> <th>Administration Console</th> <th>Product Version</th> <th>Platform</th> <th>Database</th> <th>Owner</th> <th>Status</th> <th></th> <th></th></tr> </thead>";




// loop through results of database query, displaying them in the table

while($row = mysql_fetch_array( $result )) {



// echo out the contents of each row into a table
echo "<tbody>";

echo "<tr>";

echo '<th>' . $row['id'] . '</th>';

echo '<td><a href="'.$row['curl'].'">'.$row['curl'].'</a></td>';

echo '<td>' . $row['pversion'] . '</td>';

echo '<td>' . $row['platform'] . '</td>';

echo '<td>' . $row['dversion'] . '</td>';

echo '<td><a href="mailto:'.$row['email'].'@informatica.com">' . $row['email'].'</a></td>';

echo '<td>' . $row['status'] .'</td>';

echo '<td><a href="php/edit.php?id=' . $row['id'] . '">Edit</a></td>';

echo '<td><a onclick="javascript:confirmationDelete($(this));return false;" href="php/delete.php?id=' . $row['id'] . '"onclick="return confirm("Do you want to delete this?")">Delete</a></td>';

echo "</tr>";

echo "</tbody>";

}





// close table>

echo "</table>";

?>

<script>
    function confirmationDelete(anchor)
{
   var conf = confirm('Are you sure want to delete this record?');
   if(conf)
      window.location=anchor.attr("href");
}


$("tr").not(':first').hover(
  function () {
    $(this).css("background","yellow");
  },
  function () {
    $(this).css("background","");
  }
);

$(document).ready(function(){
    $('#myTable').DataTable();
});

</script>



<p><a href="php/new.php">Add a new record</a></p>


</div>
</body>

</html>

</div>
  • 写回答

2条回答 默认 最新

  • dourukeng5302 2017-03-30 15:13
    关注

    You don't have to use # to specify ID. Change the following echo "<table id='#myTable' align='center' border='1' cellpadding='2'>";
    to echo "<table id='myTable' align='center' border='1' cellpadding='2'>";

    Also, you are including multiple versions of jQuery. You may only include <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> Note that the original mysql extension is deprecated. (http://php.net/manual/en/migration55.deprecated.php)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大