Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP Codeigniter中检索每月1到31之间的num行数据_Php_Codeigniter - Fatal编程技术网

在PHP Codeigniter中检索每月1到31之间的num行数据

在PHP Codeigniter中检索每月1到31之间的num行数据,php,codeigniter,Php,Codeigniter,我试图获取在特定月份之间创建的数据,例如,我试图获取2019年12月创建的数据行总数 这是我的模型代码 //Get january sles public function getJanSale() { $yr = date('Y'); $this->db->where('td_created_at BETWEEN "'. date('d-m-Y', strtotime("1-12-".$yr)). '" and "'. date('d-m-Y', strtotime

我试图获取在特定月份之间创建的数据,例如,我试图获取2019年12月创建的数据行总数

这是我的模型代码

//Get january sles
public function getJanSale()
{
    $yr = date('Y');
    $this->db->where('td_created_at BETWEEN "'. date('d-m-Y', strtotime("1-12-".$yr)). '" and "'. date('d-m-Y', strtotime("31-12-".$yr)).'"');
    $query = $this->db->get('zd_item_downloads');
    return $query->num_rows();
}

结果返回0。同时,我得到了一些12月份创建的数据,这些数据作为当前时间戳插入到我的数据库中。我做错了什么?

对于所有月份的总数:

select
  SUM(CASE month(td_created_at) WHEN 1 THEN 1 ELSE 0 END) AS 'Jan',
  SUM(CASE month(td_created_at) WHEN 2 THEN 1 ELSE 0 END) AS 'Feb',
  SUM(CASE month(td_created_at) WHEN 3 THEN 1 ELSE 0 END) AS 'Mar',
  SUM(CASE month(td_created_at) WHEN 4 THEN 1 ELSE 0 END) AS 'Apr',
  SUM(CASE month(td_created_at) WHEN 5 THEN 1 ELSE 0 END) AS 'May',
  SUM(CASE month(td_created_at) WHEN 6 THEN 1 ELSE 0 END) AS 'Jun',
  SUM(CASE month(td_created_at) WHEN 7 THEN 1 ELSE 0 END) AS 'Jul',
  SUM(CASE month(td_created_at) WHEN 8 THEN 1 ELSE 0 END) AS 'Aug',
  SUM(CASE month(td_created_at) WHEN 9 THEN 1 ELSE 0 END) AS 'Sep',
  SUM(CASE month(td_created_at) WHEN 10 THEN 1 ELSE 0 END) AS 'Oct',
  SUM(CASE month(td_created_at) WHEN 11 THEN 1 ELSE 0 END) AS 'Nov',
  SUM(CASE month(td_created_at) WHEN 12 THEN 1 ELSE 0 END) AS 'Dec'
from 
  zd_item_downloads
单月:

select
  SUM(CASE month(td_created_at) WHEN 12 THEN 1 ELSE 0 END) AS 'Dec'
from 
  zd_item_downloads
根据年度总计数进行更新

$this->db->select("
  SUM(CASE year(td_created_at) WHEN " . date("Y") . " THEN 1 ELSE 0 END) AS 'current_year_total'")
->from('zd_item_downloads')
管理php日期(“y”)函数以获取上一年的计数

针对2019年12月的具体情况

$this->db->select("
      SUM(CASE month(td_created_at) WHEN 12 THEN 1 ELSE 0 END) AS 'Dec'")
->from('zd_item_downloads')
->where("td_created_at BETWEEN '" . date("Y") . "/01/01' AND '" . date("Y") . "/12/31'")

您可以在这里尝试以下代码

    public function getJanSale()
    {
        $year = date('Y');
        $month = date('m');
        $this->db->where('MONTH(td_created_at) = '. $month. ' AND YEAR(td_created_at) = '. $year);
        $query = $this->db->get('zd_item_downloads');
        return $query->num_rows();
    }
    public function getSale()
    {
        $year = date('Y');
        $month = date('m');
        $this->db->where('MONTH(td_created_at) = '. $month. ' AND YEAR(td_created_at) = '. $year);
        $query = $this->db->get('zd_item_downloads');
        return $query->num_rows();
    }
Mysql函数
CURDATE()


您可以在这里尝试以下代码

    public function getJanSale()
    {
        $year = date('Y');
        $month = date('m');
        $this->db->where('MONTH(td_created_at) = '. $month. ' AND YEAR(td_created_at) = '. $year);
        $query = $this->db->get('zd_item_downloads');
        return $query->num_rows();
    }
    public function getSale()
    {
        $year = date('Y');
        $month = date('m');
        $this->db->where('MONTH(td_created_at) = '. $month. ' AND YEAR(td_created_at) = '. $year);
        $query = $this->db->get('zd_item_downloads');
        return $query->num_rows();
    }

我希望以年度基准为基础,例如,先生,我想展示2019年12月的销售情况,因此,当d年cahbgrs到2020年12月时,我以实际销售为基础year@Zubdev根据两个场景更新了答案您的答案不合适请解释您的陈述。根据@Zubdev想要的结果,您的ans不正确。您的查询将提供不必要的all row with all column,而我的查询只返回一行with count column