By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

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

P: 2
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
Apr 13 '12 #1
Share this Question
Share on Google+
5 Replies


100+
P: 1,059
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_data."`";
Apr 13 '12 #2

P: 93
by core php ===>

<a href="view_details.php?by=name">Sort By Name</a>
<a href="view_details.php?by=language">Sort By Language</a>
<a href="view_details.php?by=location">Sort By Location</a>
<a href="view_details.php?by=category">Sort By Category</a>

on page ====>

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

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

$query = mysql_query($qry) or die(mysql_error());

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

}

?>


Bharat Parmar(Bharat383)
Apr 14 '12 #3

P: 93
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....
Apr 14 '12 #4

P: 2
Thank you

Boss ...
Apr 15 '12 #5

P: 3
The concept as follows as
Expand|Select|Wrap|Line Numbers
  1. if(isset($_GET['sorting']))
  2. {
  3.   if($_GET['sorting']=='ASC')
  4.   {
  5.   $sort='DESC';
  6.   }
  7.   else { $sort='ASC'; }
  8. }
visit: http://www.phponwebsites.com/2014/05...mn-header.html
May 8 '14 #6

Post your reply

Sign in to post your reply or Sign up for a free account.