doudao9915
doudao9915
2017-03-30 15:00

将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 dourukeng5302 4年前

    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)

    点赞 评论 复制链接分享
  • duancongjue9202 duancongjue9202 4年前

    Your biggest issue here is you're not calling your database in your query. It should be mysqli_query($db, "SELECT * FROM players ORDER BY $order $ascdesc") where $db is your mysqli_connect variable called in your connect-db.php file. Also your mysqli_error() should be mysqli_error($db) or whatever you called your mysqli_connect. On doing that, the mysqli_error will give you a better idea of where you're failing. You can also check your php log file to catch errors there.

    点赞 评论 复制链接分享