Compare the data in the array and count the number of instances it encounters in the database using codeigniter

I have this month of the month $monthsNum = ['1','2','3','4','5','6','7','8','9','10','11','12'];and I want it to be compared in the month_uploaded database column with values ​​from 1 to 12. I want it to count the number of instances of the values ​​of the $ monthNum array in the month_uploaded column and store it in the $ count_uploads array . placed with a null value in the $ count_uploads array. How should I do it? Your help is greatly appreciated. Thank. Below are snippets of my codes.

enter image description here

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);

}

Output: False

array (size=5)
0 => int 1
1 => int 1
2 => int 1
3 => int 1
4 => int 1

Output Required:

 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

This is closer, you just need to get the correct count value for each array key

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);

}

Output:

 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
+4
4

, , array_count_values. , ->get()->result_array();

$array = range(1,12);
$result = array(7,7,7,6);
$final = array_count_values($result);
$final_array = array();
foreach($array as $value){
    $final_array[$value] = array_key_exists($value ,$final) ? $final[$value] : 0;
}
print_r($final_array);

:

$arr = array(0 => array ('month_uploaded' =>  '7'),  1 => array ('month_uploaded' =>  '7'),  2 => array ('month_uploaded' =>  '7'), 3 => array ('month_uploaded' =>  '7'),  4 => array ('month_uploaded' =>  '6' ));
$result = array_count_values(array_column($arr,'month_uploaded'));
$final_array = array();
foreach($array as $value){
    $final_array[$value] = array_key_exists($value ,$result) ? $result[$value] : 0;
}
print_r($final_array);

Fiddle

+1

.

$count_uploads[$row->month_uploaded][] = $row->month_uploaded;

.

echo '<pre>';
print_r($count_uploads);
echo '</pre>';

array(
  3 => array(3, 3),
  4 = array()
)
+1
Use following functions and get your result.

function count_uploads_perMonth(){

$monthsNum = array('1','2','3','4','5','6','7','8','9','10','11','12');
$this->db->select("count(month_uploaded) as cnt");
$this->db->where_in('month_uploaded',$monthsNum);
$this->db->group_by('month_uploaded');
$query = $this->db->get('$this->table_par');


  foreach( $query->result() as $row ){

    $count_uploads[] = $row->cnt;

  }
  echo "<pre>";
  print_r($count_uploads);

 }
+1
source

Change the SQL query to

SELECT month_uploaded-1 `month`, COUNT(*) `count` FROM table_name GROUP BY month_uploaded

you will get (5.1), (6.4). Then

// Prepare array with zeros
$count_uploads = array_fill(0, 12, 0);

foreach( $query->result() as $row )
   $count_uploads[$row->month] = $row->count;
+1
source

Source: https://habr.com/ru/post/1598660/


All Articles