dongshi2458 2014-04-12 04:30
浏览 31
已采纳

从Mysql表转换时,日期失去位置

My problem is that when I display my SQL table, the date is in a different order as it is on the SQL table, so the date does not correspond with the eid.

The SQL table has the following order.

EID 72: DATE: 2014-04-08 SUBJECT: 3rd event

EID 70: DATE: 2014-04-02 SUBJECT: 1st one

EID 69: DATE: 2014-04-01 SUBJECT: 2nd Event

EID 71: DATE: 2014-03-31 SUBJECT: Fourth event

However it displays in html as

EID: 71 DATE: 2014-04-08 SUBJECT: 3rd event

EID: 72 DATE: 2014-04-01 SUBJECT: Fourth event

EID: 69 DATE: 2014-04-02 SUBJECT: 1st one

EID: 70 DATE: 2014-03-31 SUBJECT: 2nd Event

For some reason when translating from SQL through PHP, the date loses it's order. Here is my php code which displays the results

$conn = mysql_connect("localhost","admin","password") or die ("could not connect to the server");
        mysql_select_db("app") or die ("that database could not be found");

        $result = mysql_query("SELECT * FROM  `notification` WHERE `date` AND `active`=0 < NOW()") or die ("The query could not be completed. Please try again later");

        while($noticerow=mysql_fetch_array($result, MYSQL_ASSOC))
        {
            $nid[] = $noticerow['nid'];
            $eid[] = $noticerow['eid'];
            $subject[] = $noticerow['subject'];
            $noticedate[] = $noticerow['date'];
        }

        $a=0;
        while(mysql_num_rows($result)>$a)
        {
            echo "<tr><td><a onclick='location.href=\"calendar.php?action=display_event&oid=".$eid[$a]."\"'>".$eid[$a]."</a></td><td><a onclick='location.href=\"calendar.php?action=display_event&oid=".$eid[$a]."\"'>".$noticedate[$a]."</a></td><td><a onclick='location.href=\"calendar.php?action=display_event&oid=".$eid[$a]."\"'>".$subject[$a]."</a></td><td><form action='rnotice.php' method='POST'><button value='submit' class='btn btn-danger pull-right' type='submit' onclick='location.href=\"calendar.php?action=display_event&oid=".$eid[$a]."\"'>Remove</button><input name='eid' type='hidden' value=".$eid[$a]."></form></td></tr>";
        $a++;
        }
  • 写回答

1条回答 默认 最新

  • dougudu3564 2014-04-12 04:42
    关注

    you need to use the order by keyword in the SQL statement but I don't know the columns name you are using.

    Assuming that you want to order them by EID and you have a column name called EID for that your select statement should be something like:

    SELECT * FROM  `notification` WHERE `date` AND `active`=0 < NOW() order by eid ASC
    

    if you want to sort it by date then

    SELECT * FROM  `notification` WHERE `date` AND `active`=0 < NOW() order by `date` ASC
    

    To sort the records in a descending order, you can use the DESC keyword instead of ASC

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100