Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将变量从一个函数传递到另一个函数_Php_Codeigniter - Fatal编程技术网

Php 将变量从一个函数传递到另一个函数

Php 将变量从一个函数传递到另一个函数,php,codeigniter,Php,Codeigniter,如何将$variable从comments()传递到someFunction() **编辑**如果您愿意,请随时指出任何其他错误 class Home扩展CI_控制器{ 私用$idArray; 函数_u构造() { 父项::_构造(); $this->load->model('home_model'); $this->load->library('tank_auth'); $this->load->library('form_validation'); } 公共职能指数(){ $home_d

如何将
$variable
从comments()传递到someFunction()

**编辑**如果您愿意,请随时指出任何其他错误

class Home扩展CI_控制器{
私用$idArray;
函数_u构造()
{
父项::_构造();
$this->load->model('home_model');
$this->load->library('tank_auth');
$this->load->library('form_validation');
} 
公共职能指数(){
$home_data['initial_two']=$this->home_model->get_two_brands();
$home_data['user_id']=$this->tank_auth->get_user_id();
$home_data['username']=$this->tank_auth->get_username();
$this->load->view('home\u view',$home\u data);
}
公共函数get_two(){
$get_results=$this->home_model->get_two_brands();
if($get_results!=false){
$html='';
foreach($get_results as$result){
$html.='
  • '; } list($result1,$result2)=$get\u results; $idOne=$result1->id; $idTwo=$result2->id; $this->idArray=array($result1->id,$result2->id); //变量转储($this->idArray); $result=array('status'=>'ok','content'=>$html); 标题('Content-type:application/json'); echo json_编码($result); 退出(); } }//公共函数get_two(){ 函数用户_pick(){ $this->form_validation->set_规则('pick','pick','required | trim | integer | xss_clean'); $this->form_validation->set_规则('notPick','notPick','required | trim | integer | xss_clean'); //$arr=$this->idArray; var_dump($this->idArray);//这是空的 $pick=$_POST['pick']; $notPick=$_POST['notPick']; $user_id=$this->tank_auth->get_user_id(); 如果($this->form\u validation->run()==FALSE) { $result=array('status'=>'no','content'=>“no good!”); 标题('Content-type:application/json'); echo json_编码($result); 退出(); }else{//if($this->form_validation->run()==FALSE | |$do_input==NULL) $upload=$this->home\u model->user\u pick($user\u id,$pick,$notPick); $result=array('status'=>'ok','content'=>“谢谢!”); 标题('Content-type:application/json'); echo json_编码($result); 退出(); }//if($this->form_validation->run()==FALSE | |$do_input==NULL) } }//类Home扩展CI_控制器{右括号 /*home.php文件结尾*/ /*位置:./application/controllers/home.php*/
    class Blog extends CI_Controller {
    
    public function index()
    {
        echo 'Hello World!';
    }
    
    public function comments()
    {
        $variable = "Hello";
    }
    
    public function someFunction()
    {
        echo $variable;
    }
    
    }
    
    class Home extends CI_Controller {
    
    private $idArray;
    
    function __construct()
    {
        parent::__construct();
        $this->load->model('home_model');
        $this->load->library('tank_auth');
        $this->load->library('form_validation');
    
    } 
    
    public function index() {
    
        $home_data['initial_two'] = $this->home_model->get_two_brands();
    
        $home_data['user_id']   = $this->tank_auth->get_user_id();
        $home_data['username']  = $this->tank_auth->get_username();
    
        $this->load->view('home_view', $home_data);
    }
    
    public function get_two() {
    
                $get_results = $this->home_model->get_two_brands();
    
                if($get_results != false){
    
                  $html = '';
    
                   foreach($get_results as $result){
                         $html .= '<li><a href="#" class="pick" id="'.$result->id.'">'.$result->brand.'</a></li>';
                    }
    
                    list($result1, $result2) = $get_results;
    
                    $idOne = $result1->id;
                    $idTwo = $result2->id;
    
                    $this->idArray = array($result1->id, $result2->id);
    
    //var_dump($this->idArray);
    
                     $result = array('status' => 'ok', 'content' => $html);
                        header('Content-type: application/json');
                        echo json_encode($result);
                        exit();
    
                }
    
    }//public function get_two() {
    
    function user_pick() {
    
        $this->form_validation->set_rules('pick', 'Pick', 'required|trim|integer|xss_clean');
        $this->form_validation->set_rules('notPick', 'Not Pick', 'required|trim|integer|xss_clean');
    
        //$arr = $this->idArray;
     var_dump($this->idArray); // This is NULL
        $pick = $_POST['pick'];
        $notPick = $_POST['notPick'];
        $user_id = $this->tank_auth->get_user_id();
    
        if ($this->form_validation->run() == FALSE)
                {
                    $result = array('status' => 'no', 'content' => "No good!");
                        header('Content-type: application/json');
                        echo json_encode($result);
                     exit();
    
        }else{//if ($this->form_validation->run() == FALSE || $do_input == NULL)
    
                    $upload = $this->home_model->user_pick($user_id, $pick, $notPick);
    
                    $result = array('status' => 'ok', 'content' => "Thank you!");
                        header('Content-type: application/json');
                        echo json_encode($result);
                     exit();
    
        }//if ($this->form_validation->run() == FALSE || $do_input == NULL)
    
    }
    
    }//class Home extends CI_Controller { closing bracket
    
    /* End of file home.php */
    /* Location: ./application/controllers/home.php */
    
    class Blog extends CI_Controller {
    
        public function index()
        {
            echo 'Hello World!';
        }
    
        public function comments()
        {
            $_SESSION['variable'] = Array('k1'=>'v1','k2'=>'v2') ;
            // Store the variable in session so it can be called
            // in another page or ajax call
        }
    
        public function display()
        {
            echo $_SESSION['variable'] ;
        }
    }
    
    
    // my index.php file
    
    var $blog = new Blog() ;
    $blog->comments() ;
    
    //another ajax_called.php file, call in ajax on in another browser tab
        var $blog = new Blog() ;
        $blog->display() ;