Php & MySQL Database : How To Sort Html Table DataUsing Php
In this Php Tutorial we will see How To Use Sort Html Table Data In Ascending And Descending Order By Column Name Using MySQL Database And Php .
I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .
Php Source Code:
<?php
// Ascending Order
if(isset($_POST['ASC']))
{
$asc_query = "SELECT * FROM users ORDER BY age ASC";
$result = executeQuery($asc_query);
}
// Descending Order
elseif (isset ($_POST['DESC']))
{
$desc_query = "SELECT * FROM users ORDER BY age DESC";
$result = executeQuery($desc_query);
}
// Default Order
else {
$default_query = "SELECT * FROM users";
$result = executeQuery($default_query);
}
function executeQuery($query)
{
$connect = mysqli_connect("localhost", "root", "", "test_db");
$result = mysqli_query($connect, $query);
return $result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title> PHP HTML TABLE ORDER DATA </title>
<style>
table,tr,th,td
{
border: 1px solid black;
}
</style>
</head>
<body>
<form action="php_html_table_data_order.php" method="post">
<input type="submit" name="ASC" value="Ascending"><br><br>
<input type="submit" name="DESC" value="Descending"><br><br>
<table>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<!-- populate table from mysql database -->
<?php while ($row = mysqli_fetch_array($result)):?>
<tr>
<td><?php echo $row[0];?></td>
<td><?php echo $row[1];?></td>
<td><?php echo $row[2];?></td>
<td><?php echo $row[3];?></td>
</tr>
<?php endwhile;?>
</table>
</form>
</body>
</html>
// Ascending Order
if(isset($_POST['ASC']))
{
$asc_query = "SELECT * FROM users ORDER BY age ASC";
$result = executeQuery($asc_query);
}
// Descending Order
elseif (isset ($_POST['DESC']))
{
$desc_query = "SELECT * FROM users ORDER BY age DESC";
$result = executeQuery($desc_query);
}
// Default Order
else {
$default_query = "SELECT * FROM users";
$result = executeQuery($default_query);
}
function executeQuery($query)
{
$connect = mysqli_connect("localhost", "root", "", "test_db");
$result = mysqli_query($connect, $query);
return $result;
}
?>
<!DOCTYPE html>
<html>
<head>
<title> PHP HTML TABLE ORDER DATA </title>
<style>
table,tr,th,td
{
border: 1px solid black;
}
</style>
</head>
<body>
<form action="php_html_table_data_order.php" method="post">
<input type="submit" name="ASC" value="Ascending"><br><br>
<input type="submit" name="DESC" value="Descending"><br><br>
<table>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>
<!-- populate table from mysql database -->
<?php while ($row = mysqli_fetch_array($result)):?>
<tr>
<td><?php echo $row[0];?></td>
<td><?php echo $row[1];?></td>
<td><?php echo $row[2];?></td>
<td><?php echo $row[3];?></td>
</tr>
<?php endwhile;?>
</table>
</form>
</body>
</html>
3 comments
commentsHello , its so bad source code without sql code, where I can get sql code?
ReplyI am very confused , you just put only php and html code , but where I WILL TAKE SQL?
include 'conn.php';
Reply$result = mysqli_query($conn,"SELECT * FROM users ORDER BY age DESC");
use this new format instead
when the code loads what will be the logic to fire the exclusive credits from the form that follows
Reply