Welcome Guest, Not a member yet? Register   Sign In
Problem with arrays
#1

[eluser]silent_assassin[/eluser]
I have stored $this->input->post values in an array which is contained in one function.When i try to print the array in another function it displays following error.Both the functions present in same controller.
Quote:Message: Undefined variable: products
This is my controller
Code:
<?php
class Product extends Controller
{

    function Product()
    {
        parent::Controller();
    }

    function index()
    {
        
        $this->load->view('collect_info');
    }
    public function preview()
    {
        if($this->input->post('preview'))
        {
            $this->load->view('preview_product');    
                
        $products=array(
                $this->input->post('p_name'),
                $this->input->post('p_model'),
                $this->input->post('p_short_desc')
        );
        }
        elseif($this->input->post('cancel'))
        {
            
        }
        
    }
    public function create()
    {
        print_r($products);
        
    }
}

?>
How this should be coded
#2

[eluser]silent_assassin[/eluser]
Any ideas?
#3

[eluser]mddd[/eluser]
You should read up on variable scopes. A variable that you declare inside a function only exists in that function.
If you want to access it from outside the function you need to store the variable somehere else. A logical place is to make it a class property (that is, a variable that is common to all functions in the class).
Code:
<?php
class Product extends Controller
{

    var $products;

    function Product()
    {
        parent::Controller();
    }

.....

    function some_function()
    {
        // to get to the products variable, use $this->products
        $this->products = 'somevalue';
    }

    function other_function()
    {
        // you can still use the variable here, even though we're in another function
        print_r($this->products);
    }
}
#4

[eluser]silent_assassin[/eluser]
Thanks for you help.Now i got it.But print_r($this->products) displays blank page?
#5

[eluser]mddd[/eluser]
Maybe you didn't put anything in the variable?? I can't judge that.
#6

[eluser]silent_assassin[/eluser]
No.When i print array in some_function() it displays correct results.But if i print same array in other_function() it displays a blank page!
#7

[eluser]mddd[/eluser]
Paste your entire code again then. And how are you calling those functions?
#8

[eluser]silent_assassin[/eluser]
Controller
Code:
<?php
class Product extends Controller
{
    var $products;
    function Product()
    {
        parent::Controller();
    }
    function index()
    {
        $this->load->view('collect_info');
    }
    function preview()
    {
        if($this->input->post('preview'))
        {
            $this->products=array(
                $this->input->post('p_name'),
                $this->input->post('p_model'),
                $this->input->post('p_short_desc')
        );
            $this->load->view('preview_product');    
            print_r($this->products);
        }
        elseif($this->input->post('cancel'))
        {
            
        }
    }
    function create()
    {
        print_r($this->products);
    }
}

?>
View 1:
Code:
<?php
    $this->load->helper('form');
    $this->definitions->includes();
    echo form_open('product/preview');
    echo PRODUCT_NAME.form_input('p_name').'<br>';
    echo PRODUCT_MODEL.form_input('p_model').'<br>';
    echo PRODUCT_SHORT_DESCRIPTION.'<br>';
    echo form_textarea('p_short_desc').'<br>';
    echo PRODUCT_FULL_DESCRIPTION.'<br>';
    echo form_textarea('p_full_desc').'<br>';
    echo PRODUCT_DATE_ADDED.'<br>';
    echo PRODUCT_DATE_AVAILABLE.'<br>';
    echo PRODUCT_QUANTITY.form_input('p_qty').'<br>';
    echo PRODUCT_PRICE.form_input('p_price').'<br>';
    echo PRODUCT_META_KEYWORDS.'<br>';
    echo form_textarea('p_meta_keywords').'<br>';
    echo PRODUCT_META_DESCRIPTION.'<br>';
    echo form_textarea('p_meta_desc').'<br>';
    echo form_submit('preview','Preview');
    echo form_submit('cancel','Cancel');
    echo form_close();
?&gt;
View 2
Code:
&lt;?php
        $this->definitions->includes();
        $this->load->helper('form');
        echo PRODUCT_NAME .$this->input->post('p_name').'<br>';
        echo PRODUCT_SHORT_DESCRIPTION .$this->input->post('p_short_desc').'<br>';
        echo PRODUCT_FULL_DESCRIPTION .$this->input->post('p_full_desc').'<br>';
        echo PRODUCT_PRICE .$this->input->post('p_price').'<br>';
        echo form_open('product/create');
        echo form_submit('create_product','Create Product');
        echo form_close();
?&gt;
For simplicity I am printing array in create_function().But when I call this function the array must save in a database.
#9

[eluser]mddd[/eluser]
So first you call index(). It shows a form. Then you call preview() with your form information. It shows some information and puts something in $this->products. Then you call create() with some form information. But when you call create(), $this->products is empty! Variables are not stored between requests! If you want something in $this->products in create(), you have to put it there! Now there is nothing, so it shows nothing.
#10

[eluser]silent_assassin[/eluser]
Yes.variables are not stored between requests.In this program,if i want to save the form data in an array and use it for inserting data into database in which part i have to store form data in array.




Theme © iAndrew 2016 - Forum software by © MyBB