Hello,
I am trying to dynamically re-order the results from a php script using AJAX. Trouble is I don't know how to keep track of the value of the order. For instance, if it was set to ASC last time, set it to DESC and vice versa.
Here is my HTML:
<div id="order_links_div">
<a href="#" name="DEFAULT" onmouseup="showUser('DEFAULT')">DEFAULT</a>
</div>
<div id='members_div'>
<table id="header_table">
<tr>
<th>Members No.</th>
<th><a href="#" onmouseup="showUser('username')">Username</a></th>
<th><a href="#" onmouseup="showUser('join_date')">Join Date</th>
</tr>
</table>
<table id="results_table">
<?php require("sort.php"); ?>
</table>
</div>
It takes the column name and is passed as a parameter to the ajax function which calls the sort.php script and passes in this value along in the url everytime it is clicked and the query in the sort.php orders them by database table name...
Here is my Ajax function:
function showUser(col) {
if (col=="") {
document.getElementById("results_table").innerHTML=xmlhttp.responseText;
}
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
document.getElementById("results_table").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","sort.php?col="+col,true);
xmlhttp.send();
}
So at this point, I can switch the columns no problem and they order correctly. I also 'require' the sort.php in my html file so that something is displayed if nothing has been clicked yet, as you can see in the above code.
But I'm not sure at what point in my sort.php script do I create the session. Here is my sort.php:
<?php
require("connectdb.php");
$asc = "ASC";
$desc = "DESC";
$_SESSION['order'] = $desc;
function order(){
global $asc;
global $desc;
static $counter = 1;
if($counter++){
if($_SESSION['order'] == $asc){
$_SESSION['order'] = $desc;
return;
}
else{
$_SESSION['order'] = $asc;
return;
}
}
}
if(!empty($_GET['col']) && $_GET['col'] != 'DEFAULT'){
$column = $_GET['col'];//holds value which column was clicked.
order();
$order = $_SESSION['order'];
$sql = mysql_query("SELECT * FROM members ORDER BY {$column} {$order}");
if(!$sql){
die("error with query");
}
while ($row = mysql_fetch_array($sql)){
echo "<tr><td>".$row['id']."</td><td>".$row['username']."</td><td>".$row['join_date']."</td></tr>";
}
order();
}
else if(!empty($_GET['col']) && $_GET['col'] == 'DEFAULT') {
$sql = mysql_query("SELECT * FROM members");
if(!$sql){
die("error with default query");
}
while ($row = mysql_fetch_array($sql)){
echo "<tr><td>".$row['id']."</td><td>".$row['username']."</td><td>".$row['join_date']."</td></tr>";
}
}
else {
$sql = mysql_query("SELECT * FROM members");
$_GET['col'] = 'undefined';
if(!$sql){
die("error with onload query");
}
while ($row = mysql_fetch_array($sql)){
echo "<tr><td>".$row['id']."</td><td>".$row['username']."</td><td>".$row['join_date']."</td></tr>";
}
}
?>
Do I create it at the very top of the script or within the first if statement (seeing as though we know the user has clicked something...)?
I would really appreciate some help with this. Been racking my brains and reading sites etc for days and cannot figure out how to change the order depending on the last order..
I thought about using a for loop to keep track of how many times the sort.php script had been accessed, but couldn't figure out how to build it.
Kind regards,
Labtec.