order by asc and desc in php

<?php
include "include/config.php";
$query = "select * from student";
if(isset($_POST['search'])){
    $search_term = $_POST['search_term'];
    $query .= " WHERE fname = '{$search_term}' ";
}

// if ($_GET['sort'] == 'fname')
// {
//     $query .= " ORDER BY fname";
// }

if(isset($_GET['order'])){
    $isAsc = isset($_GET['order'])? (bool) $_GET['order']: 1;
    $query .= " ORDER BY fname ".($isAsc?"ASC":"DESC").";";

}

$result = mysqli_query($conn,$query) or die(mysqli_error($conn));

?>

<html>
    <head>
        <title>Student Information System</title>

        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

        <!-- Optional theme -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
        <style>
        tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}
        </style>
        <link rel="stylesheet" href="/css/style.css">

    </head>
    <body>
        <div class="container">
            <div class="" id="mylink">
                <h3>Student Information System</h3>
                <a href="index.html">Add Record</a>
                <a href="view.html">Display</a>
            </div>
            <div class="">
                <form method="post" action="view.php">
                    <input type=text name="search_term">
                    <input type="submit" name="search" value="search">
                </form>
            </div>

            <div class="">
                <table class="table">
                    <tr>
                        <th>id</th>
                       <th>
                           <a href="view.php?order=<?php echo isset($_GET['order'])?!$_GET['order']:1; ?>">
      First
   </a>
                       </th>
                       
                        <th>Last</th>
                        <th>Gender</th>
                        <th>Mobile</th>
                        <th>Email</th>

                    </tr>
                    <?php
                     while ($record = mysqli_fetch_assoc($result)) {
                   
                    echo "<tr>" ;

                    echo "<td>".$record['id']."</td>";

                    echo "<td>".$record['fname']."</td>";

                    echo "<td>".$record['lname']."</td>";

                    echo "<td>".$record['gender']."</td>";

                    echo "<td>".$record['mno']."</td>";

                    echo "<td>".$record['email']."</td>";



                    echo "</tr>" ;

                    }
                    ?>
                </table>
            </div>
        </div>
        <script src="https://code.jquery.com/jquery-3.1.1.js"
            integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA="
            crossorigin="anonymous"></script>
        <!-- Latest compiled and minified JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    </body>
</html>
Previous
Next Post »

Disqus Shortname

Comments system