1

I brought all values from the db and showed them on a page. Now, if I click the column it should sort ascending or descending.

How do I do this?

3

6 Answers 6

4

If you want to do this solely in PHP you will need to add parameters onto the query string in the URL.

www.example.com/table.php?col=mycolumn&sort=desc

That would be in the anchor tag for that column, then in table.php you would have the logic to handle sorting.

Sign up to request clarification or add additional context in comments.

8 Comments

Remember not to put these values into query directly to avoid SQL injection
Now ,i have bought them in anchor tag.How to pass them in query string
@fddsf here i posted an example: stackoverflow.com/questions/2993027/… there is only field name mentioned but for the direction the code would be all the same
@Schrapnel this can be easily avoided by checking for desc and asc only. Even better would be to use PDO to avoid it altogether.
@Col It has everything to do here. you started comment with SQL-injections. The best way to prevent that is to use PDO! the query-string could be unsafe(even to XSS).
|
0

You'll be using JavaScript. I suggest sorttable.

4 Comments

JS has very limited use, just for one-page sized datasets
@Col. What does "one-page" mean? That there would be so many tr elements to keep track of, JavaScript just isn't feasible for larger datasets?
mean like Stackoverflow. there are many pages with questions and no browser can hold them all - so, for each another page another request to the server being made. and to sort all questions in another direction will require another request to the server as well
@Col. I still don't understand. Why are pages an issue at all? Wouldn't the table be a single table element on a single page? Are you talking about a paginated table with lots and lots of rows in it?
0

Expanding on roflwaffle's post, you will want to use query string vars. From these build a string which you add into your query.

$querySort = array();
if (isset($_GET['colName'])) {
    $s = $_GET['colName'];
    $snext = '';
    switch ($s) {
        case '':     $snext = 'desc'; break;
        case 'desc': $snext = 'asc';  break;
        case 'asc':  $snext = ''; break;
        default:     $snext = '';
    }
    if (!empty ($snext)) {
        $querySort[] = "colName $snext";
    }
    $colName_link = "colName=$snext";
    $colName_text = $snext;
}

// Further column checks

if (count($querySort) == 0) {
    // Default sort if no sort is given.
    $querySort[] = "colName asc";
}

$sort = implode (', ', $querySort);

$query = mysql_query ("SELECT * FROM table ORDER BY $sort");

So given the following query string,

www.example.com/table.php?colName=desc  

the query would look like this

SELECT * FROM table ORDER BY colName desc LIMIT 0, 15

and the category link would look like this

echo "<a href='?$colName_link'>Column Name</a> <small>$colName_text</small>";

6 Comments

@Gutter ball.i need to do asce or descen by clicking column header
@fddsf make the column header a hyperlink.
@Gutter ball:I have done it but what php code to pass through the link.
@fddsf I have edited my post so that hopefully its a little more helpful for you. The code for the link is at the end of my example.
It seems the task is too hard for him :) You have to either write and test whole code, or give up :) One have to have some basic knowledge on web-development before asking such questions.
|
0

The best way to do this is by using javascript(no need to refresh page). Yahoo!'s YUI3 has a very nice datatable component. I can not make up from your question if you got the complete dataset on your PHP page but I will assume this for now. You should have a look at YUI3's Column Sorting page to get you started.

2 Comments

Wow! Do you have such a short users list that it fits into just one page? My condolences ;)
Lol Okay maybe not such a good example :P. I think I should remove the query altogether ;)
0

Please take a look at THIS link.Check the Example HERE. I use it in one of my project for the same purpose , hope it will help you too.

Comments

-3

request: www.example.com/table.php?col=mycolumn&sort=desc

//may be some check , about the column is in  the table field
//sort is in the set of (asc , desc)

$q = "select * from mytable where mycond order by addslashes($_GET['mycolumn']) addslashes($_GET['sort'])";

2 Comments

Shouldn't use addslashes, should use prepared statements
@Logan Bailey LOL! Are you sure you understand the question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.