Php And MySQL - How To Order Html Table Data Using Php

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>






///////////////OUTPUT: 




php table default order
Html Table Default Order
                                                             
php table ascending order
Html Table Ascending Order
                                                                 
php table descending order
Html Table Desending Order


                     




Share this

Related Posts

Previous
Next Post »

3 comments

comments
16 avril 2020 à 22:31 delete

Hello , its so bad source code without sql code, where I can get sql code?
I am very confused , you just put only php and html code , but where I WILL TAKE SQL?

Reply
avatar
2 juillet 2020 à 08:52 delete

include 'conn.php';

$result = mysqli_query($conn,"SELECT * FROM users ORDER BY age DESC");
use this new format instead

Reply
avatar
Anonyme
3 janvier 2023 à 10:37 delete

when the code loads what will be the logic to fire the exclusive credits from the form that follows

Reply
avatar