Welcome Guest, Not a member yet? Register   Sign In
Global banners system
#1

[eluser]Choo[/eluser]
I have a site working on ci and a simple banners system on it. Data saved in mysql and small helper used to process it. So in my views banners can be shown such way:

Code:
<?=banner(4)?>

Where 4 is id of than banner.

So, if I have, for example, 5 banners, I have 5 requests to database. I don't like it. I want to preload all of them to an array:

Code:
<?=banners['4']?>

How to do it? I was trying to create hook, which creating this array and making it global, is it proper way?

Sorry for my english.
#2

[eluser]jedd[/eluser]
Hi Choo and welcome to the CI forums.

If you are okay with loading them all into an array - which will be n database calls - how do you see this an improvement over the 5 calls to the database currently? I suppose you may have some benefit from the caching mechanism, and I guess your banners don't change content very often.

In any case, I'd probably extend your controller - the [url="http://ellislab.com/codeigniter/user-guide/general/core_classes.html"]MY_Controller approach[/url] - and load them in there, saving them to the $this->data 'global' array, for access in all your views.
#3

[eluser]Choo[/eluser]
Thanks.

Yes, cache is a good soultion, but I mentiond banner system as example to find out the way how to use such global variables and arrays and I like your answer, I'll extend controller.

About the database calls. May be I don't understand you correct. To get all banners I can make only one request to fill an array.
#4

[eluser]jedd[/eluser]
[quote author="Choo" date="1239559376"]About the database calls. May be I don't understand you correct. To get all banners I can make only one request to fill an array.[/quote]

Ah, yes, of course. You're right. banner() makes a call each time, doesn't it, to the db.

Best approach would be to identify which banners you're going to use for the page view, in MY_Controller, and only load the banners you need. If you don't have many more than you are likely to be displaying, though, the overhead might not be worth it.
#5

[eluser]Choo[/eluser]
Yes, my array contains only banners I need. So, thank you again. Banners have been upgraded ).
#6

[eluser]Choo[/eluser]
I fill array in my_controller constructor, is it right?
#7

[eluser]jedd[/eluser]
[quote author="Choo" date="1239561373"]I fill array in my_controller constructor, is it right?[/quote]

I prefer to have functions in my MY_Controller class - and call those from the constructor. I think it's neater, and keeps the constructor easier to read (and make changes while testing).




Theme © iAndrew 2016 - Forum software by © MyBB