• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm not sure if I've done this properly?

#1
[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');
$header = array('mainmenu');

if you're on the blog page:
Code:
$left = array('');
$header = ('mainmenu');

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 {

    function Welcome()
    {
        parent::Controller();
    }
    
    function index()
    {
            $data['heading'] = "it seems to work";
            $this->load->view('template', $data);
    }
}
Any help on this greatly appreciated.

Thanks,

Justin

#2
[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

#3
[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');

class Doubleflushengine extends Controller {

    
    function Doubleflushengine() {

        parent::Controller();
        $this->load->database();

        //we need to find our controller id so that we can find which modules are published on this page.
        if ($this->uri->segment(1) === FALSE) {

                $controllerId = 1;

        } else {
        
                $query = $this->db->query('SELECT id FROM dbf_controllers WHERE controller = "'. $this->uri->segment(1) .'"  LIMIT 1');
                $row = $query->row();
                $controllerId = $row->id;

        }

        //done that (sort of) now, let's grab ALL module ids from the modules
        //published table where the controllerId = the one we just found.
        //if there are none - then we dont need to go any further as we arent loading nay mods on this page.

        $query = $this->db->query("SELECT module_id FROM dbf_modules_published WHERE controller_id = $controllerId");

        if ($query->num_rows() > 0) {

            foreach ($query->result() as $row) {

                $moduleId = $row->module_id;

                 //echo $moduleId .',<br />';
                //    $controllerId = $row->controller_id;
                    //echo $controllerId .',<br />';


                //nice! now, we need to work out what position on the page the module is to be published.
                //this involves two tables, modules_position and modules.
                //modules_published - id, module_id, controller_id
                //modules_position - id, position, description
                //modules - id, name, title, published, auth, description, position

                $this->db->select('modules_position.position, modules.name, modules.id');
                $this->db->from('modules_position');
                $this->db->join('modules', 'modules_position.id = modules.position');
                $this->db->where('modules.published = 1 and dbf_modules.id = '. $moduleId .'');

                $query = $this->db->get();

                //$query2 = $this->db->join('position, name', 'modules.position = modules_position.position
                 //   WHERE modules.id = ', 'left');

                if ($query->num_rows() > 0) {

                    foreach ($query->result() as $row) {

//this leaves us with the information we need to load our modules. The name of the module and the position to load it. In the template we simply tell it to load left modules and the mods below, will be loaded. We just need to get these results to the template now.

                        //echo $row->position .'<br />';
                        //echo $row->name .'<br />';
                        

                    }

                }

            }

        } else {

            echo 'no rows';

        }
            //cool, now we have our modules to load, we now need to perform whatever actions we need to be performing.

//Dont forget to...
            //Escape Queries
            //Query Bindings


//this now done in controller file.
            //$this->load->view('template', $data);


    }

}

//execute the function

            
            

/* End of file doubleflush_pi.php */
/* Location: ./system/plugins/doubleflush_pi.php */

#4
[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:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Doubleflush extends Controller {

    function Doubleflush()
    {

        parent::Controller();
        $this->load->model('doubleflushengine');

    }

    function index()
    {
        //we need to find our controller id so that we can find which modules are published on this page.
        if ($this->uri->segment(1) === FALSE)
        {
            $controllerId = 1;
        }
        else
        {
            $controllerId = $this->uri->segment(1);
        }

        $data['modules'] = $this->doubleflushengine->getModules($controllerId);
        //Now do a foreach or whatever in your view to deal with the modules
        $this->load->view('template', $data);
    }

}

Model:
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Doubleflushengine extends Model {

    
    function Doubleflushengine()
    {
        parent::Model();
        $this->load->database();
    }
    
    function getModules($controllerId)
    {  

        $query = $this->db->query("SELECT module_id FROM dbf_modules_published WHERE controller_id = $controllerId");

        if ($query->num_rows() > 0)
        {
            foreach ($query->result() as $row)
            {
                $moduleId = $row->module_id;
                $this->db->select('modules_position.position, modules.name, modules.id');
                $this->db->from('modules_position');
                $this->db->join('modules', 'modules_position.id = modules.position');
                $this->db->where('modules.published = 1 and dbf_modules.id = '. $moduleId .'');

                $query = $this->db->get();
            }

        } else {
            echo 'no rows';
        }
    }
}

(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?&gt; 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

#5
[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

#6
[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 Big Grin

You drink beer? I'll go get you a sixer while you wait!

#7
[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:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Doubleflushengine extends Model {


    function Doubleflushengine()
    {
        parent::Model();
        $this->load->database();
    }

    function getModules()
    {
        //we need to find our controller id so that we can find which modules are published on this page.

        if ($this->uri->segment(1) === FALSE) {
            $controllerId = 'doubleflush';
        }
        else {
            $controllerId = $this->uri->segment(1);
        }
        //This line gets us the items we need to load our modules, the module name, the module position (needed in template)
        $this->db->select('modules.name, modules_position.position, controllers.controller');
        //the table we're selecting them from
        $this->db->from('modules');
        //this is so we can grab the name of the module.
        $this->db->join('modules_published', 'modules_published.module_id = modules.id');
        //joining our position table so we can get the position data
        $this->db->join('modules_position', 'modules_position.id = modules.position');
        //joining our controllerId table so we can convert the url segment to a number.
        $this->db->join('controllers', 'controllers.id = modules_published.controller_id');
        //we only want modules that are published on this page.
        $this->db->where('controllers.controller = "'. $controllerId .'" AND dbf_modules.published = 1');

        $query = $this->db->get();
        //used this part just to echo the results to see that it all works
        //foreach ($query->result() as $row) {
        //    echo $row->name;
        //    echo $row->position;
        //}

    }
}

[controller]
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Doubleflush extends Controller {

    function Doubleflush()
    {

        parent::Controller();

    }

    function index()
    {
        //echo $controllerId;
        $data['modules'] = $this->doubleflushengine->getModules();
        //Now do a foreach or whatever in your view to deal with the modules
        //thanks for the tip Randy but I'm unsure what to foreach? Not sure if my returned data is stored correctly??
        $this->load->view('template', $data);
    }

}
/* End of file welcome.php */
/* Location: ./system/application/controllers/doubleflush.php */

[And finally, my butchered template with the end 'plan', will probably create a helper to make the template look 'nicer' when finished]
Code:
&lt;?php

&lt;!-- usual header stuff goes here --&gt;

&lt;?php
//<div id="header">

//if 'header' was returned in the results from our engine
if ( $results == 'header' )
{
    foreach {
        //then load the attached modules, that were returned as the second part of the results array
        //include_once $modulename .'.php'
    }
} else {
//do nothing
}

//<end div header>
//container div...
//<left column div>
if ( $results == 'left' )
{
    foreach {
        //then load the attached modules, that were returned as the second part of the results array
        //include_once $modulename .'.php'
    }
} else {
//do nothing
}
//end left div

//advertisement div, noisy neighbour div, etc etc etc div.

?&gt;

#8
[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')
{
    foreach($modules['name'] as $module)
    {
        print_r($module);
    }
}

if($modules['position'] == 'left')
{
   echo 'left';
}

etc...

Randy

#9
[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
Severity: Notice
Message: Undefined index: position
Filename: views/template.php
Line Number: 3


[template--&gt;]
Code:
1. &lt;?php
2.
3.        if($modules['position'] == 'header')
4.        {
5.           foreach($modules['name'] as $module)
6.            {
7.                print_r($module);
8.            }
9.        }
10. ?&gt;


[doubleflush controller --&gt; (last portion)]
Code:
function index()
    {
        //echo $controllerId;
        $data['modules'] = $this->doubleflushengine->getModules();
        $this->load->view('template', $data);
    }


[model --&gt; (last portion)]
Code:
$query = $this->db->get();
        //used this part just to echo the results to see that it all works
        //foreach ($query->result() as $row) {
        //    echo $row->name;
        //    echo $row->position;
        //}
        return $query->result_array();

    }

Can you tell me i'm missing something simple? (and possibly stupid?!)

Justin

#10
[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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.