如何在php中根据时间从数据库中获取记录

时间:2015-02-12 10:18:01

标签: php mysql

我从数据库中获取记录,列表应按时间排序。最新的预约请求应位于顶部。现在它按文件编号排序。     这是我的代码.....我希望首先根据时间显示最近的记录,例如,如果我今天添加2条记录意味着这两条记录应该首先基于时间.......

<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";
    $resultPatient=$db->select($query);
    $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>File No</th>
                <th>Name</th>
                <th>Email</th>
                <th>Age</th>
                <th>Gender</th>
                <th>Phone</th>
                <th>Date</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 $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['phone'];?></td>
                    <td class="center"><?php echo $req_date;?></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>

3 个答案:

答案 0 :(得分:0)

您可以将orderby用于此目的,例如

$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 time DSC";

答案 1 :(得分:0)

$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.time DESC";

答案 2 :(得分:0)

按DESC排序

应该适合你。

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 time desc

使用ASC将以升序顺序提供,DESC将以降序

提供