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 .
- PhpMyAdmin .


Php Source Code:


// Ascending Order
    $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>
        <title> PHP HTML TABLE ORDER DATA </title>
                border: 1px solid black;
        <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>
                    <th>First Name</th>
                    <th>Last Name</th>
                <!-- populate table from mysql database -->
                <?php while ($row = mysqli_fetch_array($result)):?>
                    <td><?php echo $row[0];?></td>
                    <td><?php echo $row[1];?></td>
                    <td><?php echo $row[2];?></td>
                    <td><?php echo $row[3];?></td>
                <?php endwhile;?>


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

Next Post »