0

I get the information of my database into a table on my website. I sort my table when I click the link below, but I'd like to undo the sorting by clicking the same link again. How do I do that? I researched rsort & unsset but I just can't get the solution.

For example I have a table with the tableheader "firstname" which contains all the names of course. When I click "firstnames", it sorts the firstnames.

$sqli = "SELECT * FROM data_base"

 if ($_GET['sort'] == 'firstname')
  {
      $sqli .= " ORDER BY firstname ASC"; 
  }


 <th><a href=\"test.php?sort=firstname\">Firstname</a></th>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Syno
  • 1,056
  • 10
  • 25
  • what's your normal sort ? – Guillaume Sainthillier Mar 07 '17 at 09:02
  • I'm keep adding lines to a table.. so not a default one – Syno Mar 07 '17 at 09:03
  • There is no "normal" sort. If you don't place an order statement, the rows will be sent in the most appropriate way for the mysql server. After you select data from the table which is sorted, mysql may or may not store your result set in the cache. If you query the same table next time, the server sees that the cached result may fit and presents exactly that. – Psi Mar 07 '17 at 09:04
  • Hmm so there is no way of deleting the GET after i click the link again? – Syno Mar 07 '17 at 09:06
  • 1
    You don't need to delete anything. If you click the link again, it will sort again, because again the parameter `sort` is set to firstname or lastname. You need to remove that parameter from the link at all – Psi Mar 07 '17 at 09:07
  • 1
    Another way would be to remove the ORDER BY from the query and perform the sort in PHP as needed. – RST Mar 07 '17 at 09:07
  • How can I do that @RST – Syno Mar 07 '17 at 09:08
  • 1
    There is always a way? What do you really want to do? Note: your code has syntax error so it is only a part of it!? If you dont want to sort then just do an `$_GET['sort']=null;` before the `if`. But if you want more, then show real code not only part of it. – JustOnUnderMillions Mar 07 '17 at 09:08
  • 1
    Or just add `No Sorting` !? There are many roads to rome... – JustOnUnderMillions Mar 07 '17 at 09:10
  • It has to be the same link that I sort and on another click undo the sort @JustOnUnderMillions – Syno Mar 07 '17 at 09:11
  • 1
    Or just add `No Sorting` without even specifying the parameter at all. You have to toggle in Javascript what you want to send to the server. The server only does what it sees in this single request – Psi Mar 07 '17 at 09:12
  • Sry, dont get it, what do you mean with `same link`? Should the link still show `Fistname` but not sorting by first name. plz, explain the logic in your question (not here in the comments). _another click undo the sort_ as may comment 3min ago says? sry, im out. – JustOnUnderMillions Mar 07 '17 at 09:12
  • 1
    http://stackoverflow.com/questions/35066131/sorting-asc-and-desc-on-same-link – JustOnUnderMillions Mar 07 '17 at 09:19
  • Thank you guys for helping, you helped me alot – Syno Mar 07 '17 at 09:26

4 Answers4

1

Depending on what was clicked, the links have to be different:

  if ($_GET['sort'] == 'firstname')
  {
      $sqli .= " ORDER BY firstname ASC"; 
  }
  else if ($_GET['sort'] == 'lastname ')
  {
      $sqli .= " ORDER BY lastname ASC";
  }

 if ($_GET['sort'] == 'firstname'){
     echo '<a href=\"test.php\">Firstname</a>';
 }
 else {
     echo '<a href=\"test.php?sort=firstname\">Firstname</a>';
 }

 if ($_GET['sort'] == 'lastname'){
     echo '<a href=\"test.php\">Lastname</a>';
 }
 else {
     echo '<a href=\"test.php?sort=lastname\">Lastname</a>';
 }
Psi
  • 6,387
  • 3
  • 16
  • 26
1

Pass your parameter according to you previously clicked your link

$sqli = "SELECT * FROM data_base";
$fname="firstname";
$lname="lastname";
 if ($_GET['sort'] == 'firstname')
  {
      $sqli .= " ORDER BY firstname ASC"; 
      $fname="";
  }
  else if ($_GET['sort'] == 'lastname')
  {
      $sqli .= " ORDER BY lastname ASC";
      $lname="";
  }


 echo "<a href=\"test.php?sort=$fname\">Firstname</a>";
 echo "<a href=\"test.php?sort=$lname\">Lastname</a>";
B. Desai
  • 16,414
  • 5
  • 26
  • 47
0

I recommend a new approach:

Add an ID column to your table and use it as primary key, this can be your default sort. It's better a sort ordenation at database level instead of php level (ex: array).

Link1: Default sort
Link2: Sort by FirstName
Link3: Sort by LastName

Also, you can play with ASC and DESC in same link to sort ASCending or DEScending.

0
session_start();

$sqli = "SELECT * FROM data_base"

if ($_GET['sort'] == 'firstname'){
      if(!isset($_SESSION['first']))//checks if there is a no session
      {
         $sqli .= " ORDER BY firstname ASC";//sorting 
         $first='1';
         $_SESSION['first']=$first;//start a session
      }

      if(isset($_SESSION['first']))//checks if their is a session
      {
          $sqli .= "";        //no sorting
          unset($_SESSION['first']); destroys the session so next time it will sort the results
      }
}

else if ($_GET['sort'] == 'lastname ')
{ 
    if(!isset($_SESSION['first']))     //checks if there is a no session
    {
        $sqli .= " ORDER BY lastname ASC";  //sorting 
        $first='1';
        $_SESSION['last']=$last;  //start a session
    }
    if(isset($_SESSION['last']))     //checks if their is a session
    {
        $sqli .= "";        //no sorting
        unset($_SESSION['last']); 
    }
}

 <a href=\"test.php?sort=firstname\">Firstname</a>
 <a href=\"test.php?sort=lastname\">Lastname</a>

You can do it with sessions just add session_start(); at the top just after

Scott
  • 1,863
  • 2
  • 24
  • 43
Gert
  • 360
  • 3
  • 8