使用php/codeigntier按月组织mysql数据

brtdzjyr  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(306)

我需要按月份组织用户登录日期。目前,我在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)'); 但它只返回数据库中的月数,而不是每个月的登录数。

v1uwarro

v1uwarro1#

你可以用 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..

当然你可以过滤这个数组。
(到家后,我会尽量补充更多的答案。)
希望我的回答对你有帮助

mgdq6dx1

mgdq6dx12#

试试这个:
您的模型文件

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数组,其中包含月份的名称、开始日期和结束日期,就像这样,然后将其传递给控制器函数

$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');
}
abithluo

abithluo3#

如果你想所有的月和年你的建议 $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
}
?>

相关问题