Codeigniter:如何将多个数据(如数组和单个数据)从控制器传递到视图?

时间:2014-04-28 13:24:49

标签: php codeigniter

我在这里:

  public function index()
{
    $this->is_logged_in();
    $this->load->model('ReportModel');
    $this->load->model('ConsultantModel');

    if (isset($_POST['report'])){
        $reports=$this->ReportModel->search($_POST['report']); 
    } else {
        $reports=$this->ReportModel->get_last_ten_entries();
    }

    $models=$this->ReportModel->getModel();
    $terms=$this->ReportModel->getTerm();

    $username = $this->session->userdata('username');
    $data['query'] = $this->ConsultantModel->getConsultantData($username);

    //i want to send data to the report list view
    //$this->load->view('report/reportlist', $data);
    $this->load->view('report/reportlist', array('reports'=>$reports,'terms'=>$terms,'models'=>$models));
}

我想在一个视图(报告/报告列表)中传递数组和数据。我知道这是可能的,因为它是一个非常简单的想法,但我不知道如何。即使它不能,我知道你们知道一些技巧。如果你知道的话,请回答这个问题。

1 个答案:

答案 0 :(得分:0)

public function index()
{
    $this->is_logged_in();
    $this->load->model('ReportModel');
    $this->load->model('ConsultantModel');

    $data = array();
    if (isset($_POST['report'])){
      $data['reports'] = $this->ReportModel->search($_POST['report']); 
    } else {
      $data['reports'] = $this->ReportModel->get_last_ten_entries();
    }

    $data['models'] = $this->ReportModel->getModel();
    $data['terms'] = $this->ReportModel->getTerm();

    $username = $this->session->userdata('username');
    $data['query'] = $this->ConsultantModel->getConsultantData($username);

    $this->load->view('report/reportlist', $data);
}

您可以在观看中使用以下内容 -

1)报告 -

i)如果是单值 -

echo $reports;

ii)如果是阵列

foreach($reports as $report)
{
    echo $report[..];
}


同样的休息。