Php & MySQL Database : How To Sort Html Table DataUsing Php
________________________________________________________
I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .
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>
Bagikan
Php And MySQL - How To Order Html Table Data Using Php
4/
5
Oleh
insurance