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

Php 如何将数组值从一个函数传递到另一个函数,并使用Codeigniter将其发送到查看页面?,php,codeigniter,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['dat

我从模型中获取数组值,该值必须传递给另一个函数

控制器

第一个功能

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);
}
第二功能

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;
}
但我正在查看页面
未定义变量:数据和未定义变量:结果

我想在没有会话的情况下使用。您能帮我一下吗?

我建议您不要从第一个函数加载视图。您只需将
$result
数组传递给另一个函数
new\u example\u added
,然后从那里加载视图。这是以正确的方式完成的。

更改第一个功能代码

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);
}
这很简单 在第一个函数中添加第二个函数,如下

控制者

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;
foreach($res的结果):
echo$res.“
”; endforeach; foreach($res视图): echo$res.“
”; endforeach;
实际上,我在第一个函数中传递结果数据以查看页面。单击“提交”按钮后,视图中有一个表单,它将检查第二个函数上的验证,但我在单击按钮后获得未定义变量:视图中的数据您提到的错误
未定义变量:数据和未定义变量:结果
在视图文件或控制器中?未定义变量:数据,单击don按钮提交后,我进入查看页面。我在第二个函数中得到缺少的参数1。如何调用第二个函数new_example_added()?显示更多的代码。实际上,我正在将结果传递给第一个函数中的视图。单击“提交”按钮后,视图中有一个表单(用户示例表单),它将检查第二个函数(添加了新的示例)上的验证,但我得到了未定义的变量:单击按钮后视图中的数据您不需要第二个函数来检查表单的验证。只需检查我的编辑answer@NarendraVerma可能是添加
=''
类似
新添加的示例($result='')
然后var转储结果