All of you use the tables in mysql database. When you click the column header at first time, it displays values in ascending order. Likewise it displays values in descending order, while clicking field name second time. Normally the table look like this:
When you click the category_Name field, it display value by ascending order as follow as:
We can make our table like tables in mysql database. We can sorting values in column by clicking filed name. It can be done by PHP.
Follow the below steps.
1. Connect file with mysql database as follows as:
mysql_connect('server_name','username','password');
mysql_select_db('db_name');
where,
- server_name means localhost,
- username is username of your database
- password is password of your database
- db_name is name of your database
2. Then, you need to sorting tables based on column filed name. So you have to pass the field name in url. Similarly, you sort table values both ascending and descending. So you need to also pass order types in url. By default, table values displays in ascending. You can fixed order types and also field name.
$field='game_ID';
$sort='ASC';
where, the mysql table display values by ascending order based on game_ID
Now your headings should be like this.
<th><a href="table1.php?sorting='.$sort.'&field=game_ID">Game Id</a></th>
<th><a href="table1.php?sorting='.$sort.'&field=category_Name">Category Name</a></th>
<th><a href="table1.php?sorting='.$sort.'&field=game_Title">Game Name</a></th>
where,
- table1.php is the name of file.
- $sort means order type either ascending or descending
- game_ID, category_Name, game_Title are field names in mysql table.
3. Now you need get the values from url.
if(isset($_GET['sorting']))
{
if($_GET['sorting']=='ASC')
{
$sort='DESC';
}
else { $sort='ASC'; }
}
if($_GET['field']=='game_ID')
{
$field = "game_ID";
}
elseif($_GET['field']=='category_Name')
{
$field = "category_Name";
}
elseif($_GET['field']=='game_Title')
{
$field="game_Title";
}
where, you handle your statergies. ie, If sorting value is ascending, then you will sort table by descending. Likewise you'll sort table by ascending, if sort value is descending. Then you need to get column field values.
4. Then you write a query in which both order types and field name should be present. The mysql query should be like this.
SELECT game_ID, category_Name, game_Title FROM yobash_game ORDER BY $field $sort
where,
- $filed is a default field name ie, gameId
- $sort is a default order type already we fixed. ie. ASC
5. Finally combine all the PHP codes as follows as:
table1.php
Finally you'll get PHP scripts for sorting table when you click the column filed name. Now you can sort tables as like as tables in mysql database.
When you click the category_Name field, it display value by ascending order as follow as:
We can make our table like tables in mysql database. We can sorting values in column by clicking filed name. It can be done by PHP.
Follow the below steps.
1. Connect file with mysql database as follows as:
mysql_connect('server_name','username','password');
mysql_select_db('db_name');
where,
- server_name means localhost,
- username is username of your database
- password is password of your database
- db_name is name of your database
$field='game_ID';
$sort='ASC';
where, the mysql table display values by ascending order based on game_ID
Now your headings should be like this.
<th><a href="table1.php?sorting='.$sort.'&field=game_ID">Game Id</a></th>
<th><a href="table1.php?sorting='.$sort.'&field=category_Name">Category Name</a></th>
<th><a href="table1.php?sorting='.$sort.'&field=game_Title">Game Name</a></th>
where,
- table1.php is the name of file.
- $sort means order type either ascending or descending
- game_ID, category_Name, game_Title are field names in mysql table.
3. Now you need get the values from url.
if(isset($_GET['sorting']))
{
if($_GET['sorting']=='ASC')
{
$sort='DESC';
}
else { $sort='ASC'; }
}
if($_GET['field']=='game_ID')
{
$field = "game_ID";
}
elseif($_GET['field']=='category_Name')
{
$field = "category_Name";
}
elseif($_GET['field']=='game_Title')
{
$field="game_Title";
}
where, you handle your statergies. ie, If sorting value is ascending, then you will sort table by descending. Likewise you'll sort table by ascending, if sort value is descending. Then you need to get column field values.
4. Then you write a query in which both order types and field name should be present. The mysql query should be like this.
SELECT game_ID, category_Name, game_Title FROM yobash_game ORDER BY $field $sort
where,
- $filed is a default field name ie, gameId
- $sort is a default order type already we fixed. ie. ASC
Sorting column by clicking column header
table1.php
<?php
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('db_name') or die(mysql_error());
$field='game_ID';
$sort='ASC';
if(isset($_GET['sorting']))
{
if($_GET['sorting']=='ASC')
{
$sort='DESC';
}
else
{
$sort='ASC';
}
}
if($_GET['field']=='game_ID')
{
$field = "game_ID";
}
elseif($_GET['field']=='category_Name')
{
$field = "category_Name";
}
elseif($_GET['field']=='game_Title')
{
$field="game_Title";
}
$sql = "SELECT game_ID, category_Name, game_Title FROM yobash_game ORDER BY $field $sort";
$result = mysql_query($sql) or die(mysql_error());
echo'<table border="1">';
echo'<th><a href="table1.php?sorting='.$sort.'&field=game_ID">Game Id</a></th>
<th><a href="table1.php?sorting='.$sort.'&field=category_Name">Category Name</a></th>
<th><a href="table1.php?sorting='.$sort.'&field=game_Title">Game Name</a></th>';
while($row = mysql_fetch_array($result))
{
echo'<tr><td>'.$row['game_Id'].'</td><td>'.$row['category_Name'].'</td><td>'.$row['game_Title'].'</td></tr>';
}
echo'</table>';
?>
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('db_name') or die(mysql_error());
$field='game_ID';
$sort='ASC';
if(isset($_GET['sorting']))
{
if($_GET['sorting']=='ASC')
{
$sort='DESC';
}
else
{
$sort='ASC';
}
}
if($_GET['field']=='game_ID')
{
$field = "game_ID";
}
elseif($_GET['field']=='category_Name')
{
$field = "category_Name";
}
elseif($_GET['field']=='game_Title')
{
$field="game_Title";
}
$sql = "SELECT game_ID, category_Name, game_Title FROM yobash_game ORDER BY $field $sort";
$result = mysql_query($sql) or die(mysql_error());
echo'<table border="1">';
echo'<th><a href="table1.php?sorting='.$sort.'&field=game_ID">Game Id</a></th>
<th><a href="table1.php?sorting='.$sort.'&field=category_Name">Category Name</a></th>
<th><a href="table1.php?sorting='.$sort.'&field=game_Title">Game Name</a></th>';
while($row = mysql_fetch_array($result))
{
echo'<tr><td>'.$row['game_Id'].'</td><td>'.$row['category_Name'].'</td><td>'.$row['game_Title'].'</td></tr>';
}
echo'</table>';
?>
Finally you'll get PHP scripts for sorting table when you click the column filed name. Now you can sort tables as like as tables in mysql database.
Thank you much for this post. I tried lots of time before see this post. I can't get correct answer. But now i know the sorting columns by clicking column header.
ReplyDeleteI need pagination concept with sorting column. Could you please post? Thanks in advance.
thank u very much for that post...nice
ReplyDeleteHow do you narrow down the field result. Let's say their are 5 different category_Name how can I select and display only 1 of those 5 and then make it sortable?
ReplyDeleteThis post is not cover your requirements.
DeleteIt is only sorting column values by clicking column header like Phpmyadmin
pls tell which one is table1.php?
ReplyDeletedidnt get this pls tell?asap
Just created php file with above reference and save this "table1.php".
DeleteThe sort part does not work well. Everytime the page is reloaded $sort will be ASC. How can I prevent that.
ReplyDeleteWhen the first time the page is loaded, $sort value will be ASC. After that it will be changed based on your clicks at header. You can see these lines
Delete$sort='ASC';
if(isset($_GET['sorting']))
{
if($_GET['sorting']=='ASC')
{
$sort='DESC';
}
else
{
$sort='ASC';
}
}
You can also see the changes on address bar.
Thanks for your reply. I tried it that way first. My idea is that the line in the example Game Id reloads the page. So I think the value of $sort will be SET again to 'ASC'.
ReplyDeleteI don't see any changes on the addressbar. It always shows ASC.
It didn't take as you expect. Because i checked the condition whether the $_GET['sorting'] is coming or not. Just you can see below codes.
Deleteif(isset($_GET['sorting']))
{
if($_GET['sorting']=='ASC')
{
$sort='DESC';
}
else
{
$sort='ASC';
}
a href="table1.php?sorting='.$sort.'&field=game_ID" was the line I wanted to write. Not the word Game Id.
ReplyDeleteThats correct..
DeletePlease see line
Game Id
You can't assign word 'Game id'. Because you can't create field with space in mysql. You need to add '-' or '_' for connect words.
No, I know. I only tried to write the line of your example. I added a line 'echo $sort;' before and after the code of your first reply.
ReplyDeleteecho $sort;
if(isset($_GET['sorting']))
{
if($_GET['sorting']=='ASC')
{
$sort='DESC';
}
else
{
$sort='ASC';
}
echo $sort;
The first time the reply is ASC ASC
After clicking on the header the reply is ASC DESC
The addressbar still does not change
I am sorry to say so.
Excuse me, I made a terrible mistake. I inserted the if(isset($_GET['sorting'])) code AFTER the code to display the header. Not before. Shame on me. I changed it and now it works. Thanks for your patience.
ReplyDeleteOk fine.. Thanks for waiting my reply.
Deletewhy are the little arrows not showing?
ReplyDeleteI mean ... how can i make em displays
DeleteBut, I guess this sort method to be slow for long data sets.
ReplyDeleteHi thanks for your articel,
ReplyDeleteBut i have problem, my table cant sort DESC.
When I click the header, he always sort the table ASC.
so i cant sort it DESC
Any idea ??
Did you get any error?
Deleteyup i have set $sort = DESC; And this is my code
Delete$sort='DESC';
echo $sort;
if(isset($_GET['sorting']))
{
if($_GET['sorting']=='ASC')
{
$sort='DESC';
}
else
{
$sort='ASC';
}
}
< my code >
according to me, $sort catch this value
else
{
$sort = 'ASC';
}
do you have any idea?
hi men,
Deletei have wrong code at:
'NIK'
the right code is:
NIK
without '
and now it works perfectly thanks guru
HI,
ReplyDeletesorting is working fine but i got this error "Undefined index: field" when i rain the code.
do you know why?
Add this code at top of your code snippet.
Deleteerror_reporting('E_ALL ^ E_NOTICE');
it works. Thanks
Deletedo you know how i can add below styles to php table?
Deletea:link {color: white;} /* unvisited link */
a:visited {color: white;} /* visited link */
a:hover {color: white;} /* mouse over link */
a:active {color: white;} /* selected link */
a:link {text-decoration: none;}
a:visited {text-decoration: none;}
a:hover {text-decoration: underline;}
a:active {text-decoration: underline;}
table.tableizer-table {
border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
.tableizer-table td {
padding: 4px;
margin: 3px;
border: 1px solid #ccc;
}
.tableizer-table th {
background-color: #104E8B;
color: #FFF;
font-weight: bold;
}
I want to add below style php table. How you can do that?
ReplyDeletea:link {color: white;} /* unvisited link */
a:visited {color: white;} /* visited link */
a:hover {color: white;} /* mouse over link */
a:active {color: white;} /* selected link */
a:link {text-decoration: none;}
a:visited {text-decoration: none;}
a:hover {text-decoration: underline;}
a:active {text-decoration: underline;}
table.tableizer-table {
border: 1px solid #CCC; font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
.tableizer-table td {
padding: 4px;
margin: 3px;
border: 1px solid #ccc;
}
.tableizer-table th {
background-color: #104E8B;
color: #FFF;
font-weight: bold;
}
<html>
ReplyDelete<head>
<style type="text/css">
Add above style here..
</style>
</head>
<body>
Add above PHP code here and add class to table
</body>
</html>
i have written the same code as you given above but it cannot sort the table plz correct my errors from below codes. Thanks in advance.
ReplyDelete$select_query=mysqli_query($DB_Connect,"SELECT * FROM users ORDER BY $field $sort");
ID
First Name
if(isset($_GET['sorting']))
{
if($_GET['sorting']=='ASC')
{
$sort='DESC';
}
else
{
$sort='ASC';
}
}
if(isset($_GET['field'])=='ID')
{
$field="ID";
}
elseif(isset($_GET['field'])=='First_Name')
{
$field="First_Name";
}
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteif(isset($_GET['sirala']) && isset($_GET['sorting'])) {
ReplyDeleteif ($_GET['sirala'] == 'uye_adi') {
$_SESSION["sirala"] = "uye_adi";
} elseif ($_GET['sirala'] == 'tarih') {
$_SESSION["sirala"] = "tarih";
}
$field = $_SESSION["sirala"];
if ($_GET['sorting'] == 'ASC') {
$_SESSION["sort"] = "DESC";
} elseif($_GET['sorting'] == 'DESC') {
$_SESSION["sort"] = "ASC";
}
$sort = $_SESSION["sort"];
}
if(isset($_SESSION["sirala"])) {
$field = $_SESSION["sirala"];
}
if(isset($_SESSION['sort'])) {
$sort = $_SESSION["sort"];
}
Good work sir and it is so helpful for me you done a good job.
ReplyDeletethanks sir for your great work.
ReplyDeleteNotice: Undefined index: field in C:\xampp7.3\htdocs\abc\sort.php on line 16
ReplyDeleteYou make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers. 増田裕介
ReplyDelete