Welcome Guest, Not a member yet? Register   Sign In
Where is the best place to put this bit of code...
#1

[eluser]sanchothefat[/eluser]
Hi, I've only just started hacking around with CI. I'm not too hot on php but I get OO programming and how to use classes/methods and so on. What I'm trying to figure out how best to organise my code for a simple application I'm writing as part of a site.

My question is, say you have a section of the web page that you want on all pages of the site, but that bit has some dynamic data in it. How do you include that data across multiple controllers?

To put it in context on the site i'm working on there is a list of gig dates down the side. These are parsed from an iCal feed in my default controller. Should I create a public method to collect the data and call it in each controller and then just include the appropriate sub-view in the controller's main view file? If so where/how would you define that method?

Hopefully that makes some sense...

Cheers
#2

[eluser]webthink[/eluser]
There are a few approaches to achieve the modularity you're seeking. Do a search on the forums for 'modules' to see a couple popular third party solutions. Besides those, a simplistic way to achieve that is by putting the common functionality in libraries or helpers. You can call one view from within another too if the visual components don't change from one page to the next.
#3

[eluser]sanchothefat[/eluser]
Thanks for the tips. I'm happy just to use libraries at the moment, so I've got that working ok, but now it seems that the anchor() helper function is adding the url segments onto the current controller segment. eg:

anchor('user/jay','jays profile');

in default controller ->
Code:
<a href="/user/jay">jays profile</a>

in user controller ->
Code:
<a href="/user/user/jay">jays profile</a>


is this supposed to be like this? Can I make it work the same for every controller and if so some help doing it would be really appreciated. The way I need to use it would be possible for me to set a variable in the view with the anchor() functions in to make it think it's the default controller but I'm not so keen on that idea.

Thanks again, and for any further advice =]
#4

[eluser]sanchothefat[/eluser]
Sorry ignore that, i was missing the base_url in my config file!




Theme © iAndrew 2016 - Forum software by © MyBB