Trying to find out how to add sort option of my mysql results that are fetched with Ajax functionality. Sorting by mysql columns or something. Below are some "th" sections where I would like to have sorting options implemented.
HTML page
<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>
Please not that sorting is added only for ID column at the moment to avoid complicating code here.
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';
}
?>