dongxu1875 2014-03-30 09:35
浏览 10
已采纳

PHP日历在新div中过期日期

I'm creating a calendar/schedule for a client, it's kinda rough since I'm a frontend developer and complete new with php.

This is what I got: http://www.nrg-agency.com/events/

The script is real basic, and it was sorted on most far date first, which I got working now that the upcoming date is sorted on top.

My question is, how could I create that the past events automatically will go in a new div under the schedule with "Past Events"?

This is what I got in my "Functions.php" file:

<?php

function init_db(){

   global $db;

   $sql = "SHOW TABLES FROM $db[db]";
   $result = mysql_query($sql);

   $buildNow = 'yes';

   while($row = mysql_fetch_row($result)){
               if($row[0] == 'joe_cal_events'){
                  $buildNow = 'no';
               }
   }

   if($buildNow == 'yes'){
      mysql_query("
      CREATE TABLE `joe_cal_events` (
                          `id` mediumint(8) NOT NULL auto_increment,
                          `start` date NOT NULL default '2014-30-04',
                          `end` date NOT NULL default '2020-01-01',
                          `title` text NOT NULL,
                          `url` text NOT NULL,
                          `description` text NOT NULL,
                          PRIMARY KEY  (`id`)
                        ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
      ") or die(mysql_error());
   }

}
foreach ($dataArray as $key => $row) {
    $orderByDate[$key]  = strtotime($row['start']);
}

array_multisort($orderByDate, SORT_ASC, $dataArray);

array_multisort($orderByDate, SORT_ASC, $dataArray);

function prettyDate($date,$type){
   list($y,$m,$d) = explode("-", $date);
   if($type == 1){
      return "$m/$d/$y";
   }
   if($type == 2){
      return(date("j F Y",mktime(0,0,0,$m,$d,$y)));
   }
}

?>

And this in my events.php:

<body>

";


$q = mysql_query("select * from joe_cal_events order by start asc");
while($r = mysql_fetch_array($q)){

   print "
   <div class='feedEkList'><li>
   <div class='itemTitle'>$r[title]</div>
   <div class='itemdate'>".prettyDate($r[start],2)."</div>
   ";
   if($r[description]){
      print "<div class='itemText'>$r[description]</div>";
   }
   print "
   </li>
   </div>
   ";
}

print "
</div>
</body>

Thanks again!

  • 写回答

1条回答 默认 最新

  • dongzhan1948 2014-03-30 09:55
    关注

    Loop through your array.

    $pastevents = array();
    $futureevents = array();
    
    foreach($events as $event) {
        if(strtotime($event['date']) < time()) {
           $pastevents[] = $event;
        } else {
           $futureevents[] = $event;
    }
    

    Now you have two arrays, one containing your past, and one containing your future events.

    In your case replace:

    while($r = mysql_fetch_array($q)){
    

    with:

    $pastevents = array();
    $futureevents = array();
    
    while($r = mysql_fetch_array($q)){
        if(strtotime($r['date']) < time()) {
            $pastevents[] = $r;
        } else {
            $futureevents[] = $r;
        }
    }
    
    foreach($futureevents as $event) {
       // your display logic
    }
    
    foreach($pastevents as $event) {
        // your display logic
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题