include external php in the view through a loop and pass variables to it |
[eluser]Spredemann[/eluser]
OK so I have been using codeIgniter for a little more than a week and I have gotten stuck for the first time... What I want to achieve is: - display a user profile page with info grabbed from the db (done) - display comments that other users have posted on the profile (trouble!!!) The thing is... this profile page is all done in parts, where the sidebar is one thing, the main profile where the info is displayed is another, header another, and so on... The comments are also a separate php file containing its html formatting, like a template. The reason why I'm doing so is to be able to easily change the design in one file and it will update the way comments are displayed in both profile pages, pictures, etc. In this comment template I have variables related to the commenter information, plus the comment itself. Ok so the main question is pretty much what the title says: How can I include these comments in my view, through a loop, while each time sending to the include the data from the commenter and the comment itself? ps:the loop part is not the problem, mostly the data sending and if there is any other obvious way to do this that I'm not seeing... thanks in advance!
[eluser]vbsaltydog[/eluser]
Why are you using an include to display comment data? Thats what views are for. You might want to look into view partials with HMVC.
[eluser]Spredemann[/eluser]
as I said, my page is made up of parts, like the default header and footer, plus pieces like the navigation sidebar, the main content area, a right sidebar with friends and stuff. I thought that doing so would be useful to be able to reuse the same templates over and over and when I modify the template, it will be modified everywhere. I don't know if that is a good practice or common as I am also kind of new to php, coming from an actionscript background. Anyway, as the comments are right in the middle of the html code, I thought that I would put some php code into the view itself to loop through the data sent by the controller containing a multidimensional array of comments... Is that a good idea or is there another way that I'm missing??? The header and footer are being called by the controller though.. I have found that doing a Code: $this->load->view ('viewpath', $data)
[eluser]CroNiX[/eluser]
Code: //get our header data
[eluser]Spredemann[/eluser]
thanks a lot!!! that approach looks great! I'm gonna try it right now
[eluser]TWP Marketing[/eluser]
[quote author="Spredemann" date="1328467201"]OK so I have been using codeIgniter for a little more than a week and I have gotten stuck for the first time... What I want to achieve is: - display a user profile page with info grabbed from the db (done) - display comments that other users have posted on the profile (trouble!!!) The thing is... this profile page is all done in parts, where the sidebar is one thing, the main profile where the info is displayed is another, header another, and so on... The comments are also a separate php file containing its html formatting, like a template. The reason why I'm doing so is to be able to easily change the design in one file and it will update the way comments are displayed in both profile pages, pictures, etc. In this comment template I have variables related to the commenter information, plus the comment itself. Ok so the main question is pretty much what the title says: How can I include these comments in my view, through a loop, while each time sending to the include the data from the commenter and the comment itself? ps:the loop part is not the problem, mostly the data sending and if there is any other obvious way to do this that I'm not seeing... thanks in advance! [/quote] @Spredemann You are using partial views, which I think is a good way to handle this. Rather than using php includes, use the CI loader class to create partial views. Pass data to the views using the second parameter ($data) of the loader class method: $this->load->view('file_name', $data, true/false) NOTE THE THIRD PARAMETER! In your controller, load the comments info (as an array) for the particular profile into the $comments_data array. You may do this with database calls to your model methods (MVC) or read them directly in the controller (not really MVC but it will work). Code: ... In your comments_view above, loop through the $comments array in whatever order you wish and echo this to the browser. Code: ... The page_view is like your template (it is a template). Code: ... Rather than using PHP includes, I recommend using partial views, which are explained in the user guide.
[eluser]Spredemann[/eluser]
@ TWP Marketing I see what you did there :lol: . I hadn't noticed the third parameter until now and I can really see how it is useful. Right now there are some questions in my mind about other details related to this approach but I guess I will first try it and see for myself. Anyway this really looks good and saves me from using the template parser which seems to make the page take longer to load as the documentation says. |
Welcome Guest, Not a member yet? Register Sign In |