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

Link to comment
https://forums.phpfreaks.com/topic/285140-sorting-two-tables-from-mysql-query/
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
}

@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'];
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.