使用php / codeigntier

时间:2018-05-15 20:40:15

标签: php mysql codeigniter

我需要按月组织用户登录日期。目前,我在代码点火器中这样做:

我的型号代码:

public function getApril(){
  $this->db->select('*');
  $this->db->where('Date >=','2018-04-01');
  $this->db->where('Date <=','2018-04-30');
  $this->db->from('user_data');
  $count = $this->db->count_all_results();
  return $count;
}

public function getMarch(){
  $this->db->select('*');
  $this->db->where('Date >=','2018-03-01');
  $this->db->where('Date <=','2018-03-31');
  $this->db->from('user_data');
  $count = $this->db->count_all_results();
  return $count;
}

public function getFebruary(){
  $this->db->select('*');
  $this->db->where('Date >=','2018-02-01');
  $this->db->where('Date <=','2018-02-28');
  $this->db->from('user_data');
  $count = $this->db->count_all_results();
  return $count;
}

我的控制器代码:

public function index(){
  $data["april"]= $this->login_model->getApril();
  $data["march"]= $this->login_model->getMarch();
  $data["february"]= $this->login_model->getFebruary();
  $this->load->view('main/header');
  $this->load->view('main/body', $data);
  $this->load->view('main/footer');
}

在我看来,我只是使用echo来显示结果,如下所示:

<p><strong><?php echo $april?></strong></p>

这似乎是一种非常冗余的方法。我确信有更好的办法解决这个问题。

我尝试使用'group_by',如此:$this->db->group_by('MONTH(Date)');但只返回数据库中的月数,而不是每个月的登录次数。

4 个答案:

答案 0 :(得分:2)

您可以使用MONTH()方法

只需使用一个功能即可访问数据

public function getMonth($theMonthNumber){
  $this->db->select('*');
  $this->db->where('MONTH(Date)', $theMonthNumber);
  $this->db->from('user_data');
  $count = $this->db->count_all_results();
  return $count;
}

而且,如何统计每月登录?

尝试使用此查询

SELECT MONTH(Date) as the_month, COUNT(*) as the_value
FROM user_data
WHERE user_data.user_id = "the_user_id_i_send"
GROUP BY MONTH(Date)

如果您在codeigniter中使用此功能,则该功能将如下所示

function some_function_name($user_id){
     $query = $this->db->query('the same query i wrote before')
           ->result_array();
     return $query;
 }

预期输出应该是一个包含与月份相关的关联键的数组,示例输出

  echo json_encode($query[0]); //the first month where the user has registers
  echo json_encode($query[3]); //the third month....and so on.. 

当然你可以过滤这个数组。

(当我回到家时,我会尝试补充更多答案。)

希望我的回答可以帮助你

答案 1 :(得分:0)

这里试试这个:

您的模型文件

public function getDataByMonth($start, $end){
  $this->db->select('*');
  $this->db->where('Date >=', $start);
  $this->db->where('Date <=', $end);
  $this->db->from('user_data');
  $count = $this->db->count_all_results();
  return $count;
}

然后创建一个包含月份名称,开始日期和结束日期的月份数组,然后将其传递给控制器​​函数

$months = array(
    "january"   =>  array("01-01-2018", "31-01-2018"),
    "february"  =>  array("01-02-2018", "28-02-2018"),
    "march"     =>  array("01-03-2018", "31-01-2018")
);

最后在你的控制器中

public function index($months){
    foreach ($months as $key => $value) {
        $data[$key] = $this->login_model->getDataByMonth($value[0], $value[1]);
    }
  $this->load->view('main/header');
  $this->load->view('main/body', $data);
  $this->load->view('main/footer');
}

答案 2 :(得分:0)

如果你想要所有月份和年份,你对$this->db->group_by('MONTH(Date)')的建议非常接近

这样的东西

模型

public function getDataByMonth()
{
    $query = $this->db
        ->select('count(*) AS count, MONTH(Date) AS month, YEAR(Date) AS year')
        ->from('user_data')
        ->group_by('MONTH(Date)')
        ->group_by('YEAR(Date)')
        ->order_by('YEAR(Date)', 'DESC')
        ->order_by('MONTH(Date)', 'DESC')
        ->get();

    return $query->result();
}

控制器

public function index(){
    $this->load->view('main/header');
    $this->load->view('main/body', ['data' => $this->login_model->getDataByMonth()]);
    $this->load->view('main/footer');
}

视图

<?php
foreach ($data AS $obj)
{
    $objDateTime = DateTime::createFromFormat('n', $obj->month);
?>
    <p><strong><?php echo $objDateTime->format('F').' '.$obj->year.' | '.$obj->count; ?></strong></p>
<?php
}
?>  

答案 3 :(得分:0)

$query = $this->db
        ->select('count(*) AS count, MONTH(Date) AS month, YEAR(Date) AS year')
         ->group_by('MONTH(Date)')
        ->group_by('YEAR(Date)')
        ->order_by('YEAR(Date)', 'DESC')
        ->order_by('MONTH(Date)', 'DESC')
        ->get("tablename");