Welcome Guest, Not a member yet? Register   Sign In
Confused about variables
#4

Hi there, thank you both very much for your replies.

I'd be very grateful if you would have a look at my responses.

I am still very new to web programming, having come from a Windows PC C# programming background a long time ago, so forgive me if I haven't quite got the point of all this yet.

(07-28-2016, 10:24 AM)mwhitney Wrote: First, you would probably be better off setting a specific property for the data you need to store, rather than a generic $data array (but you should probably use more specific names, because there are potentially a lot of things attached to $this in the controller). In general, it should be quite rare to store data as a property of your controller, especially as a property of your base controller (MY_Controller).

I guess I am just going with what I have seen from some of the tutorials online. I understand that variables/data should not be stored in controllers, as controllers retrieve data from the model and pass it to views, but I guess data in this context is being used to set up parameters that are being displayed on the view.

It seems to me that a variable defined in the constructor of a class is behaving more like a constant, in that you can set it in a controller's constructor, then use it in any of the methods in that controller, and even pass it to a view, but that it gets destroyed once the view goes out of scope so to speak.

Does that mean that every time a controller is called, a new instantiation of the class is created, used, and then destroyed once the page is rendered?

If that is so, then is the only way to return parameters set on a page by a user is either as POST data or as extensions to the URL?
i.e.
Code:
http://example.com/[controller-class]/[controller-method]/[arguments]

(07-28-2016, 10:24 AM)mwhitney Wrote: Second, did you define a constructor in your extending controller (and, if so, did you remember to call the parent's constructor in the constructor)? If you did not define a constructor, you should be fine, but if you did define one and did not call the parent's constructor, then your parent's constructor isn't being called to initialize the data. The same goes for MY_Controller, did you remember to call parent::__construct() in MY_Controller's constructor?

Yes, the constructor was defined - snippet shown below:

Code:
class MY_Controller extends CI_Controller
{
public $data = array();

function __construct()
{
parent::__construct();

//Set a value in the $data array to make sure that $data is initialised
$this->data['user']='USER variable in MY_CONTROLLER';
$this->data['counter'] = 4;

(07-28-2016, 10:24 AM)mwhitney Wrote: Views shouldn't be calling controller methods. Whatever data the view needs should be supplied to it when you call $this->load->view() (as you've done when passing $this->data to it). A reference to a method from a view using $this might be resolved, but the context might not be what you expect, which may be the source of your issues (especially since you're relying so heavily on the context by changing properties in the controller).

I am confused by this. If views can't call controller methods, then how do you control the flow of your site? I was of the understanding that was how one got back into a controller from a view?

If I can't do it using the method shown below (which as far as I can see is a call from a view into a method inside a controller) then how does one control the flow of one's site?

For example, to go to another page in the site from a button, I would use something like this:

Code:
<a href="<?php echo site_url('Start_here_controller/show_B_page')?>" class="button large">TEST_B VIEW</a>

and the code for show_B_page would be something like:

Code:
public function show_B_page()
{
echo ('$this->data[user] is ' . $this->data['user'] . '<br>');

$a = $this->data['counter'];
echo ('$a is ' . $a . '<br>');
$a = $a + 1;
echo ('$a is ' . $a . '<br>');
$this->data['counter'] = $a;
echo ('$a is ' . $a . '<br>');
echo ('$this->data[counter] is ' . $this->data['counter'] . '<br>');

$this->render('test_b');
}

(note the code above is test code for me to try and figure out how this stuff is working...)

(07-28-2016, 10:24 AM)mwhitney Wrote: Finally, when you call $this->load->view(), then exit the method called by the URL, once the loader finishes processing your view(s), that's the end of your response to the current request, and the data stored in your controller goes away. If you want data to be stored between requests, you need to use a session or store it some other way (e.g. a file, database, or cache).

I think that connects with my first response - if I want to pass parameters back from a view to a controller method, I have to pass it back as parameters on the method call - and this I guess results in a new instantiation of the controller.

So in order to store stuff, I either use sessions, or create a table in the database for variables. I can use parameters at the end of a method call to pass variables into a method as another way of controlling flow I guess...

It would that what I have been calling $data (defined as an array) can only be used as constants since that data can't be permanently changed as the class gets destroyed when the page has been rendered by the loader.

Am I on the right track?

I do apologise if these questions are really dumb.

Thanks for your help.

JB
Reply


Messages In This Thread
Confused about variables - by MightBeABitLate - 07-27-2016, 03:35 PM
RE: Confused about variables - by InsiteFX - 07-28-2016, 04:28 AM
RE: Confused about variables - by mwhitney - 07-28-2016, 10:24 AM
RE: Confused about variables - by MightBeABitLate - 07-29-2016, 05:06 AM
RE: Confused about variables - by mwhitney - 07-29-2016, 07:26 AM
RE: Confused about variables - by dave friend - 07-29-2016, 08:27 AM
RE: Confused about variables - by MightBeABitLate - 07-29-2016, 08:44 AM
RE: Confused about variables - by mwhitney - 07-29-2016, 09:53 AM
RE: Confused about variables - by dave friend - 07-29-2016, 09:05 AM
RE: Confused about variables - by PaulD - 07-29-2016, 09:11 AM
RE: Confused about variables - by MightBeABitLate - 07-29-2016, 10:42 AM
RE: Confused about variables - by dave friend - 07-29-2016, 11:28 AM
RE: Confused about variables - by MightBeABitLate - 07-29-2016, 11:54 AM
RE: Confused about variables - by mwhitney - 07-29-2016, 12:17 PM
RE: Confused about variables - by fmertins - 07-29-2016, 01:14 PM
RE: Confused about variables - by mwhitney - 07-29-2016, 02:01 PM



Theme © iAndrew 2016 - Forum software by © MyBB