$data variable not recognised until after refresh when passing array to view |
[eluser]rossmurphy[/eluser]
I am passing the $data variable to my view page like so.. Code: $data['Counter'] = 0; When my view loads, i get a PHP error... Quote:A PHP Error was encountered But when i refresh the page the variable is recognised... and ideas?
[eluser]gtech[/eluser]
can we have the view code.... in the view you should print out counter like so: Code: <?=$Counter?>
[eluser]jedd[/eluser]
Hi Ross, and welcome to the CI forums. Have a re-read of the CI User Guide, specifically the bit about views. When you pass $data to a view, you don't actually get a $data variable at the view end. Instead, you get the exploded contents, as it were, of the $data array. This is why you were right to have this: Code: $data['Counter'] = 0; But when it gets to the view, you will now have a variable called $Counter Absolutely no idea why a refresh would give you some value, especially without seeing code, but I think we should politely ignore this anomaly and just get it working consistently first.
[eluser]Dam1an[/eluser]
I would also recommend you have your variable names all lower case, as per the style guide
[eluser]rossmurphy[/eluser]
I understand the way array's are exploded. My fault, i should have been thinking and paste ALL of my code.. so here it is: This is my model. Code: <?php This is my controller. Code: <?php
[eluser]jedd[/eluser]
Okay, it looks like in your model you are assuming that $data is the same as $this->data. I think this is probably your biggest conceptual problem, judging by your code. The most obvious impact of this is that when you talk about $data in your model, you're not talking about what you think you are talking about. It also looks like you're treating $data as a kind of globally-scoped variable, which is a style and/or logic problem. For data you need to refer to in the model, you should pass it to the model's method explicitly as parameters. Similarly, any returned data should be returned (rather than set in a global variable or facsimile of same). Can I also suggest that within your controller - if you are going to use one or more of your models throughout pretty much every method, put the ->load->model() calls in the controller's constructor. Otherwise, put their load call in a labelled section at the start of each method - it'll make it easier for you to read. You may also want to consolidate some of your models - bingorooms and bingolaunch may both be easier for you if they are called rooms() and launch() methods and reside in your Bingo() model (for example).
[eluser]rossmurphy[/eluser]
That was great advice, will really help clean up my code a lot. Thanks jedd!
[eluser]rossmurphy[/eluser]
if i have a function that queries an api then receives xml results, and it is a common function used by nearly every controller in my app, should this function be in a model? Thanks.
[eluser]gtech[/eluser]
I would suggest a library or a helper, or inherit(Extend) from a base controller, a model in therory is there as an interface to the database but as CI is a loose framework putting it a valid solution if it makes sense to you.
[eluser]rossmurphy[/eluser]
is it frowned upon to load a model in a helper? |
Welcome Guest, Not a member yet? Register Sign In |