如何将数组值从一个函数传递到另一个函数并发送它以使用Codeigniter查看页面?

时间:2017-10-09 12:39:29

标签: php codeigniter

我从模型获取数组值,该值必须传递其他函数

控制器

第一个功能

public function example_from_view(){
 $int_id = $this->session->userdata['int_url_id']['int_id'];
$result['data']=$this->formbuilder_Model->check_example_form_builder_fields($int_id);
print_r($result['data']); //I have to pass this array to second function so I tried $this->new_example_added($result);
 $this->load->view('user-example-form',$result);//passing elements to the view page
    }

第二项功能

public function new_example_added($result)
{
print_r($result['data']);
$this->load->view('user-example-form', $result);
}

查看

foreach ($data as $key) {
 echo $exp_fields_name=$key->fields_name;
}

但我正在查看页面undefined variable: data and Undefined variable: result。 我想在没有会话的情况下使用。你能帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:2)

更改第一个功能代码

public function example_from_view(){

      if(isset($this->input->post('SUBMIT_BUTTON_NAME'))){
         //do your validation here
      }
      $int_id = $this->session->userdata['int_url_id']['int_id'];
      $data = $this->formbuilder_Model->check_example_form_builder_fields($int_id);
      //print_r($result['data']); //I have to pass this array to second function so I tried $this->new_example_added($result);
      $result['data'] = $data;
      $this->load->view('user-example-form',$result);//passing elements to the view page
}

删除此功能

public function new_example_added($result)
{
  //print_r($result['data']);
  $data['data'] = $result;
  $this->load->view('user-example-form', $data);
}

答案 1 :(得分:0)

我建议你不要从第一个功能加载视图。您应该将$result数组传递给另一个函数new_example_added,然后从那里加载视图。这是以正确的方式完成的。

答案 2 :(得分:0)

非常简单 像这样在第一个函数中添加第二个函数..

在控制器中

class Test extends CI_Controller{
  public function one(){
     $data['result'] = array(
        'one' => '1',
        'two' => '2',
        'three' => '3'
    );
    #calling Second function
    $data['view'] = $this->second($data);
    $this->load->view('testview', $data);
  }
  public function second($data)
  {
    $data = array(
        'four' => '4',
        'five' => '5',
        'six' => '6'
    );
    return $data;
  }
}

然后在视图页面

foreach($result as $res):
   echo $res."<br/>";
endforeach;
foreach($view as $res):
    echo $res."<br/>";
endforeach;