How can i display sort by ascending & descending list on PHP page from MySQL database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • isha bhai
    New Member
    • Apr 2012
    • 2

    #1

    How can i display sort by ascending & descending list on PHP page from MySQL database

    Hello My Dears,

    I have php page with mySQL database. Here data is not sorted. Table is 4 column like as --
    (1) Name
    (2) Language
    (3) Location
    (4) Category

    I want to display sorted by name (default option - an option that is selected automatically unless an alternative is specified. )

    But visitors can display this list --

    1. Sort by Name,
    2. Sort by Language,
    3. Sort by Location Or
    4. Sort by Category

    by click on table header option...

    If anybody of my-dears can help me. Plz ....Plz ...... post Code 4 it.

    Thanks
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    i read your post and lost interest to answer.

    but I will tell you this:

    if you send a parameter to webserver such as sortby=Name

    then in your php you can create a query like below:
    "Select * FROM `desired_table` ORDER BY `".$received_da ta."`";

    Comment

    • Bharat383
      New Member
      • Aug 2011
      • 93

      #3
      by core php ===>

      <a href="view_deta ils.php?by=name ">Sort By Name</a>
      <a href="view_deta ils.php?by=lang uage">Sort By Language</a>
      <a href="view_deta ils.php?by=loca tion">Sort By Location</a>
      <a href="view_deta ils.php?by=cate gory">Sort By Category</a>

      on page ====>

      <?php
      if(isset($_GET['by']))
      {
      $search_by = $_GET['by'];

      $qry = "select * from table_name order by ".$search_b y.";
      }
      else
      {
      $qry = "select * from table_name";
      }

      $query = mysql_query($qr y) or die(mysql_error ());

      while($row = mysql_fetch_arr ay($query))
      {
      /* your displaying code here */

      }

      ?>


      Bharat Parmar(Bharat38 3)

      Comment

      • Bharat383
        New Member
        • Aug 2011
        • 93

        #4
        if you wanna quick result then use AJAX on click calla javascript function and pass value in php page and fetch the result .....
        it too speedy than core php and your whole page will not refresh just the details part will be fresh it's look more dynamic....

        Comment

        • isha bhai
          New Member
          • Apr 2012
          • 2

          #5
          Thank you

          Boss ...

          Comment

          • parthi19
            New Member
            • Jan 2014
            • 3

            #6
            Sorting column by clicking column header using php and mysql

            The concept as follows as
            Code:
            if(isset($_GET['sorting']))
            {
              if($_GET['sorting']=='ASC')
              {
              $sort='DESC';
              }
              else { $sort='ASC'; }
            }
            visit: http://www.phponwebsites.com/2014/05...mn-header.html

            Comment

            Working...