Column Sort by cleacking heading link

Discussion in 'PHP' started by nile1483, Apr 16, 2009.

  1. #1
    hi friends

    i make simple database table in php my sql and display data report in html table by each
    i have a table of 5 to six column like name city area state etc.
    i want to make each heading link once user click on particular link data is sorted by ascending order by this column, next time user click on same column data is sort by descending order if user click another link data is sort by acending of particular link

    can anybody tell how to make header link and other verification

    i want to something like http://demo-admin.magentocommerce.com/index.php/admin/catalog_product/ but now use of javascript or ajax pure php

    thanks
     
    nile1483, Apr 16, 2009 IP
  2. nayes84

    nayes84 Member

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    do you want it with ajax? or just normal html links?
     
    nayes84, Apr 17, 2009 IP
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    I personally prefer using tokens to perform the operation you are requesting.

    The column link might be something like

    sb = sort by
    so = sort order (ASC or DESC)
    1 = Name
    2 = Country

    <a href='page.php?sb=1'>Name</a> <a href='page.php?sb=2'>Country</a>

    At the top of the page you would store any tokens in the session

    
    <?php
    session_start();
    
    if($_GET){
      
      if($_SESSION['sb']==$_GET['sb']){
    
        if($_SESSION['so']=='ASC'){
    
          $_SESSION['so']='DESC';
    
        }
        else
        {
    
          $_SESSION['so']='ASC';
    
        }
      }
      else
      {
    
        $_SESSION['sb'] = $_GET['sb'];
        $_SESSION['so'] = 'ASC';
    
      }
    }
    ?>
    
    PHP:
    You can then execute your query with something like

    mysql_query("SELECT * FROM table ORDER BY '" . $_SESSION['sb'] . "' " . $_SESSION['so']);


    Theres probably a more efficient way of doing it though. :)
     
    Weirfire, Apr 17, 2009 IP