Welcome Guest, Not a member yet? Register   Sign In
Using MY_Controller problem
#1

[eluser]JamesTaylor[/eluser]
I have some code set up which queries a database to return some event dates, descriptions and news items that are to be shown on every page across the site. I have got the code up and running in a template file via a controller currently.

From looking at the user guide, other posts and the wiki it appears that my best method to have this info shown on every page is via the MY_Controller.php. I am trying to follow the Wiki to implement it but am obviously making a mistake somewhere as when i run the code directly in the controller it works but when i try to port it out the MY_Controller i get errors in the view?

this is what i have in the MY_Controller file
Code:
<?php

class  MY_Controller  extends  Controller  {

    function MY_Controller ()
    {
        parent::Controller();
        
//Loads Next Competition Info
    $this->load->model('Notifications');
    $data['CompResults'] = $this->Notifications->NextCompetition(time());
    
//Loads Next Social Event Info
    $this->load->model('Notifications');
    $data['SocialResults'] = $this->Notifications->NextSocialEvent(time());
    
//Loads Latest News headlines
    $this->load->model('Notifications');
    $data['NewsResults'] = $this->Notifications->LatestNews();
    
    }
}

and this is what i have in the normal controller:
Code:
<?php

class Home extends MY_Controller {

//Home Page
    function index(){
    parent::MY_Controller();
    
    $data['main_content'] = 'Home';
    $data['title'] = 'Welcome to ';
    $data['h1'] = '.....';
    $data['h2'] = '.....';
    $data['BodyClass'] = 'Home';
    $data['FlashFile'] = 'crossfade_xml';
    $data['Image1'] = 'Image1.jpg';
    $data['Alt1'] = 'Pict1.jpg';
    $data['Image2'] = 'Image1.jpg';
    $data['Alt2'] = 'Pict2.jpg';
    $data['Image3'] = 'Image1.jpg';
    $data['Alt3'] = 'Pict3.jpg';
    $data['Image4'] = 'Image1.jpg';
    $data['Alt4'] = 'Pict4.jpg';
    
    $this->load->view('template', $data);

    }
}

I think for whatever reason the $data variables i am creating in the MY_Controller are not being made available to the normal Home controller?

I imagine its to do with the $this->$data element that is shown in the Wiki, but if i use $this->data instead of $data in my Home controller when loading the view and passing the info i end up with a view that displays loads of errors??
#2

[eluser]nicholas.byfleet[/eluser]
Try setting data as a class level variable that you could access like $this->data i think you might be confusing variable scope. see if you can access the other variables through parent::data or something like that. Sorry I couldn't be more help.
#3

[eluser]Phil Sturgeon[/eluser]
Code:
<?php

class  MY_Controller  extends  Controller  {

    var $data = array();

    function MY_Controller ()
    {
        parent::Controller();
        
//Loads Next Competition Info
    $this->load->model('Notifications');
    $this->data['CompResults'] = $this->Notifications->NextCompetition(time());
    
//Loads Next Social Event Info
    $this->load->model('Notifications');
    $this->data['SocialResults'] = $this->Notifications->NextSocialEvent(time());
    
//Loads Latest News headlines
    $this->load->model('Notifications');
    $this->data['NewsResults'] = $this->Notifications->LatestNews();
    
    }
}

and

Code:
<?php

class Home extends MY_Controller {

//Home Page
    function index(){
    parent::MY_Controller();
    
    $this->data['main_content'] = 'Home';
    $this->data['title'] = 'Welcome to ';
    $this->data['h1'] = '.....';
    $this->data['h2'] = '.....';
    $data['BodyClass'] = 'Home';
    $this->data['FlashFile'] = 'crossfade_xml';
    $this->data['Image1'] = 'Image1.jpg';
    $this->data['Alt1'] = 'Pict1.jpg';
    $this->data['Image2'] = 'Image1.jpg';
    $this->data['Alt2'] = 'Pict2.jpg';
    $this->data['Image3'] = 'Image1.jpg';
    $this->data['Alt3'] = 'Pict3.jpg';
    $this->data['Image4'] = 'Image1.jpg';
    $this->data['Alt4'] = 'Pict4.jpg';
    
    $this->load->view('template', $this->data);

    }
}
#4

[eluser]JamesTaylor[/eluser]
Thanks Phil, that works perfectly!

When Nicolas says try making it a class level variable is that what has been achieved by making the code $this->data instead of $data??

I am reasonably new to programing and certainly new to codeigniter.

Thanks for the help.

James
#5

[eluser]Phil Sturgeon[/eluser]
Yup. $data is a variable that only exists within the function (method) you are calling it in.

$this->data is a property (class/object variable) that will exist throughout the entire Controller class. Meaning that MY_Controller and Home all share it, as they all extend from each other.

Inheritance is a wonderful thing, but confusing when you are new to programming. :-)
#6

[eluser]asylmottaket[/eluser]
I am maybe asking stupid, but what if I would extend the CI controller diffrently for an admin section than for a public section?

I mean, is it possible to make two kinds of "MY_controller"'s ?
#7

[eluser]clip[/eluser]
In your MY_Controller Add another class that extends MY_Controller after your MY_Controller class


Code:
class MY_Admin extends MY_Controller
{
function __construct()
{
etc.......
}
}
#8

[eluser]asylmottaket[/eluser]
Would MY_Controller be a good place to stuff in all sorts of image uploading/processing, authentication checking functions/methods?
#9

[eluser]jedd[/eluser]
[quote author="asylmottaket" date="1257478214"]
Would MY_Controller be a good place to stuff in all sorts of image uploading/processing ...
[/quote]

Almost definitely not.

Quote: ... authentication checking functions/methods?

A qualified yes.
#10

[eluser]asylmottaket[/eluser]
Alright.. so now I've just dived into deep water..

I have now a Base_controller like the one under, and I tried something like clip suggested (if I've done it right):

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Base_controller
*/
class Base_controller extends Controller {
    
    var $data = array();
    
    function __construct()
    {
        parent::Controller();
        
        $this->data['page_id'] = end($this->uri->segments);
                
        $this->load->vars('some_test', array('tests' => 'Windows 3.1 rocks'));
    }
    
}

class Backend_controller extends Base_controller {
    
    function __construct()
    {
        parent::Base_controller();
    }
    
    function _post_array($post_vars)
    {
        $this->load->helper('security');
        
        $post_array = array();
                
        foreach ($post_vars as $key => $value)
        {
            $post_array[$key] = xss_clean($value);
        }
        
        return $post_array;
    }
}

// End File Base_controller.php
// File Source /system/application/libraries/Base_controller.php


The main issue is that nothing works.. Wink

The thing I'm trying to do, is for some admin-controller to extend the Backend_controller, and for public controllers to just extend Base_controller, if someone follow me.


Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Admin
*/
class Admin extends Backend_controller {
    
    function index()
    {
          $this->load->library('form_validation');
        $data = array();
        
        if($this->input->post('submit'))
        {
            if ($this->form_validation->run('test') === TRUE)
            {
                $post_array = $this->_post_array( $_POST );
                
                print_r($post_array);
            }
        }

        $this->load->view('test', $data);
    }

}
// End File Admin.php
// File Source /system/application/controllers/Admin.php




Theme © iAndrew 2016 - Forum software by © MyBB