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

时间:2014-03-24 14:29:06

标签: php codeigniter

我是否可以将$variable从comments()传递给someFunction()?

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 */

1 个答案:

答案 0 :(得分:0)

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() ;