I'm not sure if I've done this properly? |
[eluser]JitsunRO[/eluser]
I've tried to make myself a simple CMS type application with CI and I've managed to do fairly well ( I think!) in that I've achieved most for the small goals I set. I wanted a solution that would allow me to load modules in a sidebar (or any other designated) position, on different pages all with the same template. ie main menu loads on all pages, login module loads only on main. So, I've setup a few tables and have all the queries in place to work out which modules I am to load when the page loads up. index.php-> loads login and main menu. index.php/blog-> loads main menu. I have set it all up in a small plugin that executes on autoload. plugin converts page url to id. checks id to see which, if any, modules are published to that page. gets module id and checks position that the module is to be loaded. The end result for the plugin (if you're on the main page) Code: $left = array('login, advert, polls'); if you're on the blog page: Code: $left = array(''); This is where I'm stuck, and it's probably a fairly simple solution. How do I now get those $left and $header arrays FROM my auto loaded plugin back to my Controller to send through to my template? (my template will load as such ( if ( $left ) { execute all left modules } Code: class Welcome extends Controller { Thanks, Justin
[eluser]Randy Casburn[/eluser]
Hi Justin - can you translate the term "plugin" into a term that is consistent with a term in the CI user guide? If not, is this a home grown class that is completely disconnected from the CI super object? Where are the arrays stored? Are they private memebers of your plugin object? Need some info pls. Randy
[eluser]JitsunRO[/eluser]
Hi Randy thanks for your reply. I may have the term wrong but far as I'm aware it's a plugin... (maybe that's where I've gone wrong to start with?!) Quote:Plugins work almost identically to Helpers. The main difference is that a plugin usually provides a single function, whereas a Helper is usually a collection of functions. Helpers are also considered a part of the core system; plugins are intended to be created and shared by our community.from here here is my complete 'plugin' (a bit messy!) I hope you can make sense of it. (stored in systems/plugins/doubleflush_pi.php As I stated in my previous post - it all executes via autoload. Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
[eluser]Randy Casburn[/eluser]
Wow - where do I start... First, this is a class and not a function. So it violates the definition of a "Plug in". If you want to see what a plug in is supposed to look like, keep reading the page your referenced and open the captcha_pi.php it references and take a look at it. There is not class definition, there is only a function just exactly as the page says. Second, what you have here is "written as" a controller (but is really mostly a model). Third, what you have here is a model (inside a controller - that's all the data access/management stuff) ---- So your question has changed shape a little I think... If you did things like this: Controller: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Model: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); (This model likely will need repair. I'm not familiar with your data model so I'm sure I broke this.) If you build your template to deal with the array of <=?$modules?> in a foreach loop (or whatever) in your view, you can will have access to your data. I hope this answers your question and clears things up a little. -------- The bottom line is that you were not building a "plug-in", you were building an MVC structure from the very beginning. You were doing things correctly you just didn't get things put together just quite right. Good job and welcome to CI. Randy
[eluser]Randy Casburn[/eluser]
** Disclaimer post ** I cut and pasted the above Model code! Justin - please rewrite the model code so that you're not running queries inside a loop! Just not a good thing to do. With enough imagination you can pull all that data with a single query without repeatedly hammering your DB server. You'll also need to return a value from the getModules method. I forgot to put in a return value from your query result. Randy
[eluser]JitsunRO[/eluser]
Thanks heaps Randy - I have another day off from my 'real' job so I'll start hammering away at the code again. Your answer really cleared things up for me, will keep you posted on how I go You drink beer? I'll go get you a sixer while you wait!
[eluser]JitsunRO[/eluser]
Alrighty then, I have made some more progress but am still stumped on parsing the data from my model back to my controller and then on to my template (view). I have rewritten my db query, fixed up the model. It all seems to make sense and when tested by inserting echos and changing pages (ie switch from index to blog to forums etc) it echoes the correct results. (very happy!) but as I stated, I cannot for the life of me nut the data transferring / display out. I've attached the model, the controller and the slashed down view showing what I wish to do. I'm not looking for a completed solution - just a little prod in the right direction (teach a man to fish etc etc etc) Justin [Model] Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); [controller] Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); [And finally, my butchered template with the end 'plan', will probably create a helper to make the template look 'nicer' when finished] Code: <?php
[eluser]Randy Casburn[/eluser]
In your model you have to return your results thus... right after $query = $this->db->get(); put... Code: return $query->result_array(); It appears you'll be returning a multidimensional array with 'positions' and 'names'... Then in your view you'll use the array something like this... Code: if($modules['position'] == 'header') Randy
[eluser]JitsunRO[/eluser]
Hi Randy and thanks again for your help. Unfortunately, I still cannot get the results through to the template? [the error] Code: A PHP Error was encountered [template-->] Code: 1. <?php [doubleflush controller --> (last portion)] Code: function index() [model --> (last portion)] Code: $query = $this->db->get(); Can you tell me i'm missing something simple? (and possibly stupid?!) Justin
[eluser]Randy Casburn[/eluser]
Well I took a huge leap off the cliff (a big guess) about the structure of the result set coming from your query. Hell, I didn't even know if there is a 'position' index in the array...and apparently there isn't. At some point, you've got to take ownership of this thing and be responsible for figuring out what is coming from your own DB queries. I've shown you exactly how to use your query result set in your views. I've shown you exactly how to structure your models. I've shown you exactly how to structure you controller. The documentation shows you how to do all this to. I can't peer through the veil of your imagination to determine your data model so I can build the rest of it for you too. You are very, very close and I know you can do this. In you view, print_r() or var_dump() the array that is returned by the query to determine what index to use to test for 'header' or 'left' or 'right' or whatever. Then replace 'position' in $modules['position'] with that index. Best of luck to you, Randy |
Welcome Guest, Not a member yet? Register Sign In |