【问题标题】:How to count the date to make a list of events?如何计算日期以列出事件?
【发布时间】:2013-08-18 15:34:05
【问题描述】:

我正在使用 Codeigniter V2.1.3,我有一个日历模板,其中包含日程安排内容,我想计算特定日期的事件。这是我的数据库:

              ----Table: schedule -----
   id  | materialID  | borrowerID | date_reserve |
   ------------------------------------------------
   1   |     7       |     7      |  2013-08-16  |
   2   |     10      |     10     |  2013-08-16  |
   1   |     12      |     13     |  2013-08-18  |

我想要的是在我的日历模板中,date=2013-08-16 的总事件将是 2 events。这是我的代码不起作用,因为它一直只向我发送1 event 也许你可以找出我的错误在哪里:

    $query = $this->db->select('*')->from('schedule')->like('date_reserve', "$year-$month")->get();
    $cal_data = array();
    foreach ($query->result() as $row) {
        $index = ltrim(substr($row->date_reserve,8,2), '0'); 
        $cal_data[$index] = count($row->borrowerID). 'event(s)';
    }
    return $cal_data;

有什么帮助吗?

【问题讨论】:

  • 使用 SELECT COUNT(*) 代替,如果您想要完整日期的结果,请在 where 语句中提供完整日期,否则如果您只想要年份和月份,您的 like 语句应该类似于'2013-08%' 匹配该月中的任何一天。

标签: mysql codeigniter calendar codeigniter-2


【解决方案1】:

如果您想要确切日期的事件数:YYYY-MM-DD,您可以替换为 where:

$r = $this->db->select('id, materialID, borrowerID, date_reserve')
              ->where('date_reverse', $year."-".$month."-".$day)
              ->get('schedule')
              ->result();

您也可以 print_r 结果,这样您就可以看到结果是什么。您也可以回显最后一个查询:echo $this->db->last_query();

编辑: 我们必须在每个日期运行查询以获取事件数。

$cal_data = array();
    for($i=1;$i<=31;$i++)
    {
        $this->db->select('COUNT(id) as count,date_reserve,borrowerID');
    $this->db->from('schedule');    
    $this->db->where('date_reserve',"$year-$month-$i"); 
    //$this->db->group_by('date_reserve');    
    $query = $this->db->get();


    foreach ($query->result() as $row) {
        $cal_data[$index] = $row->count. 'event(s)';
    }

【讨论】:

  • 我怎样才能把它放在我的日历模板中?
  • 只需用这个替换您的查询。然后在 foreach 中,您将使用 $r 作为参数:foreach($r as $row)
  • 你从哪里得到的$day?
  • 你从哪里得到的 $year ?
  • 您需要从帖子数据中设置这些变量,或者根据需要设置这些变量
【解决方案2】:

尝试使用group_by

public function get_results($date) {   

    $this->db->select('COUNT(id),date_reserve');
    $this->db->from('schedule');

    $this->db->like('date_reserve',$date);

    $this->group_by('date_reserve');    
    $result = $this->db->get();

    return $result->result_array();

}

【讨论】:

  • @LeonardDrapeza 嗯现在你面临什么问题??
  • 我无法将它放在我的日历模板中。它告诉我1 event chatstep.com/#leonard 先生,你愿意和我一起来吗?
猜你喜欢
  • 2019-08-12
  • 2010-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多