How to Sort Data using Alphabetical order in PHP MySQL

By Super Admin | Jun 10, 2021 | PHP
Share : Whatsapp

https://www.fundaofwebit.com/post/how-to-sort-data-using-alphabetical-order-in-php-mysql

In this post, you will learn how to sort data using Alphabetical Order in PHP MySQL, so guys, lets create a form with select option to select the sorting type with one submit button. Design a html table to view the data in php mysql. We are going to Sort the Data using GET Method.

We will be using Bootstrap 5 version to design the user interface.

So, Lets get started:

Step 1: Create a file named index.php and paste the below code as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Funda of Web IT</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-12">
                <div class="card mt-5">
                    <div class="card-header">
                        <h4>How to Sort Data in Alphabetical Order A-Z | Z-A (Ascending / Descending) in PHP MySQL</h4>
                    </div>
                    <div class="card-body">
                        
                        <form action="" method="GET">
                            <div class="row">
                                <div class="col-md-4">
                                    <div class="input-group mb-3">
                                        <select name="sort_alphabet" class="form-control">
                                            <option value="">--Select Option--</option>
                                            <option value="a-z" <?php if(isset($_GET['sort_alphabet']) && $_GET['sort_alphabet'] == "a-z"){ echo "selected"; } ?> > A-Z (Ascending Order)</option>
                                            <option value="z-a" <?php if(isset($_GET['sort_alphabet']) && $_GET['sort_alphabet'] == "z-a"){ echo "selected"; } ?> > Z-A (Descending Order)</option>
                                        </select>
                                        <button type="submit" class="input-group-text btn btn-primary" id="basic-addon2">
                                            Sort
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </form>

                        <div class="table-responsive">
                            <table class="table table-bordered">
                                <thead>
                                    <tr>
                                        <th>Product Name</th>
                                        <th>Price</th>
                                        <th>Created At</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                        $con = mysqli_connect("localhost","root","","phptutorials");

                                        $sort_option = "";
                                        if(isset($_GET['sort_alphabet']))
                                        {
                                            if($_GET['sort_alphabet'] == "a-z")
                                            {
                                                $sort_option = "ASC";
                                            }
                                            elseif($_GET['sort_alphabet'] == "z-a")
                                            {
                                                $sort_option = "DESC";
                                            }
                                        }
                                        
                                        $query = "SELECT * FROM a_products ORDER BY name $sort_option";
                                        $query_run = mysqli_query($con$query);

                                        if(mysqli_num_rows($query_run) > 0)
                                        {
                                            foreach($query_run as $row)
                                            {
                                                ?>
                                                <tr>
                                                    <td><?= $row['name']; ?></td>
                                                    <td><?= $row['price']; ?></td>
                                                    <td><?= $row['created_at']; ?></td>
                                                </tr>
                                                <?php
                                            }
                                        }
                                        else
                                        {
                                            ?>
                                                <tr>
                                                    <td colspan="3">No Record Found</td>
                                                </tr>
                                            <?php
                                        }
                                    ?>
                                </tbody>
                            </table>
                        </div>

                    </div>
                </div>
            </div>

        </div>
    </div>
    
    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>



Thanks for reading...