Arun
Arun

Reputation: 97

how to show sorting for jquery datatable in DESC

I'm displaying data from database in JQuery data tables.I want to display latest inserted record in top. My SQL query is working fine,but however JQuery data tables sorting is not happening.....so I want to sort data DESC order for data tables. Where I have to change in plugins? I'm using the following plugins:

jquery.dataTables.js, jquery.resizable.js, jquery.sortable.js

This is my code:

Appointments.php

            <head>
            <!--<meta http-equiv="refresh" content="30" />
            --></head>
            <?php
                include_once 'config.php';
                $menu_name = 'patient'; 
                $_SESSION['menu']= $menu_name;  
                if(isset($_REQUEST['id']) && $_REQUEST['id']!='')
                {
                    if(isset($_REQUEST['act']) && $_REQUEST['act']!='')
                    {
                        if($_REQUEST['act']=='r')
                        {
                            $id = $_REQUEST['id'];
                            $deleteCondition = " id_patient='".$id."'";
                            $deleted=$db->delete('patients',$deleteCondition);
                            header("Location: appointments.php?m=r");
                        }
                    }
                }
               /*echo $query="SELECT p.*,u.fileno FROM patients AS p,users AS u WHERE p.status='a' AND p.users_id_user=u.id_user order by p.id_patient DESC";*/
              /*echo  $query="select p.*,u.fileno,s.title from patients as p INNER JOIN users as u ON p.users_id_user=u.id_user
               left join services as s ON p.service_id=s.id_service where p.status='a'"; */
                echo  $query="select p.*,u.fileno,s.title from patients as p INNER JOIN users as u ON p.users_id_user=u.id_user
               left join photos as s ON p.service_id=s.id_photo where p.status='a' order by p.id_patient DESC";
               $resultPatient=$db->select($query);


               //$qry="select p.*,s.title from patients as p,service as s innser join services on p.service_id=s.id_service";
                //$resultPatient=$db->select($qry);
                $msgConfirmDelete ="Do you really want to delete this Patient ?";
                include_once 'header.php';
            ?>
            <script type="text/javascript">
             var msgDelete = '<?php echo $msgConfirmDelete; ?>';

             function confirmDelete()
             {
             if (confirm(msgDelete))
             {
             return true;
             }
             else
             {
             return false;
             }
             }
             </script> 

            <!-- Sidebar begins -->
            <div id="sidebar">

                <!-- Left Main menu begins -->
                    <?php include_once 'menu.php'; ?>
                <!-- Left Main menu ends -->


                <!-- Secondary nav --> 
                <div class="secNav">
                    <div class="secWrapper">
                        <!--div class="secTop">
                        </div-->
                        <!--div class="divider"></div-->
                        <div id="general">
                            <div id="general" style="display: block;" class="active">
                            <ul class="subNav">
                                <li><a href="add_appointment.php" title=""><span class="icos-images2"></span>Add Appointment</a></li>
                                <li class="activeli"><a href="appointments.php" title="" class="this"><span class="icos-images2"></span>View/Edit Appointments</a></li>
                                <li><a href="appointments_history.php" title=""><span class="icos-images2"></span>Appointments History</a></li>
                                <li><a href="reminder_settings.php" title=""><span class="icos-images2"></span>View/Edit Reminder Settings</a></li>
                                <li><a href="add_reminder_settings.php" title=""><span class="icos-images2"></span>Add Reminder Settings</a></li>
                                <li><a href="medicine_reminder_settings.php" title=""><span class="icos-images2"></span>View/Edit Medicine Reminder Settings</a></li>
                                <li><a href="add_medicine_reminder_settings.php" title=""><span class="icos-images2"></span>Add Medicine Reminder Settings</a></li>
                            </ul>
                        </div>   
                        </div> 
                   </div> 
                   <div class="clear"></div>
               </div>
            </div>
            <!-- Sidebar ends -->


            <!-- Content begins -->
            <div id="content">
                <div class="contentTop">
                    <span class="pageTitle"><span class="icon-screen"></span>View Appointments</span>
                    <div class="clear"></div>
                </div>

                <!-- Breadcrumbs line -->
                <div class="breadLine">
                    <div class="bc">
                        <ul id="breadcrumbs" class="breadcrumbs">
                            <li><a href="index.php">Dashboard</a></li>
                            <li><a href="appointments.php">Appointments</a></li>
                            <li class="current"><a href="#" title="">View Appointments</a></li>
                        </ul>
                    </div>
                </div>
                <!-- Main content -->
                <div class="wrapper">


                    <div class="fluid">
                        <?php if(isset($_GET['m'])&&$_GET['m']=="a"){?>
                       <div class="nNote nSuccess">
                      <p>Appointment Added Successfully !</p>
                      </div>
                       <?php  }
                       if(isset($_GET['m'])&&$_GET['m']=="d"){?>
                       <div class="nNote nSuccess">
                      <p>Appointment Status Changed Successfully !</p>
                      </div>
                      <?php  }
                       if(isset($_GET['m'])&&$_GET['m']=="r"){?>
                       <div class="nNote nSuccess">
                      <p>Appointment Deleted Successfully !</p>
                      </div>
                      <?php  }
                       if(isset($_GET['m'])&&$_GET['m']=="re"){?>
                       <div class="nNote nSuccess">
                      <p>Reminder Sent Successfully !</p>
                      </div>
                      <?php } 
                       if(isset($_GET['m'])&&$_GET['m']=="u"){?>
                       <div class="nNote nSuccess">
                      <p>Appointment Updated Successfully !</p>
                      </div>
                      <?php } ?>
                    <div class="widget">
                        <div class="whead"><h6>View Appointment Information</h6><div class="clear"></div></div>
                        <div id="dyn2" class="shownpars">
                            <a class="tOptions act" title="Options"><img src="images/icons/options.png" alt="" /></a>
                            <table cellpadding="0" cellspacing="0" border="0" class="dTable">
                            <thead>
                            <tr>
                            <th>Sl. No.<span class="sorting" style="display: block;"></span></th>
                            <!--<th>Sl.No</th>-->
                            <th>Date</th>
                            <th>File No</th>
                            <th>Name</th>
                            <th>Email</th>
                            <th>Age</th>
                            <th>Gender</th>
                            <th>Service</th>
                            <th>Phone</th>
                            <th>Time</th>
                            <th width="80">Action</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php
                            $sl=0;
                            $req_date='';   
                            if($resultPatient && count($resultPatient)>0)
                            foreach($resultPatient as $patient)
                            {
                                $sl+=1;                    
                                $req_date=date('d F Y', strtotime($patient['date']));
                            ?>
                                <tr class="gradeX">
                                <td class="center"><?php echo $sl;?></td>
                                <td class="center"><?php echo $req_date;?></td>
                                <td class="center"><?php echo $patient['fileno'];?></td>
                                <td class="center"><?php echo $patient['name'];?></td>
                                <td class="center"><?php echo $patient['email'];?></td>
                                <td class="center"><?php echo $patient['age'];?></td>
                                <td class="center"><?php echo strtoupper($patient['gender']);?></td>
                                <td class="center"><?php echo $patient['title'];?></td>
                                <td class="center"><?php echo $patient['phone'];?></td>
                                <td class="center"><?php echo $patient['time'];?></td>
                                <td class="tableActs">
                                <a href="edit_appointment.php?id=<?php echo $patient['id_patient'];?>" class="tablectrl_small <?php if($patient['confirm_status']=='c') echo 'bGreen'; else echo 'bDefault';?> tipS" title="Edit"><span class="iconb" data-icon="&#xe1db;"></span></a>
                                <a onclick="return confirmDelete();" href="appointments.php?id=<?php echo $patient['id_patient'];?>&act=r" class="tablectrl_small bDefault tipS" title="Delete"><span class="iconb" data-icon="&#xe136;"></span></a>
                                <a href="solveAppointments.php?id=<?php echo $patient['id_patient'];?>" class="lightbox tablectrl_small bDefault tipS" title="Status"><span class="iconb" data-icon="&#xe047;"></span></a>
                                </td>
                                </tr>
                                <?php }?>                
                            </tbody>
                            </table> 
                        </div>
                        <div class="clear"></div> 
                    </div> 

                    </div>
                </div>
                <!-- Main content ends -->

            </div>
            <!-- Content ends -->

            </body>
            </html>

Upvotes: 1

Views: 172

Answers (1)

Alankar More
Alankar More

Reputation: 1115

You can do this in following way. may be it helps you.

 $(document).ready(function() {  var oTable = $('#example').dataTable(); oTable.fnSort( [ [0,'asc'], [1,'asc'] ] );} );

You can read more options here and click on "fnSort" for sorting details.

Upvotes: 1

Related Questions