Passing dynamic data to a view - help needed |
[eluser]k2zs[/eluser]
Hello, I am new to MVC and CodeIgniter. I have been working with it since this weekend and have made a lot of progresss but I am stuck. I have a model that collects page information from a single database table to deliver the web pages dynamically. I want to set some dynamic variables for the page title, page id, parent id, and so on. Page ID and parent ID will be used to change the css class in navigation to show the active page. Here is my model: Code: <?php Here is my controller: Code: <?php I have been trying to figure out how to create the dynamic variables in the controller and pass them to the template but can't seem to get it. The nav, sub-nav, and page body work as I am doing a "foreach" loop in the view but to but I don't want to do a "foreach" inside the title tag in the head. as a temporary solution I have a PHP block at the top before any output that loops the "pages" data to create the variables I need but to me that defeats the whole purpose of MVC... Can someone explain to me how to build dynamic variables in a controller and pass them to a view?
[eluser]Mischievous[/eluser]
looks like everything is good... just do <?=$mainNav?> in your view?
[eluser]k2zs[/eluser]
[quote author="Mischievous" date="1288316770"]looks like everything is good... just do <?=$mainNav?> in your view?[/quote] ?????? Whats that supposed to do? It has nothing to do with what I need and it does nothing when I do that... Could you have ment Code: <?= echo $mainNav?>
[eluser]k2zs[/eluser]
[quote author="Mischievous" date="1288316770"]looks like everything is good... just do <?=$mainNav?> in your view?[/quote]... also whats the significance of Code: <?= => Code: <?php ?>
[eluser]k2zs[/eluser]
Perhaps I should start over with this question... How do I extract specific elements of a multi-dimensial array in my controller, returned from a model, and pass those elements to my view as a variable?
[eluser]tonanbarbarian[/eluser]
why not just pass the array to the view and sort it out in the view as for the <?= ?>, that is basic PHP. http://au.php.net/manual/en/language.bas...hpmode.php
[eluser]k2zs[/eluser]
[quote author="tonanbarbarian" date="1288331729"]why not just pass the array to the view and sort it out in the view as for the <?= ?>, that is basic PHP. http://au.php.net/manual/en/language.bas...hpmode.php[/quote] Ok... Being new to codeigniter and the MVC concept I thought you were supposed to keep raw processing in the controller. This is what I have done as a work around in my view: Code: <?php if that is normal I will continue that way... Thanks
[eluser]tonanbarbarian[/eluser]
it depends on what you mean by raw processing a view is supposed to be the presentation logic, so az view is supposed to take the data provided by the controller and present it in some format, mostly HTML generally that can require looping through recordsets to display all of the data etc. the things that fall into the grey area are whether any formatting on the data should be done in the view or in the controller or the model. personally i do most of my formatting in the view because of the way i implement MVC there is not hard and fast rule in CI how you implement the mvc in fact in CI the only thing you MUST have is a controller, if you choose not to use models or views you can, but almost no one does this The way I see MVC is... Controller handle the business logic, i.e. they take a request from the user and decide how that is processed to give a result. This means taking data input by the user and passing it to models and then sending the resulting data from the model to the view where it is formatted for presentation to the user. Models handle raw data. Create, Retrieve, Update and Delete. Models do not need to really know what the data is for or how it will be presented. Some simple formatting of data such as concatenation or substring of fields but no html formatting etc Views present raw data in some format. Usually the format is HTML, but it could just as easily be CSV, XLS, PDF, JSON, XML or any other format you like. This means I generally have a bit more PHP code in my view than some people may like, but some complex formatting can be done with helpers, such as creation of form fields, which helps resuce or simplify the php code in the view
[eluser]k2zs[/eluser]
Thank you for that explanation, In the above model I would like to limit the results of "getSubNav" to the "ParentID" returned by "getPage". How would I pass variables between functions in my model? |
Welcome Guest, Not a member yet? Register Sign In |