How to access a variable in a PHP class that is set by another function? Codeigniter

后端 未结 2 444
后悔当初
后悔当初 2021-01-28 22:03

I have this code here in PHP using Codeigniter framework, I can\'t seem to get my head around class variables in this code seems completely different to C++.

I was wonde

相关标签:
2条回答
  • 2021-01-28 22:13
    class Submit extends CI_Controller {
    
        private $record_id=false;
    
        public function __construct(){
             if(isset($_SESSION['record_id'])){
                  $this->record_id = $_SESSION['record_id'];
             }
        }
    
        public function send_data(){
            $this->record_id = $this->submit_model->create_record($completedstaffrows, $completedeventrows);
            $_SESSION['record_id'] = $this->record_id;
            if ($this->record_id == FALSE){
                echo "Failed to add to database";
            }
            else{
                redirect('submit/success');
            }
            return;
        }
        public function success(){
    
             $page['page'] = 'success';
             $page['record'] = $this->record_id;
             $this->load->view('template', $page );
        }
    }
    
    0 讨论(0)
  • 2021-01-28 22:20

    use codeigniter's tiny feature called Flashdata which allows you to have temorarily store data between requests.

    so your code would be

    function send data{
    $this->session->set_flashdata('recordid', $recordid);
    }
    
    function success{
     $recordid =  $this->session->flashdata('recordid');
    }
    

    got it ?

    0 讨论(0)
提交回复
热议问题