Home > OS >  how to combine month wise data as a single row from database table in php
how to combine month wise data as a single row from database table in php

Time:04-06

i have a website in php codeigniter where i am saving day wise reports of each clients, below is my table:

enter image description here

now i am displaying it in front end like below:

<tr>            
    <th>Customer</th>
    <th>Date</th>
    <th>Quantity</th>
    <th>Price</th>
    <th>Status</th>
</tr>

<?php 
foreach($billing as $bl){
?>
   <tr>
       <td><?=$bl->customer?></td>
       <td><?php echo date('F, Y', strtotime($bl->date));?></td>
       <td><?=$bl->quantity?></td>
       <td><?=$bl->milkprice?></td>
       <td></td>
   </tr>
<?php 
}
?>

my sql is like below:

public function selectbilling()
{
    $this->db->select('*');
    $this->db->from('delivered');
    $query = $this->db->get();
    $result = $query->result();
    return $result;
}

what i want here is to display data month wise in the front end, that is combining all days in the database table of respective clients and make them month wise and make it as a single row, can anyone please tell me how to accomplish this, thanks in advance

CodePudding user response:

You can try to replace selectbilling() function with:

 public function selectbilling()
 {
    $this->db->select('*, SUM(milkprice) as milkprice, SUM(quantity) as quantity');
    $this->db->from('delivered');
    $this->db->group_by('YEAR(date), MONTH(date), customer');
    $query = $this->db->get();
    $result = $query->result();
    return $result;
}
  • Related