trouble getting drop menu from model |
[eluser]trevor2[/eluser]
I'm having issues getting my drop menu from the model. The foreach section works if I have it in the view. Model Code: class Submit_model extends Model { Controller Code: function submit() Code: <select> My error A PHP Error was encountered Severity: Notice Message: Undefined property: Portal::$model Filename: controllers/portal.php Line Number: 22 Fatal error: Call to a member function dropmenu() on a non-object in C:\xampp\htdocs\codeigniter\system\application\controllers\
[eluser]trevor2[/eluser]
This doesn't work either. Code: function statemenu()
[eluser]pistolPete[/eluser]
First of all, you NEVER EVER echo in a model! Read through http://en.wikipedia.org/wiki/Model–view–controller http://ellislab.com/codeigniter/user-gui...w/mvc.html Since your model is called Submit_model, you access its object using: Code: ...
[eluser]trevor2[/eluser]
Guess I don't understand how to properly echo $data in the view. Since there should be no looping in the view, the foreach loop generating the <option> tags would have to be in the controller, but it's not working.
[eluser]pistolPete[/eluser]
[quote author="trevor2" date="1234745912"]Guess I don't understand how to properly echo $data in the view. Since there should be no looping in the view,...[/quote] Not necessarily, if you don't try to follow the MVC approach too strict, you can of course have loops in your views. Have a look at the user guide: http://ellislab.com/codeigniter/user-gui...views.html [quote author="trevor2" date="1234745912"]... the foreach loop generating the <option> tags would have to be in the controller, but it's not working.[/quote] "It's not working" isn't a good explanation of your problem... be more precise!
[eluser]trevor2[/eluser]
It's soo confusing learning this class and object stuff right out the gate. This works. My problem was that I was echoing $data in the view, when it should have been the array deal, state_menu, in this part: $data['state_menu'] = $this->submit_model->getstates(); I would prefer to have foreach in the controller, so I can just "echo $whatever", but my head is hurting a bit at the moment. Model Code: class Submit_model extends Model { Controller Code: class Portal extends Controller{ View Code: <select>
[eluser]pistolPete[/eluser]
[quote author="trevor2" date="1234755676"] I would prefer to have foreach in the controller, so I can just "echo $whatever", but my head is hurting a bit at the moment. [/quote] The way you used your model, controller and view is similar to my structure. You could of course generate a string containing all the contents in your model / controller and pass it to the view, but what do you gain from it?
[eluser]obiron2[/eluser]
There is nothing wrong with looping in the view to generate list data (e.g. dropdown options) however if you do this a lot, you may want to farm it off to a function that generates the html and returns it to the view as an html string, but this should be called from withing the view as it is a presentation decision, not a business logic one. I use this process for rendering forms. I build all the form data in the controller and then call a generate_form($formdata) helper which produces the form with all the correct labels, ids and formatting DIVs that I require. The big advantage is that if you want to change the rendering of repeated information, you simply have to change the function and not every view that uses it. Obiron |
Welcome Guest, Not a member yet? Register Sign In |