
function count_uploads_perMonth(){
$monthsNum = ['1','2','3','4','5','6','7','8','9','10','11','12'];
$query = $this->db->select("*")
->from($this->table_par)
->where_in("month_uploaded",$monthsNum)
->get();
foreach( $query->result() as $row ){
$count_uploads[] = count($row->month_uploaded);
}
var_dump($count_uploads);
}
Вывод: неправильный
array (size=5)
0 => int 1
1 => int 1
2 => int 1
3 => int 1
4 => int 1
Желаемый результат:
array (size=12)
0 => 0 or null
1 => 0 or null
2 => 0 or null
3 => 0 or null
4 => 0 or null
5 => 1
6 => 3
7 => 0 or null
8 => 0 or null
9 => 0 or null
10 => 0 or null
11 => 0 or null
Это ближе, просто нужно получить правильное значение счетчика для каждого ключа массива.
function count_uploads_perMonth(){
$monthsNum = array('1','2','3','4','5','6','7','8','9','10','11','12');
$this->db->select("month_uploaded as cnt");
$this->db->where_in('month_uploaded',$monthsNum);
$this->db->group_by('month_uploaded');
$query = $this->db->get($this->table_par);
$count_uploads = array_fill(1, 12, 0);
foreach( $query->result() as $row ){
$count_uploads[$row->cnt] = $row->cnt;
}
var_dump($count_uploads);
}
Вывод:
array (size=12)
1 => int 0
2 => int 0
3 => int 0
4 => int 0
5 => int 0
6 => string '6' (length=1) --- value should be 1 and length is 1
7 => string '7' (length=1) --- value should be 4 and length is 4
8 => int 0
9 => int 0
10 => int 0
11 => int 0
12 => int 0
Подробнее здесь: https://stackoverflow.com/questions/315 ... he-databas
Мобильная версия