0

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 wondering how I can get a local variable in one class (function) method to another class method.

But not passing them as variables as I have to use a redirect function instead which cannot take variables.

The variable I want to have accessible is $record_id , i tried making it public etc does not like it.

class Submit extends CI_Controller {

function send_data()
{
        $record_id = $this->submit_model->create_record($completedstaffrows, $completedeventrows);

                        if ($record_id == FALSE)
                        {
                            echo "Failed to add to database";
                        }

                        //Submittal to database was successful
                        else
                        {

                            redirect('submit/success');
                        }
                        return;
                    }

This is the function that I want to have access the $record_id

public function success()
{

$page['page'] = 'success';
$page['record'] = $record_id;
$this->load->view('template', $page );


}

Remember - I cannot pass this as a variable to the other function as I need to use a redirect so that my URL will not screw up. Thanks

Cheers!

sqlmole
  • 997
  • 7
  • 17
  • 31

2 Answers2

1

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 ?

Hardik
  • 536
  • 4
  • 11
1
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 );
    }
}
Darm
  • 5,581
  • 2
  • 20
  • 18
  • it's not gonna get `$this->record_id == false` at success() function()?, because I suppose when a redirect is made, we reload the controller again, but it will go to success() method with `$this->record_id` placed with a false value due to the new instance for the class. – dennisbot Mar 01 '13 at 05:15