Q:

How to sort table columns with PHP and MySQL?

belongs to collection: PHP Programming Exercises

0

How to sort table columns with PHP and MySQL

This exercise describes a good example of how to sort HTML table data in ascending and descending order on clicking the header using PHP and MySQL. Table sorting is a very important functionality. It provides functionality for the users to easily sort the data in ascending and descending order as per their requirements. Here is the process of sorting MySQL data in the HTML table by column name in ascending and descending order.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

To Sort Table Data in PHP and MySQL

Here is the complete code to sort table data in ascending and descending order on toggle the table header.

<html>
    <head>
        <link rel="stylesheet" href="bootstrap.min.css" />
        <style type="text/css">
            .table { width: 40%; border: 2px solid #edf7b7 ; }
            th {background: #edf7b7 none repeat scroll 0 0 !important; }
        </style>
    </head>
    <body>

            <?php 
            $conn = new mysqli('localhost', 'root', '', 'company');
            if($conn->connect_error){
                die("Error in DB connection: ".$conn->connect_errno." : ".$conn->connect_error);    
            }

            $field = $_GET['sort'];
            if($field == ''){
               $field = 'created_date'; 
            } 

            $ordertype = ($_GET['order'] == 'desc')? 'asc' : 'desc';
            if($_GET['order'] == 'asc'){
                $sort_arrow =  '<img src="sorting-arrow-desc.png" />';
            }
            else if($_GET['order'] == 'desc'){
                $sort_arrow =  '<img src="sorting-arrow-asc.png" />';
            }
            else{
                $sort_arrow =  '<img src="sorting-arrow-desc.png" />';
            }

            $select = "SELECT * FROM `employee` ORDER BY $field $ordertype ";
            $result = $conn->query($select);
            if($result->num_rows > 0){
                echo '<table class="table table-striped" >';
                echo '<tr>';
                echo '<th><a href="sortingtbl.php?sort=emp_id&order='.$ordertype.'">Id ';
                if($field == 'emp_id') { echo $sort_arrow; }      
                echo '</a></th>'; 
                echo '<th><a href="sortingtbl.php?sort=emp_name&order='.$ordertype.'">Employee Name  '; 
                if($field == 'emp_name') { echo $sort_arrow; } 
                echo '</a></th>'; 
                echo '<th>Email</th>'; 
                echo '<th>Phone</th>';  
                echo '<th><a href="sortingtbl.php?sort=created_date&order='.$ordertype.'">Registered Date ';
                if($field == 'created_date') { 
                    echo $sort_arrow; 
                } 

                echo '</a></th>';  
                echo '</tr>';
                    while($row = $result->fetch_object()){
                        echo '<tr>';  
                        echo '<td>'.$row->emp_id.'</td>';
                        echo '<td>'.$row->emp_name.'</td>';
                        echo '<td>'.$row->email.'</td>';
                        echo '<td>'.$row->phone.'</td>';
                        echo '<td>'.date('Y-m-d', strtotime($row->created_date)).'</td>';
                        echo '</tr>';
                    }
                echo '</table>';
            }
            ?>
    </body>
</html>

When we will execute the above code, the table look like this -

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Programming Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to get current directory, filename and code li... >>
<< How can I post a form without refreshing the page?...