将Datatables排序添加到PHP页面

时间:2017-03-30 15:00:29

标签: javascript php

我曾尝试将数据表添加到我的PHP网络应用表中。我按照说明添加了一个thead和tbody标签,顶部的链接和底部的脚本标签中的document.ready。似乎没有什么工作。任何想法?



<!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>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您不必使用#来指定ID。更改以下内容 echo "<table id='#myTable' align='center' border='1' cellpadding='2'>";
至     echo "<table id='myTable' align='center' border='1' cellpadding='2'>";

此外,您还包含多个版本的jQuery。你可能只包括     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 请注意,不推荐使用原始的mysql扩展。 (http://php.net/manual/en/migration55.deprecated.php

答案 1 :(得分:0)

这里最大的问题是你没有在查询中调用数据库。它应该是mysqli_query($ db,“SELECT * FROM players ORDER BY $ order $ ascdesc”)其中$ db是你的connect-db.php文件中调用的mysqli_connect变量。你的mysqli_error()也应该是mysqli_error($ db)或者你所谓的mysqli_connect。在这样做时,mysqli_error将让您更好地了解失败的位置。您还可以检查您的php日志文件以捕获那里的错误。