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

(This post was last modified: 07-29-2016, 10:43 AM by MightBeABitLate. Edit Reason: spelling mistake )

MWhitney, PaulD, Dave,

Once again, I am extremely grateful for the high quality of the responses that I am receiving, and they encourage me to explore CI further. Your helpfulness is outstanding. All your posts are very very useful.

I think my confusion about variables vs constants was not adequately described.

I am happy with the difference between variables and constants in the context of PHP (and in most programming languages), and I understand MWhitney's point about defining properties for access from other classes, but it seems to me that within the context of CI....

Hmmm... just thinking out loud...

I think I've got this.....I HOPE I've got this...

You can declare variables within the constructor of the controller. You can then access and modify those variables within any of the controller methods RIGHT UP TO THE POINT AT WHICH YOU RENDER A VIEW.

So, for example, you might create a controller with a variable that holds a count of the entries in a database that met some criteria, and you could set that variable accordingly. You might then pass that result to other methods in your controller, and change that value for whatever reason, but once you render out that view with the data passed to the view, then the controller "disappears" for want of a better expression, and next time you instantiate that controller, all your variables are reset back to their default values as per the constructor.

That explains why my pseudo-code shown above always shows the $this->data['counter'] value as being 4 - because when the view calls the controller method, the controller class is a new version every time! And in order to make it increment, I would have to store the value in a database and read it in every time and then increment it - or store it in a session.

I think I've cracked it!

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