Welcome Guest, Not a member yet? Register   Sign In
Hooks with DB or other solution
#1

[eluser]Unknown[/eluser]
Hello there, greetings from Brazil and 10x for the good work. Sorry the bad english, here we go:

I had a standard header and footer for my website, so all the views go like this:

<?=$this->load->view("header");?>

Html text and generated text of the view

<?=$this->load->view("footer");?>

Fine. But after finished the website (always on this part), the client requested a modification and now I've a news ticker on the footer.

Doubt: how am I supposed to get db data directly from a view? 'couse otherwise I would have to change all my controllers (is, they are too much) to first get the news info and send it into the views

I tried to get a pre_controller hook to store the news info into a session var, but the hook doesn't connect to the database. I guess it is not loaded yet.

So, that's it.
#2

[eluser]kucerar[/eluser]
[quote author="Froskie" date="1186444431"]

Doubt: how am I supposed to get db data directly from a view? 'couse otherwise I would have to change all my controllers (is, they are too much) to first get the news info and send it into the views
[/quote]

Should be right there in the view:

Code:
var_dump( $this->db );

Query that.

Or maybe this doesn't work for the shared footer file?

-R
#3

[eluser]Unknown[/eluser]
Yes, it works. It's just I didn't want to process any data at the view. 10x anyway...
#4

[eluser]Hamilogre[/eluser]
I just came across this, and I have/had a similar problem. I also am trying to have dynamic data in my header which is called on every page. I wanted to get this data from a function in a model, and I noticed that if i tried to load the model from the view, it did not work, and it game me a "cannot perform this method on a non-member function" or something...

Like I said, pulling the data directly from the DB, instead of the model gives me the same results, but in the event that I change the logic in the model, I would have to change the header file as well to give me the same results. Any ideas?
#5

[eluser]Michael Wales[/eluser]
I'm not doing anything here to help the conversation, but we receive a lot of Brazilians that use the term 10x. What the hell does that mean?

I assume it means "good job" but I'm not so sure...

"10 times anyway"
"10 times for the good work"

* El Forum shrugs
#6

[eluser]Zeldinha[/eluser]
I'm also interested in ideas about this, since I'm trying to make a website and will run into this problem very soon. I made my own MVC system once (when I was trying to figure out how this worked) and I decided to simply added all the "generic" parts of the website into the main View (so I could call the methods from all the extended children). Maybe it was a bit ugly, but it worked, heh. Anyway, I guess we can't do that with CI... So how would it be a proper way to do it?

P.S. I'm not Brazillian, but I think it means "thanks" ( 10x -> tenx -> thanks ) Smile
#7

[eluser]ssjcory[/eluser]
I'm not sure if this fixes your problem but I find it easiest to break things up into a class with multiple variables for each segment. That way it can be changed easily. The best way to have headers/footers that are dynamic is by using a Templating Engine such as Smarty
-Cory
#8

[eluser]Michael Wales[/eluser]
ah... thanks

Man, you know you're getting old when you can't keep up w/ the kid's crazy acronyms any longer.
#9

[eluser]ssjcory[/eluser]
LOL i'm 20 and i dind'/t get it
-Cory
#10

[eluser]Rick Jolly[/eluser]
It might be a little late for the following suggestion:
Your problem is another example of why subclassing the CI controller is such a good idea. If all your pages had extended a single custom controller you could have just added the data for the footer in the custom parent controller's constructor using $this->load->vars();

Ok, another simple alternative that might actually help you at this stage:
You could create a simple helper that gets and returns the data, then just call that helper directly from the view. Within a helper you can get the CI instance using $CI =& get_instance();. Once you have the reference to the CI instance, you can do anything that you would do from within your controller (call models, libraries, access inputs (session, url, post), even load a view!).




Theme © iAndrew 2016 - Forum software by © MyBB