Welcome Guest, Not a member yet? Register   Sign In
codeigniter, $this-> property not available in 1 method after being set in another
#1

[eluser]bill19[/eluser]
Hi everyone.

I have the following method in my 'Main' controller:

Code:
function display_upload_form($row)
{
        $this->row=$row;
        echo $this->row;
        
        
  $this->load->view('upload_form', array('error' => ' ' ));
}

echo $this->row produces the correct db row number.

Following this, the form submits to Main/do_upload

Code:
<?php echo form_open_multipart('Main/do_upload');?>

At the beginning of the 'do_upload' method, I have

Code:
function do_upload()
{
          
                   var_dump($this->row);

The output is :
Quote:string '' (length=0)

Why is $this->row not available globally?

Thank you,

Bill
#2

[eluser]InsiteFX[/eluser]
[code]
class some_class extends CI_Controller {

public $row;

}
[code]
#3

[eluser]bill19[/eluser]
My code now has:

Code:
class Main extends Common_Auth_Controller {


    public $row;
    
    function __construct()
    {
        parent::__construct();
        
        //do stuff here -
//        remember that $this->the_user is available in this controller
        //as is $the_user available in any view I load

  
        $this->primary_folder='...';
        $this->secondary_folder='...';

The var_dump result is now NULL
#4

[eluser]solvo[/eluser]
Hey Bill,

It looks to me like you're getting the row and setting it inside a controller method, then building the page and sending the output to the browser. Then the user submits the form to the server, and you're expecting that the variable you set will still exist and have the value set in the previous call to the server?

If I have your question correct, then this will not work. Dynamically set variables only survive as long as the request to the server. Once your output is built and sent back to the browser, any variables you set will be gone from memory.

Instead, take that $row_id and send it to your view. You can then set a hidden input in your form named "row_id" to the value of the $row_id. Now when the form builds in the user's browsers, that value will still exist. When they submit the form, one of the fields will be "row_id", and assuming they haven't modified your form (which is a terrible assumption to make, but for now we'll run with it), then the method that processes your form will have that value you're looking for.

Might look something like this:

Code:
class Main extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        //Do what you need to here
    }

    function display_upload_form($row)
    {
        $this->load->view('upload_form', array('row_id' => $row, 'error' => '' ));
    }

    function do_upload()
    {
        $row_id = $this->input->post('row_id');
        var_dump($row_id);
    }
}

Your upload form should then contain something like this:

Code:
<input type="hidden" name="row_id" id="row_id" value="<?=$row_id?>" />

Alternatively, you can have the link you post your form to have the row_id in the URL, so that row_id value will get passed as a parameter to the do_upload() method.

To do this, your form should ultimately look something like this:

Code:
<form method="POST" action="http://yoursite.com/main/do_upload/<?=$row_id?>">

And then your do_upload method will look like this:

Code:
function do_upload($row_id)
{
    var_dump($row_id);
}


...this is all assuming I interpreted your problem correctly, of course.
#5

[eluser]bill19[/eluser]
Hi Solvo,

Thanks for the detailed explanation and code. You have solved my problem. I didn't know that:
Quote:Dynamically set variables only survive as long as the request to the server.
.

I followed the hidden field approach you described, and I've been able to pass the row number back to the second controller method.

Regards,

Bill





Theme © iAndrew 2016 - Forum software by © MyBB