Results 1 to 10 of 10

Thread: Sorting content in a database

  1. #1
    Join Date
    Jan 2007
    Posts
    197
    Rep Power
    0

    Default Sorting content in a database

    The is in mysql and is displayed using php, how do i assign sorting buttons (ascending and descending order) by clicking on the heading of the table...how is this achieved

  2. #2
    Join Date
    Feb 2006
    Posts
    4,242
    Rep Power
    0

    Default

    |--- www.RealJamaicaEstate.com ™ ---|
    Invest small = small returns [micro enterprise] | Invest Big = returns Big [macro enterprise]
    --- www.fashionsJAMAICA.com ™ -|- www.ChampsJamaica.com

  3. #3
    Join Date
    Jan 2007
    Posts
    197
    Rep Power
    0

    Default

    Does any1 have an exiting script that shows how this is done and implemented?

  4. #4
    Join Date
    Apr 2003
    Posts
    13,269
    Rep Power
    34

    Default

    It's nothing hard bro. Each header has a link that passes a variable via the url, something like http://www.mywebsite.com/index.php?sort=desc

    Then in your script, you just check the sort value from the url and see if the value is desc or asc. If it's desc, you add the DESC to the end of your sql query. Same goes for ASC.

  5. #5
    Join Date
    Feb 2006
    Posts
    4,242
    Rep Power
    0

    Default

    Sort = "SELECT field1, field2, field3 FROM TABLE ORDER BY field1 ASC"
    |--- www.RealJamaicaEstate.com ™ ---|
    Invest small = small returns [micro enterprise] | Invest Big = returns Big [macro enterprise]
    --- www.fashionsJAMAICA.com ™ -|- www.ChampsJamaica.com

  6. #6
    Join Date
    Jan 2007
    Posts
    197
    Rep Power
    0

    Default

    I've been trying it but i seem 2 be off at some point...will post the code later today

  7. #7
    digimon Guest

    Default

    Post the code, i'm sure we'll fix it

  8. #8
    Join Date
    Jan 2007
    Posts
    197
    Rep Power
    0

    Default

    I got it workin thx 2 Sutra:

    This is the query:
    PHP Code:
    function list($start=1,$rows=16
        {
          
    $order $_GET['order'];
          
    $column $_GET['sortby'];
          if(
    $column == 'info' && $order == 'asc')
          {
            
    $this->dao->fetch("SELECT * FROM item ORDER BY list ASC LIMIT ".$start.", ".$rows);
          }
          else if(
    $column == 'info' && $order == 'desc')
                 {
                      
    $this->dao->fetch("SELECT * FROM list ORDER BY list DESC LIMIT ".$start.", ".$rows);
                 }
                 else
               {
                     
    $this->dao->fetch("SELECT * FROM list ORDER BY timestamp DESC LIMIT ".$start.", ".$rows);
            }
        } 
    This is what the Column/list name shout look like:
    PHP Code:
    <a href=\"".$_SERVER['PHP_SELF']."?order=asc&sortby=info&view=table&rownum=".($rownum)."\">Column Name ^</a>

    <a href=\""
    .$_SERVER['PHP_SELF']."?order=desc&sortby=info&view=table&rownum=".($rownum)."\">Column Name v</a> 

  9. #9
    Join Date
    Feb 2006
    Posts
    4,242
    Rep Power
    0

    Default

    cool...
    what are you using it for, desktop app or web app?
    |--- www.RealJamaicaEstate.com ™ ---|
    Invest small = small returns [micro enterprise] | Invest Big = returns Big [macro enterprise]
    --- www.fashionsJAMAICA.com ™ -|- www.ChampsJamaica.com

  10. #10
    Join Date
    Jan 2007
    Posts
    197
    Rep Power
    0

    Default

    Quote Originally Posted by Utech22 View Post
    cool...
    what are you using it for, desktop app or web app?
    Its a web app.....it has a very long list stored in mysql and cannot be displayed on 1 page only (hence why javascript could not be used). Didnt know it was so simple though....big up to sutra agen

Posting Permissions

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