![]() |
Problem with arrays - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Problem with arrays (/showthread.php?tid=32434) Pages:
1
2
|
Problem with arrays - El Forum - 07-23-2010 [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: productsThis is my controller Code: <?php Problem with arrays - El Forum - 07-23-2010 [eluser]silent_assassin[/eluser] Any ideas? Problem with arrays - El Forum - 07-23-2010 [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 Problem with arrays - El Forum - 07-23-2010 [eluser]silent_assassin[/eluser] Thanks for you help.Now i got it.But print_r($this->products) displays blank page? Problem with arrays - El Forum - 07-23-2010 [eluser]mddd[/eluser] Maybe you didn't put anything in the variable?? I can't judge that. Problem with arrays - El Forum - 07-23-2010 [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! Problem with arrays - El Forum - 07-23-2010 [eluser]mddd[/eluser] Paste your entire code again then. And how are you calling those functions? Problem with arrays - El Forum - 07-23-2010 [eluser]silent_assassin[/eluser] Controller Code: <?php Code: <?php Code: <?php Problem with arrays - El Forum - 07-23-2010 [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. Problem with arrays - El Forum - 07-23-2010 [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. |