Combining 'id' based on different years in the date field

Report
Question

Please briefly explain why you feel this question should be reported .

Report Cancel

I have one table with 3 value with the same id, id each have a value with type a different date.

  id   |   name  |    date    
------ | ------- | ----------- 
  1    |  smith  |  2013-10-12 
  2    |  smith  |  2015-09-09  
  3    |  smith  |  2016-01-10  

I want to display the data in tabular form based on the year, .

  id   |  name  |     2014    |     2015     |     2016
------ | ------ | ----------- | ------------ | ----------- 
  1    |  smith |  2014-10-12 |  2015-09-09  |  2016-01-10 

but after I run the appropriate script I compose the result is not as I expected.

  id   |  name  |     2014    |     2015     |     2016
------ | ------ | ----------- | ------------ | ----------- 
  1    |  smith |  2014-10-12 |       -      |       -
  2    |  smith |      -      |  2015-09-09  |       - 
  3    |  smith |      -      |       -      |  2016-01-10 

is there who can help me to solve this problem? I use CodeIgniter framework.
This my script:

MY Controllers

public function index()
{
    $data['deptlist'] = $this->mcrud->viewkal();
    $data=array('deptlist'  => $this->mcrud->viewkal(),
                'isi'       =>'kalibrasi/v_kalibrasi');
    $this->load->view('layout/wrapper', $data); 

}  

MY Models

function viewkal()  {
    $this->db->order_by('name','ASC');
    $this->db->group_by('name');
    $query = $this->db->get('mytabel');  
    return $query->result();                             
}

MY Views

<table>
  <thead>
    <tr>
        <th>Name</th>
        <th><?php echo date('Y', strtotime('-2 year')); ?></th>  
        <th><?php echo date('Y', strtotime('-1 year')); ?></th> 
        <th><?php echo date('Y', strtotime('0 year')); ?></th>  
    </tr>
  </thead>
  <tbody>
    <?php
        foreach ($deptlist as $row ) {
    ?>
    <tr>
        <td><?php echo $row->id_alat; ?></td>
        <td><?php echo $row->nama_alat; ?></td>
        <?php 
            $result = $row->tanggal;
            $data = strtotime($row->tanggal);  
            $tahun = date('Y',$data);

            if ( $tahun == date('Y', strtotime('-3 year'))) {
                echo '<td>'.$result.'</td>';
            } else {
                echo '<td> - </td>';
            }

            if ( $tahun == date('Y', strtotime('-2 year'))) {
                echo '<td>'.$result.'</td>';
            } else {
                echo '<td> - </td>';
            }

            if ( $tahun == date('Y', strtotime('-1 year'))) {
                echo '<td>'.$result.'</td>';
            } else {
                echo '<td> - </td>';
            }           

            if ( $tahun == date('Y', strtotime('0 year'))) {
                echo '<td>'.$result.'</td>';
            } else {
                echo '<td> - </td>';
            }

            if ( $tahun == date('Y', strtotime('-1 year'))) {
                echo '<td>'.$result.'</td>';
            } else {
                echo '<td> - </td>';
            }
        ?>
    <?php } ?>
    </tr>
  </tbody>

0
0 Answer 30 views 0