php - Pass variable from one function to another -


how pass $variable comments() somefunction()?

class blog extends ci_controller {  public function index() {     echo 'hello world!'; }  public function comments() {     $variable = "hello"; }  public function somefunction() {     echo $variable; }  } 

** edit ** feel free point out other mistakes if wish

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 $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); // 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 variable in session can called         // in page or ajax call     }      public function display()     {         echo $_session['variable'] ;     } }   // index.php file  var $blog = new blog() ; $blog->comments() ;  //another ajax_called.php file, call in ajax on in browser tab     var $blog = new blog() ;     $blog->display() ; 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -