dongleiqiao2107 2017-01-24 02:20
浏览 31
已采纳

基于CodeIgniter上的某些字段创建数据数组

I try to display the array data in the table and it went perfectly, but how to display the data array based on a particular field with major field name below is an example of the data in the table.

Example data

  |   Name  |   Option  |   Value       
===============================
  | Ahmad   |   High    |   90
  | Smith   |   Low     |   43
  | John    |   Low     |   55
  | Adam    |   High    |   79
  | Smith   |   High    |   80
  | Adam    |   Low     |   55
  | Ahmad   |   Low     |   48
  | John    |   High    |   90

And following the data that I expected

            |       Option     |
================================            
  |   Name  |   High  |   Low  |     
===============================|
  | Ahmad   |   90    |   48   |
  | Smith   |   80    |   43   |
  | John    |   90    |   55   |
  | Adam    |   79    |   55   |

Controllers

function detail_data() {            
        $data = array(
            'detail_option' => $this->my_model->model_detail($metadata_code),
        );                  
    $this->load->view('my_view', $data);
}

Models

function model_detail() {
    $query = $this->db->select('*')
                      ->from('tb_student')
                      ->group_by('name')
                      ->get();      

    if ($query->num_rows() > 0) {
        foreach ($query->result() as $data) {
            $result_data[] = $data;
        }
        return $result_data;
    } 
}

Views

<table>
    <thead>
    <tr>
        <td rowspan="2">Name</td>
        <td colspan="2">Option</td>
    </tr>
    <tr>
        <td>High</td>
        <td>Low</td>
    </tr>
    </thead>
    <tbody>
    <?php
        foreach ($detail_option as $row) {  
        echo '<tr>';
            echo  '<td>'.$row->name.'</td>';
            echo  '<td>'.$row->high.'</td>';
            echo  '<td>'.$row->low.'</td>'; 
        echo '</tr>';   
        }
    ?>              
    </tbody>
</table>
  • 写回答

1条回答 默认 最新

  • dongza3124 2017-01-24 02:46
    关注

    Controller:

    function detail_data() {            
            $data = array(
                'detail_option' => $this->my_model->model_detail()
            );                  
        $this->load->view('my_view', $data);
    }
    

    Model:

    function model_detail() {
        $query = $this->db->query("SELECT * FROM `tb_student`");
    
        $result_data = array();
    
        if ($query->num_rows() > 0) {
            foreach ($query->result() as $data) {
                $result_data[$data->Name]['Name'] = $data->Name;
                $result_data[$data->Name][$data->Option] = $data->Value;
            }
            return $result_data;
        }
    }
    

    HTML:

    <table>
        <thead>
        <tr>
            <td rowspan="2">Name</td>
            <td colspan="2">Option</td>
        </tr>
        <tr>
            <td>High</td>
            <td>Low</td>
        </tr>
        </thead>
        <tbody>
        <?php
            foreach ($detail_option as $row) {  
            echo '<tr>';
                echo  '<td>'.$row['Name'].'</td>';
                echo  '<td>'.$row['High'].'</td>';
                echo  '<td>'.$row['Low'].'</td>'; 
            echo '</tr>';   
            }
        ?>              
        </tbody>
    </table>
    

    Is this something you wanted?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?