Results 1 to 11 of 11

Thread: [RESOLVED] Sorting by a column

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Resolved [RESOLVED] Sorting by a column

    I've seen loads of tables where I can click on the heading title and it will sort the table in order - does anyone know how this is done or any good tutorials on it?

  2. #2
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Sorting by a column

    Usually the data displayed is from a database which is where the sorting takes place.

    eg.
    PHP Code:
    //When page loads for first time show in ASC order
    $sSQL "SELECT * FROM YourTable ORDER BY YourField ASC"

    //Then when column heading is clicked do the opposite sorting.
    $sSQL "SELECT * FROM YourTable ORDER BY YourField DESC" 

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Sorting by a column

    Thanks for that - I was wondering when the user clicks on the column does it call a function on another php page or a function within the same page??? How does it work - do you have any examples?

  4. #4
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Sorting by a column

    This link should help you out

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Sorting by a column

    Hmmm, nope there's an error - I get: 'Error: A problem was encountered while executing this query.'

    Strange.... anyway I'll search on this topic for more info...

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Sorting by a column

    Use the query string to specify the field to sort by and the direction.

    Ensure that these values are properly sanitized in your code to ensure no other data except the column name appears in the query.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Sorting by a column

    Have you got any examples pleasE?????

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Sorting by a column

    Quote Originally Posted by wwwfilmfilercom
    Have you got any examples pleasE?????
    Write it yourself and stop being lazy.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Sorting by a column

    Quote Originally Posted by wwwfilmfilercom
    Have you got any examples pleasE?????
    http://google.com/search?q=query+string+php

  10. #10
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Sorting by a column

    The link that lintz provided gives enough information and example of the very thing you want, except you probably want to include the ability to either sort by ascending or descending, which is just another quick step. in the link he posted, you use $_GET['order'] for the column name you're sorting by and you could just add $_GET['sort'] or something that would contain either "ascending" or "descending" (or any shortened abbreviations, like asc/desc, a/d, etc), then you just tack the command (either ASC or DESC) onto the end of your query and you're ready to go.

    it's quite simple, really.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    From the UK
    Posts
    422

    Re: Sorting by a column

    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width