0
Ajaxの機能でフェッチされたmy mysqlの結果のソートオプションを追加する方法を見つけようとしています。 mysqlの列などで並べ替えます。以下は、ソートオプションを実装したいと思ういくつかの "th"セクションです。Ajax MYSQLの検索とソートの結果
HTMLページ
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tool details</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<input type="text" name="search_text" id="search_text" placeholder="Search for tool details" class="form-control" />
<div id="result"></div>
<script>
$(document).ready(function(){
load_data();
function load_data(query)
{
$.ajax({
url:"fetch.php",
method:"POST",
data:{query:query},
success:function(data) {
$('#result').html(data);
}
});
}
$('#search_text').keyup(function(){
var search = $(this).val();
if(search != '') {
load_data(search);
} else {
load_data();
}
});
});
</script>
</body>
</html>
そのソートは、ここでのコードを複雑避けるために、現時点で唯一のID列に追加されていないしてください。
fetch.php
<?php
//fetch.php
include($_SERVER['DOCUMENT_ROOT'].'/db/connect-db.php');
$output = '';
if(isset($_POST["query"])) {
$search = mysqli_real_escape_string($connection, $_POST["query"]);
$query = "SELECT * FROM tools
WHERE toolsn LIKE '%".$search."%'
OR toolcategory LIKE '%".$search."%'
OR tooldesc LIKE '%".$search."%'
OR toolpn LIKE '%".$search."%'
OR toolstatus LIKE '%".$search."%'";
} else {
$query = "SELECT * FROM tools ORDER BY id";
}
$result = mysqli_query($connection, $query);
//get feedback why database not working
if (!$result) {
printf("Error: %s\n", mysqli_error($connection));
exit();
}
if(mysqli_num_rows($result) > 0) {
$output .= '
<div class="table-responsive">
<table class="table table bordered">
<tr>
<th>Customer Name</th>
<th>Address</th>
<th>City</th>
<th>Postal Code</th>
<th>Country</th>
</tr>';
while($row = mysqli_fetch_array($result)) {
$output .= '<tr>
<td>'.$row["tooldesc"].'</td>
<td>'.$row["toolcategory"].'</td>
<td>'.$row["toolsn"].'</td>
<td>'.$row["toolpn"].'</td>
<td>'.$row["toolstatus"].'</td>
</tr>';
}
echo $output;
}else {
echo 'Data Not Found';
}
?>