user17395241
user17395241

Reputation:

Descending order is not working in Datatable when coming from database

I am fetching the value from the database:

Array
(
    [0] => stdClass Object
        (
            [company_email] => [email protected]
            [schedule_date] => 2022-01-10
            [candidate_name] => sathish sat
        )

    [1] => stdClass Object
        (
            [company_email] => [email protected]
            [schedule_date] => 2022-01-10
            [candidate_name] => sathish sat
        )

    [2] => stdClass Object
        (
            [company_email] => [email protected], [email protected]
            [schedule_date] => 2022-01-05
            [candidate_name] => sathish sat
        )

    [3] => stdClass Object
        (
            [company_email] => [email protected]
            [schedule_date] => 2021-12-21
            [candidate_name] => Sai Surendra Datar
        )

    [4] => stdClass Object
        (
            [company_email] => [email protected]
            [schedule_date] => 2021-12-21
            [candidate_name] => Rahul Raj
        )

    [5] => stdClass Object
        (
            [company_email] => [email protected]
            [schedule_date] => 2021-12-21
            [candidate_name] => Dilpreet kaur
        )

    [6] => stdClass Object
        (
            [company_email] => [email protected]
            [schedule_date] => 2021-12-21
            [candidate_name] => Sai Surendra Datar
        )

    [7] => stdClass Object
        (
            [company_email] => [email protected]
            [schedule_date] => 2021-12-21
            [candidate_name] => Rahul Raj
        )

    [8] => stdClass Object
        (
            [company_email] => [email protected]
            [schedule_date] => 2021-12-21
            [candidate_name] => Rahul Raj
        )

    [9] => stdClass Object
        (
            [company_email] => [email protected], [email protected]
            [schedule_date] => 2021-12-18
            [candidate_name] => Sai Surendra Datar
        )

    [10] => stdClass Object
        (
            [company_email] => [email protected], [email protected]
            [schedule_date] => 2021-12-18
            [candidate_name] => sathish sat
        )

    [11] => stdClass Object
        (
            [company_email] => [email protected]
            [schedule_date] => 2021-12-18
            [candidate_name] => sathish sat
        )

)

But when showing in the datatable order are mismatching refer the image URL I have mentioned

Display of the datatable

enter image description here

This is my code:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.css" />
<link rel="stylesheet" href="https://cdn.datatables.net/1.11.3/css/dataTables.bootstrap4.min.css" />
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.11.3/js/dataTables.bootstrap4.min.js"></script>
<table id="example" class="table table-striped table-bordered" style="width:100%">
        <thead>
            <tr>
                <th>Candidate Name</th>
                <th>Company Email</th>
                <th>Schedule Date</th>
            </tr>
        </thead>
        <tbody>
            <?php $x = 1; foreach ($candidate_list as $record) { ?>
                            <tr> 
                                <td style="text-align: left;color: #5e5e5e;"><?php echo $record->candidate_name; ?></td>
                                <td style="text-align: left;color: #5e5e5e;"><?php echo $record->company_email; ?></td> 
                                <td class="sorting sorting_asc sorting_1" style="text-align: left;color: #5e5e5e;"><?php echo date('d-m-Y', strtotime($record->schedule_date)); ?></td>
                                </td>
                            </tr>
                        <?php $x++;} ?>
        </tbody>
       
    </table>
    <script type="text/javascript">
        $(document).ready(function() {
    $('#example').DataTable();
} );
    </script>

I need the order as from the database. How to solve this?

Upvotes: 1

Views: 722

Answers (1)

Yazan Fouad Aldbaisy
Yazan Fouad Aldbaisy

Reputation: 621

Replace your script with this

$(document).ready(function() {
    $('#example').DataTable( {
        "order": [[ 2, "desc" ]]
    } );
} );

Upvotes: 1

Related Questions