Welcome Guest, Not a member yet? Register   Sign In
what am i missing?
#1

[eluser]cityzen[/eluser]
I've read the user guide, read the PACKT book, looked at a lot of examples and I'm still just scratching my head. Is there anything worth reading about variables between controllers and views. I'm sure I'm missing something but I don't even know where to look at this point. Is it really that hard to manage variables in these MVC frameworks? It seems like simple tasks are so convoluted that I'm spending hours just trying to understand exactly what is going on. It just seems like the only way I can stay away from errors is putting code like $data['name'] = ''; all over the place if I'm using the variable $name in my view. I have to pass it in when I add, save, edit, etc.

I don't consider myself dumb when it comes to this, but this seems so illogical that I'm having a hard time making sense of any of it.

Thanks in advance for any advice/tips.
#2

[eluser]Michael Wales[/eluser]
There is a seperation between the controller and the view - they essentially have no knowledge of one another at all. You must manually tell your controller to pass specific data to the view, which the view would use as a variable.

This is by design and as you get used to it I am sure you will learn to love it. Maybe you could paste a specific example you are having a hard time with and we could help you with it?
#3

[eluser]cityzen[/eluser]
Thanks a lot Michael. That's the problem, I do like what I'm seeing, but these hangups are driving me nuts!

Here's a good example I'm working through now:

I have a form that has both a file upload and other form elements. When I add in the layer for the form upload errors I'm doing this in my controller:

Code:
if (!$this->upload->do_upload())
{
   $data['uploadErrors'] = $this->upload->display_errors('<strong>','</strong>');
   $data['view'] = 'files/addmod';
   $this->load->view('global/template', $data);
}    
else
{
... save info here ...

On my view, I currently have:

Code:
if($this->validation->error_string){
    echo 'Please correct the following errors:';
    echo $this->validation->error_string;
}

to handle the regular form errors. I would like to add this under it:

Code:
if($uploadErrors){
   echo $uploadErrors;
}

Now what happens is that I have to start declaring $uploadErrors all over the place with anything that touches this view (that handles adding and modifying data). Another somewhat related issue is that I am toggling between if a file is uploaded to show a link to the file and if not it will just show the upload box but this is starting to cause problems as well since the name of the file is $name and the upload is $userfile so I start to get a conflict between $name or $userfile being absent (since they both won't exist at the same time, but there is code in both the view and controller referring to them).

Sorry that is so long winded... I'm giving myself the "i can't go to bed until I figure this out" so I don't punch my wife in the head while I'm sleeping and having nightmares about this!

Also, is there any logical way to group together the validation and upload errors together into one nice package?
#4

[eluser]cityzen[/eluser]
While we're at it... is it normal to re-use the form that you use for adding data to update as well? I've always done it that way, but the way this works, if you're not using the *exact* same variables in both forms you'll run into problems. I could do this much easier for having a view for adding and updating and had separate functions for add, edit, save new, save edited, etc. but that seems pretty long winded to me.
#5

[eluser]Michael Wales[/eluser]
Without spending to much of my time on it, I would probably make the upload field have a rule calling a callback function. Within the callback function I would handle all of the Upload class errors if they exist, bringing them back into the Validation class via the set_message() method.

Or - you can also call these error methods from your view. Sure, it's a small break in the MVC pattern but would you rather stick to the pattern or punch your wife in your sleep?
#6

[eluser]frenzal[/eluser]
just do:
if(isset($uploadErrors)){
echo $uploadErrors;
}
#7

[eluser]cityzen[/eluser]
THank you both Michael and Frenzal.

Frenzal, you're a life saver! With the amount of crap PHP just lets you get away with, I fell into using conventions that normally wouldn't squeeze through the strict error reporting, ie: isset vs. if($var). That's a big piece of the "what am I missing" so many, many thanks to you.

Michael, thanks for your reply (again) as well. I don't think I ended up punching my wife in the neck last night, but I sure did have CI dreams! Thanks a lot for the tip on rolling the upload error into the validation error handler, etc. I think these are some of the things I needed to get my past this frustration point. I *knew* there were things I could do but just didn't know exactly how or where to even start.

Have a great day!

Mike




Theme © iAndrew 2016 - Forum software by © MyBB