Jump to content

Recommended Posts

hi guys,

 

ok i have a table and want the table headers to be links so that when they are clicked they change the order of the table to ASC then DESC and back again, and so on and so on.

 

Can anyone help? i can get the table to switch from ASC to DESC but not back again ?

 

Here is the code i have atm i know its not right but im trying to keep things as simple as i can.

 

 

<?php
$sql = mysql_query("SELECT* FROM table WHERE surname LIKE '$selection%' AND link ='1' ORDER BY surname ASC"); 
?>
<table border='1' width =''>
<tr>
<th><a href ="test.php?sort=1<?php //echo $selection;?>">name</a></th>
<th>ref</th>
<th>DOB</th>
</tr>

<?php


$sort = $_GET['sort'];


if  ($sort = $sort)
$sql=  mysql_query("SELECT* FROM table WHERE surname LIKE '$selection%' AND link ='1' ORDER BY surname DESC");
else
$sql = mysql_query("SELECT* FROM table WHERE surname LIKE '$selection%' AND link ='1' ORDER BY surname ASC");

?>

 

Any help would be good. I know i might be going about it all wrong.....

if  ($sort = $sort)

 

That is obviously always true.

 

On the side note: you might try one of several datagrid widgets, that have this functionality built in.

sorry should of been  ($sort == '1')

 

need to work out how to change th><a href ="test.php?sort=1 to 0 every other time its clicked or something like that

<?php
session_start();

if(isset($_GET['togglesort'])) {
$_SESSION['sortDir'] = 1 - $_SESSION['sortDir'];  //toggle sort order
} else {
$_SESSION['sortDir'] = 0  //default sort order
}

if ( $_SESSION['sortDir'] = 0) {
  $sortDir = "ASC";
} else {
  $sortDir = "DESC";
}

$query = "SELECT * FROM table WHERE surname LIKE '$selection%' AND link ='1' ORDER BY surname $sortDir";
//fetch data

?>
//display your table
<table border='1' width =''>
<tr>
   <th><a href ="test.php?togglesort=1">name</a></th>
   <th>ref</th>
   <th>DOB</th>
</tr>

 

<?php
session_start();
if ($_GET['dir']) {
   $_SESSION['dir'] = ($_SESSION['dir'] == 'ASC')? 'DESC' : 'ASC';
}	 

 

Cheers for all the help guys ended up using this code

 

if(isset($_GET["sort"]))

$sort = ($_GET["sort"]=="DESC")?"ASC":"DESC";

else

{

$sort="ASC";

}

<?php
session_start();
if ($_GET['dir']) {
   $_SESSION['dir'] = ($_SESSION['dir'] == 'ASC')? 'DESC' : 'ASC';
}	 

 

Cheers for all the help guys ended up using this code

 

if(isset($_GET["sort"]))

$sort = ($_GET["sort"]=="DESC")?"ASC":"DESC";

else

{

$sort="ASC";

}

 

Did you even bother to try that?  If you set the default to ASC it will sort as ASC on first page load.  If you make your link pass ASC it will switch sort as DESC if you click the link.  Now click the link again and guess what, it sorts it as DESC again. And DESC again.  And if you change your link to pass DESC, it will just keep sorting as ASC over and over and over.

 

In order for 'your script' to 'remember' whether to turn the proverbial light switch on or off, it has to know what state it's currently in (on or off).  But clicking on a link makes a new request to the server.  As far as php and your script is concerned, it's a completely new request that has nothing to do with the previous request.  There is no 'state' to speak of. 

 

In order for a single link 'switch' to work, you have to tell it what state the switch was in, before you clicked the link and restarted the whole script.  In order to do that, you need to use some means of data persistence.  You can do this with a cookie, but it's easier and less hassle to do this with a session variable, as shown to you by multiple people.

I did try it yeh, and i am really greatful for all the help. i only posted up what i come up with in case any one else finds this post. I see where your coming from and i am still working on the page so it could well change.

 

Thanks again for all the good help :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.