dongshan4316 2011-02-11 13:58
浏览 42
已采纳

将具有多个字段的数组传递给codeigniter中的视图

I'm using CodeIgniter and I'm struggling with arrays, I need to do a mysql db lookup in the controller and then pass an array to the view so I can loop through the array in the view to display a list of results. I need the array to contain two fields per row and ID and Name so eg

row1 ID = 1 , Name = Hello row2 ID = 2 , Name = World

i've been looking al over the net and I can do this by just passing either ID or Name but i cant figure out how to pass them both in the array.

//get open cases
        $this->db->where('UserID',$this->session->userdata('ID'));
                $query = $this->db->get('cases');
                if ($query->num_rows() > 0)
                        {
                            $cases = array();
                            foreach ($query->result() as $row)
                                    {
                                        // ive tried this
                                        $cases[]=$row->ID,$row->CaseName;
                                        // and this
                                        $cases[]=($row->ID,$row->CaseName);
                                        //but they both dont work :(
                                    }

I can understand basic arrays but have never really had to use them before and now im using CI and i cant simply put the sql lookup in the view and loop through it there. I know the sql lookup should be in model but im not fussed about that at the moment, just want to get the array filled out and passed to the view, any help would be appreciated.

//EDIT this below function works fine but is it how yours suppposed to use CI

function viewCase()
            {
                $caseID = $this->uri->segment(3);

                $this->db->where('ID',$caseID);
                $this->db->where('UserID',  $this->session->userdata('ID'));
                $query = $this->db->get('cases');
                if ($query->num_rows() == 1)
                {
                    foreach ($query->result() as $row)
                            {
                            $data['caseID']= $row->ID;
                            $data['caseName'] = $row->CaseName;
                            $data['fileName'] = $row->FileName;
                            $data['statusID'] = $row->Status;
                            $data['statusName']= "not set yet";
                            //we need the status friendly name
                            $this->db->where('ID', $data['statusID']);
                            $query2 = $this->db->get('case_status');
                            if ($query->num_rows() == 1)
                            {
                                foreach ($query2->result() as $row2)
                                        {
                                            $data['statusName']=$row2->Name;
                                         }
                            }

                            }


                            $data['fName']= $this->session->userdata('fName');
                            $data['lName']= $this->session->userdata('lName');
                            $data['email']= $this->session->userdata('email');
                            $data['fullName']= $this->session->userdata('fName')." ".$this->session->userdata('lName');
                            $this->load->view('head',$data);
                            $this->load->view('header_logged_in',$data);
                            $this->load->view('nav');
                            $this->load->view('view_case_view',$data);
                            $this->load->view('footer');
                            }

                else
                    {
                    $data['fName']= $this->session->userdata('fName');
                    $data['lName']= $this->session->userdata('lName');
                    $data['email']= $this->session->userdata('email');
                    $data['fullName']= $this->session->userdata('fName')." ".$this->session->userdata('lName');
                    $this->load->view('head',$data);
                    $this->load->view('header_logged_in',$data);
                    $this->load->view('nav');
                    $this->load->view('view_case_error_view');
                    $this->load->view('footer');
                    }
            }
  • 写回答

3条回答 默认 最新

  • dps57553 2011-02-11 15:21
    关注

    If you don't need to do anything with the data in the controller you can just as well pass the $query variable to your view and then foreach ($query->result() as $row) in the view.

    //EDIT: I would write your controller like this. Here I assume you must enter this controller with a ID, if you do not it will raise an error. Your URI segment code would returned false, which I think would be interpreted as zero in your query and return a zero resultset. You could set 0 as a default value like this viewCase($caseID=0) if you like.

        function viewCase($caseID)
    {
        $this->db->select('cases.ID AS CaseID', FALSE); /* you will access this as $row->CaseID */
        $this->db->select('cases.CaseName');
        $this->db->select('cases.FileNameName');
        $this->db->select('cases.Status AS StatusID', FALSE); /* if you don't need status id, you can remove this, access as $row->StatusID */
        $this->db->select('case_status.Name AS StatusName', FALSE); /* access as $row->StatusName */
        $this->db->join('case_status', 'case_statis.ID=cases.Status');
        $this->db->where('cases.ID', $caseID);
        $this->db->where('cases.UserID', $this->session->userdata('ID'));
        $query = $this->db->get('cases');
    
        $data['query'] = $query;
    
        $data['fName']= $this->session->userdata('fName');
        $data['lName']= $this->session->userdata('lName');
        $data['email']= $this->session->userdata('email');
        $data['fullName']= $this->session->userdata('fName')." ".$this->session->userdata('lName');
    
        $this->load->view('head',$data);
        $this->load->view('header_logged_in',$data);
        $this->load->view('nav');
        $this->load->view('view_case_view',$data);
        $this->load->view('footer');
    }
    

    And then in your view do something like this

    // view
    <table>
        <tr>
            <th>CaseID</th>
            <th>CaseName</th>
            <th>FileName</th>
            <th>StatusID</th>
            <th>StatusName</th>
        </tr>
        <? foreach ($query->result() as $row): ?>
            <tr>
            <td><p><?=$row->CaseID?></p></td>
            <td><p><?=$row->CaseName?></p></td>
            <td><p><?=$row->FileName?></p></td>
            <td><p><?=$row->StatusID?></p></td>
            <td><p><?=$row->StatusName?></p></td>
            </tr>
        <? endforeach; ?>
    </table>
    

    Note that the foreach will not enter if there are no rows so you don't really need to check for number of rows unless you want to do something special when there is nothing returned (like printing "no result").

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题