Jump to content

Recommended Posts

I have a PHP/MySQL search results page output to multiple columns in an HTML table. Each column header is a link to resort the results based on that column. However, I'm trying to figure out how to create the code to where the column will toggle between ascending and descending each time the link is clicked.

 

My headers for each row of results look like this:

<td><a href="index.php?sortby=fieldname">Field Name</a></td>

 

How can I get this to toggle back and forth (by default, it sorts by ascending).

I found a site online that shows what I'm trying to do:

http://www.plus2net.com/sql_tutorial/sql_order-change.php?by=asc

 

Problem is my page table and queries are set up differently, so when I implement his code, it only seems to work once. It isn't continually grabbing the variable and changing it.

I found a site online that shows what I'm trying to do:

http://www.plus2net.com/sql_tutorial/sql_order-change.php?by=asc

 

Problem is my page table and queries are set up differently, so when I implement his code, it only seems to work once. It isn't continually grabbing the variable and changing it.

 

Can we see your relevant current code?

I found a site online that shows what I'm trying to do:

http://www.plus2net.com/sql_tutorial/sql_order-change.php?by=asc

 

Problem is my page table and queries are set up differently, so when I implement his code, it only seems to work once. It isn't continually grabbing the variable and changing it.

 

Can we see your relevant current code?

 

I keep altering it to try and get it to work. It looks something like this, though (the %20 I threw in to see if it would help based on the way it parses in the browser url field):

 

<?php if (($by == " ASC") OR ($by == "%20ASC")) { $by = " DESC";}
else if (($by == " DESC") OR ($by == "%20DESC")) { $by = " ASC";}
else if (($by == "")) { $by = " ASC";}?>

<td><a href="index.php?sortby=Field_name<? echo $by; ?>&by=<? echo $by; ?>">Field name</a></td> 

 

Then in the query, the order by = $sortby.

You can look into using jQuery and tableSorter plugin

http://tablesorter.com/docs/

I can actually do this myself if I create 2 buttons/arrows to the right of each field, then hard code the ASC or DESC, but I have limited space and am trying to do this with a single clickable link, i.e., the column name.

 

Thanks for looking into it, though.

Got it!

 

I had to place this at the top of the page:

<?php $by=$_GET['by']; ?>

 

Then this:

<?php
if(isset($by) and $by==" ASC"){
$by=" DESC";
}else{$by=" ASC";} ?>

 

Then the table column is:

<td><a href="index.php?sortby=Field_name<? echo $by; ?>&by=<? echo $by; ?>">Field name</a></td>

 

And at the end of each query is:

ORDER BY $sortby

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.