0

Can you help me how to sort my data from MySQL table ?

I want to sort is by using the table head :

<?php
    if(isset($_POST['search'])) {
        $valueTosearch = $_POST['searchvalue'];
        $query = "SELECT * FROM `admin` WHERE CONCAT(`id`, `name`, `gender`, `user_group`, `date_registered`) LIKE '%".$valueTosearch."%'";
        $search_result = filterTable($query);
    } else {
        $query = "SELECT * FROM `admin`";
        $search_result = filterTable($query);
    }

    function filterTable($query)
    {
        include('config/dbconnect.php');
        $filter_result = mysqli_query($con, $query);
        return $filter_result;
    }
?>

<form method='post' action=''>
    <div><input type = 'text' name = 'searchvalue' placeholder="search by name">
        <span>
            <div style='margin-bottom:3px; margin-top:3px'>
                <input id='gradient' class='search-btn' type = 'hidden' name = 'search' value = 'search'>
            </div>
        </span>
        <div style="height: auto">
            <table id='responsive_table'>
                <thead>
                    <tr>
                        <th scope="col"><a href="sort">name</a></th>
                        <th scope="col"><a href="sort">sex</a></th>
                        <th scope="col"><a href="sort">user group</a></th>
                        <th scope="col"><a href="sort">date register</a></th>
                    </tr>
                </thead>
                <?php while($row = mysqli_fetch_array($search_result)): ?>
                <tbody>
                    <tr>
                        <td scope="row" data-label='name'><?php echo $row['name']; ?></td>
                        <td data-label='sex'><?php echo $row['gender']; ?></td>
                        <td data-label='user group'><?php echo $row['user_group']; ?></td>
                        <td data-label='date register'><?php echo $row['date_registered']; ?></td>
                    </tr>
                </tbody>
                <?php endwhile; ?>
            </table>
    </div>
</form>
3
  • You mean, you are looking for something like this: datatables.net/examples/styling/bootstrap.html ? Commented Oct 21, 2016 at 7:10
  • Could you use JQuery? Commented Oct 21, 2016 at 7:10
  • exactly @Twinfriends Commented Oct 21, 2016 at 7:34

3 Answers 3

1

Why don't you use order by clause:

SELECT * FROM table ORDER BY column;

Order By Reference

Sign up to request clarification or add additional context in comments.

1 Comment

Also not what he's looking for.
1

You can modified your query as below for sorting:

sql > select * from <table name> order by <column name>;

default sorting is ascending order else for descending you can do like

sql > select * from <table name> order by <column name> desc;

1 Comment

Its not what he's looking for.
1

If you could use JQuery, it's very simple, you have just to add the following javascript code:

$( document ).ready(function() {
    $("#responsive_table").DataTable({
      ordering: true,
      searching: true
    });
});

for a complete example see the following code:

<!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>JS Bin</title>
      <script src="https://code.jquery.com/jquery-3.1.0.js"></script>
      <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>
      <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css">
      <script>
        $( document ).ready(function() {
            $("#responsive_table").DataTable({
              ordering: true,
              searching: true
            });
        });
      </script>
    </head>
    <body>
    <form method='post' action=''>
                <div style="height: auto">
                <table id='responsive_table'>
                    <thead>
                        <tr>
                            <th scope="col">name</th>
                            <th scope="col">sex</th>
                            <th scope="col">user group</th>
                            <th scope="col">date register</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td scope="row" data-label='name'>HERE</td>
                            <td data-label='sex'>Your</td>
                            <td data-label='user group'>data</td>
                            <td data-label='date register'>loaded</td>
                        </tr>
                      <tr>
                            <td scope="row" data-label='name'>via</td>
                            <td data-label='sex'>PHP</td>
                            <td data-label='user group'>loop</td>
                            <td data-label='date register'>bye</td>
                        </tr>
                    </tbody>
                </table>
                </div>
            </form>
    </body>
    </html>

2 Comments

Yay. The only answer that is correct and really helps TO... and no upvotes for you. But the other two guys recive upvotes with answers that nobody cares about. Whats wrong today? At least, nice answer Alessandro. You're the only one who understood this question. Think the other ones just read the title.
Thanks @Twinfriends

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.