如何将赋值变量的值传递给视图?

How to pass values from a assigned variable to view?

我想将一些值传递给视图。

$result 可能是 0 or 1 or '',我尝试使用下面的代码:

public function whatwedo($result='')
{
$result = array();
$result['status'] = $result;
$this->load->view('admin/whatwedo',$result);
}
public function add_whatwedo()
    {  
       //$this->load->library('form_validation');
       $this->form_validation->set_rules('text-input','Title','required');
   
   if($this->form_validation->run() != true)
    {
      $result = 0;
      $this->whatwedo($result);
    }
    else
    {
     $this->load->model('admin_model');
     $result = $this->admin_model->ins_whatwedo($this->input->post());
     //print_r($result);exit();
     $this->whatwedo($result);
    }
}

并且在视图中:

<?php 
 print_r($status);
?>

但是,$status 是 Array ( )

问题出在这一行:

$result = array();

因为现在 $result 变量是一个 空数组 所以当你在结果上创建索引 status 时你给它分配一个 空数组.

要修复,您可以执行以下操作:

public function whatwedo($input = '')
{
    $result['status'] = $input;
    $this->load->view('admin/whatwedo', $result);
}

甚至...

public function whatwedo($input = '')
{
    $this->load->view('admin/whatwedo', ["status" => $input]);
}