Jump to content

Sorting two tables from MYSQL-query


stirrah

Recommended Posts

Hey!

 

I have two querys that are fetching data. Each query is populating a html-table.

 

I want to be able to order this data if i click on the header. I've managed to get this to work, BUT I want the first table to "remember" its sorting even if I sort the other table.

 

Look at the attached picture.

 

This is the code for the link:

<th><?php echo "<a href='my_projects.php?order=" . 'projectId' . "'>" . 'ID' . "</a>"; ?></th>

This is the code for GET value:

$order = $_GET["order"];
if (empty($order)) {
  $order = 'projectId';
}

So I think my problem is that every time I reload the page, the value gets resetted to 'projectId'...how can I make the page remember?

post-165405-0-96679900-1389022926_thumb.jpg

Edited by stirrah
Link to comment
Share on other sites

HTTP is stateless protocol nothing is remembered during HTTP requests. You'd use a cookie (or a session) to remember what table column to sort by.

 

Something like

// default order
$order = 'projectId';

// is order set in cookie?
if (isset($_COOKIE['order')) {
  // use order in cookie
  $order = $_COOKIE['order'];
}
// get order from url?
elseif(isset($_GET['order']) {
  // get order from url
  $order = $_GET['order'];

  // remember the column to order the results by.
  setcookie('order', $order, (time()+3600*24*365)); // Max lifspan 1 year
}
Link to comment
Share on other sites

@Barand good catch  ;D

 

Code amended

// default order
$order = 'projectId';

// get order from url?
if(isset($_GET['order']) {
  // get order from url
  $order = $_GET['order'];

  // remember the column to order the results by.
  setcookie('order', $order, (time()+3600*24*365)); // Max lifspan 1 year
}
// is order set in cookie?
elseif (isset($_COOKIE['order')) {
  // use order in cookie
  $order = $_COOKIE['order'];
}
Link to comment
Share on other sites

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.