Loading A Controller Within Controller Codeigniter - codeigniter

I would like to know if it is possible to make a MY_Controller that will let me do what I describe below in my controller file.
Just so I can load controllers like this:
<?php
class Home extends CI_Controller {
public function index(){
$data['column_left'] = $this->load->controllers('common/column_left');
$data['column_right'] = $this->load->controllers('common/column_right');
$data['content_top'] = $this->load->controllers('common/content_top');
$data['content_bottom'] = $this->load->controllers('common/content_bottom');
$data['footer'] = $this->load->controllers('common/footer');
$data['header'] = $this->load->controllers('common/header');
if (file_exists('/template/common/home.tpl')) {
$this->load->view('/template/common/home.tpl', $data));
} else {
$this->load->view('default/template/common/home.tpl', $data));
}
}

Related

Codeigniter global variable in view

I don't know if the title above is an correct title about my question.
Is there a way to declare a variable which we can access it from anywhere in view without need to redefine it again in each function in controller?
for example in controller file Students.php contains many function that handle the views, take a look below :
public function __construct() {
parent::__construct();
$data['count_student'] = $this->m_data->allStudent(); // count all students
}
public function index() {
$data['content'] = 'view-students';
$this->load->view('frontend/header' , $data);
}
public function showDetails() {
$data['content'] = 'view-detail-students';
$this->load->view('frontend/header' , $data);
}
I expected we can access $count_student in both view-students.php and view-detail-student.php without to define $data['count_student'] = $this->m_data->allStudent(); on each function that handle the view.
Is there possible ways to do that?
In the application/core/ create MY_Controller.php
<?php
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('m_data');
$this->data['count_student'] = $this->m_data->allStudent();
}
}
Controller use $this->data
class Students extends MY_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$this->data['content'] = 'view-students';
$this->load->view('frontend/header', $this->data);
}
public function showDetails() {
$this->data['content'] = 'view-detail-students';
$this->load->view('frontend/header', $this->data);
}
}
Now that you have extended the controller you shoudld be able to just go like
<?php echo $count_student;?>
On the view
I noticed that you can access variables in views without passing them if you declare them in the controller with $this->. Probably because they default to public visibility.
public function __construct() {
parent::__construct();
// $data['count_student'] = $this->m_data->allStudent(); // count all students
//
$this->count_all_the_students = $this->m_data->allStudents();
}
public function index() {
$data['content'] = 'view-students';
$this->load->view('frontend/header' , $data);
}
And then in the view you can use $this->count_all_the_students without putting it in the $data array.
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php
echo 'I have misled ' . $this->count_all_the_students . ' students with my MVC breaking suggestions.';
If you are using the same variable in every controller function then you have to declare it as follows
class Example extends CI_Controller{
protected $data = array();
public function __construct() {
parent::__construct();
$this->data['count_student'] = $this->m_data->allStudent();
}
public function index() {
$this->data['content'] = 'view-students'; //variables are same
$this->load->view('frontend/header' , $this->data);
}
public function showDetails() {
$this->data['content'] = 'view-detail-students'; //variables are same
$this->load->view('frontend/header' , $this->data);
}
}
This applies only if you have common variables for all views in this controller.
If you are using different variables then use the following code.
class Example extends CI_Controller{
protected $count_student= "";
public function __construct() {
parent::__construct();
$this->count_student = $this->m_data->allStudent();
}
public function index() {
$data['content'] = 'view-students'; //Variable is different
$data['count_student'] = $this->count_students;
$this->load->view('frontend/header' , $data);
}
public function showDetails() {
$data['details'] = 'view-detail-students'; // Variable is different
$data['count_student'] = $this->count_students;
$this->load->view('frontend/header' , $data);
}
}

Codeigniter - Passing data from MY_Controller to Main_controller to view

This is the complete code (I want to select the view through a variable set on the MY_Controller):
I would like to pass that variable but it doesn't "reach" the view it gives me
$ses_group = "not_logged_in" with a test echo and I'm not setting anything on the MY_Controller
class MY_Controller extends CI_Controller {
protected $special_data = array();
public function __construct()
{
parent::__construct();
}
function index() {
if (logged_in() == TRUE)
{
if (in_group('users'))
{
$this->special_data['ses_group'] = 'users';
}elseif (in_group('empresas'))
{
$this->special_data['ses_group'] = 'empresas';
}elseif (in_group('admin'))
{
$this->special_data['ses_group'] = 'admin';
}else{
// $this->special_data['ses_group'] = 'not_logged_in';
}
}
return $this->special_data;
}
The Main_Controller:
function index(
$data = array(
'ses_group' => $this->special_data
);
$this->load->view('auth/descricao_anuncio', $data);
)
The view:
<?php if($ses_group="not_logged_in"){ ?>
<li>Login</li>
<?php }elseif($ses_group="users"){ ?>
<li>Your Area</li>
<li>Logout</li>
<?php }elseif($ses_group="empresas"){ ?>
<li>Empresa</li>
<li>Logout</li>
<?php }?>
echo $ses_group;
Thanks againg!
Controller
<?php
class MY_Controller extends CI_Controller {
protected $special_data = array();
function MY_Controller () {
parent::Controller();
}
function special_data($val)
{
if(a){
$this->special_data['ses_group'] = 'users';
}elseif(b){
$this->special_data['ses_group'] = 'companies';
}else{
$this->special_data['ses_group'] = 'admin';
}
return $this->special_data;// return value of the function
}
}
You can send your value from controller to views by using $this-> in your MY_Controller
Controller
class Main_controller extends MY_Controller {
function __construct() {
parent::MY_Controller();
}
$this->data['group']= $this->special_data['ses_group'];// call function and pass parameter
$this->load->view('view_x', $this->data);
}
Views
<?php echo $group; ?>
Is this what you need
controller
class MY_Controller extends CI_Controller {
protected $special_data = array();
function special_data($val)
{
if($val=="a"){
$this->special_data = 'users';
}elseif($val=="b"){
$this->special_data = 'companies';
}else{
$this->special_data = 'admin';
}
return $this->special_data;// return value of the function
}
}
class Main_controller extends MY_Controller {
function __construct()
{
parent::__construct();
}
public function index()
{
$data = array(
'group' => $this->special_data('a')
);
$this->load->view('view_x', $data);
}
}
view
<?php echo $group; ?>

Codeigniter model not loading

I'm a newbe in codeigniter.
something is going wrong with I think the model.
this is the controler:
<?php
class Fuel extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('html');
$this->load->library('table');
}
public function image() {
$data['title'] = 'test';
$data['main_content'] = 'imagetest';
$this->load->view("template", $data);
}
public function overview() {
$this->load->model('Get_DB');
$this->Get_DB->overview() ;
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$this->load->view("template", $data);
}
when I load the image function, it works just fine, but the function overview is the problem.
this is my model:
<?php
class Get_DB extends CI_Model
{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function overzicht() {
$query = $this->db->query("SELECT * FROM invoer "
. "ORDER BY datum DESC");
$gen_query = $this->table->generate($query);
return $gen_query;
}
}
and this is my view:
<?php
echo $gen_query;
and if you want to know: my template is this:
<?php
$this->load->view('templates/header');
$this->load->view($main_content);
$this->load->view('templates/footer');
now when I open my view I get this message:
A PHP Error was encountered
Severity: Notice Message: Undefined variable: gen_query Filename:
views/overzicht.php Line Number: 3
in the model you see that I have made a var $gen_query
so why is that undifined?
regards,
Ralph
Try:
public function overview() {
$this->load->model('Get_DB');
$data = array();
$data['gen_query'] = $this->Get_DB->overzicht() ; #corrected model function and save the result in `gen_query`
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$this->load->view("template", $data);
}
In Controler:
public function overview() {
$this->load->model('Get_DB');
$result = $this->Get_DB->overview() ;
$data['title'] = 'overview';
$data['main_content'] = 'overview';
$data['re'] = $result;
$this->load->view("template", $data);
}
And In view page you can retrive the result
like
foreach($re->result() as $row)
{
//You can get each row data here $row->your_field_names
}

Codeigniter tidying up my controllers

I've followed Codeigniter's configuration straight out of the manual - and just wondered if there was a simpler or more efficient way of coding my controllers...
eg.
class Home extends CI_Controller {
public function index()
{
$this->load->helper('segment1');
$this->load->model('segment1/Leftsidebar_model');
$data['articles'] = $this->Leftsidebar_model->articles();
$this->load->model('segment1/Default_model');
$data['head'] = $this->Default_model->segment1();
$data['segment1'] = $this->Default_model->segment1();
$data['segment2'] = $this->Default_model->segment2();
$this->load->model('Rightsidebar_model');
$data['coming_up'] = $this->Rightsidebar_model->coming_up();
$data['featured_pages'] = $this->Rightsidebar_model->featured_pages();
$data['recommended_link'] = $this->Rightsidebar_model->recommended_link();
$data['testimonials'] = $this->Rightsidebar_model->testimonials();
$this->load->view('head_view', $data);
$this->load->view('header_view', $data);
$this->load->view('segment1/__leftSidebar_view', $data);
$this->load->view('segment1/__mainContent/default_view', $data);
$this->load->view('segment1/__mainContent/segment2_view', $data);
$this->load->view('__rightSidebar_view', $data);
$this->load->view('footer_view', $data);
}
}
helpers and models can be auto loaded, this can be specified in the confiq file. This will save you from having to manually load them.
As for the rest:
You can subclass CI_Controller. Eg:
My Controller extends CI controller and this would then contain a 2 methods
1) to load page head and header
2) to load page bottom
you could then subclass your controller from My_Controller and call those methods
load_page_top();
//insert whatever you have to load
load_page_bottom();
other than that the rest is up to you
Eg:
class Home extends MY_Controller
{
index()
{
$data = get_data();
load_page_top();
//insert your views here specific to the controller
load_page_bottom();
}
get_data()
{
//gather all your needed data here and return it as an array
return data;
}
}
I love this structure, clean and neat.
class Home extends CI_Controller {
public function index()
{
// Load libraries, helpers, models
$this->load->helper('segment1');
$this->load->model('segment1/Leftsidebar_model');
$this->load->model('segment1/Default_model');
$this->load->model('Rightsidebar_model');
// Data for views
$data = array(
'articles' => $this->Leftsidebar_model->articles(),
'head' => $this->Default_model->segment1(),
'segment1' => $this->Default_model->segment1(),
'segment2' => $this->Default_model->segment2(),
'coming_up' => $this->Rightsidebar_model->coming_up(),
'featured_pages' => $this->Rightsidebar_model->featured_pages(),
'recommended_link' => $this->Rightsidebar_model->recommended_link(),
'testimonials' => $this->Rightsidebar_model->testimonials()
);
// Load views
$this->load->view('head_view', $data);
$this->load->view('header_view', $data);
$this->load->view('segment1/__leftSidebar_view', $data);
$this->load->view('segment1/__mainContent/default_view', $data);
$this->load->view('segment1/__mainContent/segment2_view', $data);
$this->load->view('__rightSidebar_view', $data);
$this->load->view('footer_view', $data);
}
}
Of course, the view loading could be managed in other way, having a view loading all common sections.
I just write little additional library for rendering similar page blocks.
Something like this:
class Display_Lib{
private $_CI;
private $_template_data;
public function __construct()
{
$this->_CI =& get_instance();
}
public function set($key, $value)
{
$this->_template_data[$key] = $value;
}
public function get($key)
{
return $this->_template_data[$key];
}
public function get_template_data()
{
return $this->_template_data;
}
public function display_page($view, $data = array())
{
$this->set('content', $this->_CI->load->view($view, $data, TRUE));
$this->_CI->load->view('templates/main_template', $this->get_template_data());
}
}
Set this library in auto load:
$autoload['libraries'] = array('session', 'database', 'display_lib');
And call it in controller:
class Main extends CI_Controller{
public function index()
{
$some_data = array();
$this->display_lib->display_page('views/main_view', $some_data);
}
}

How to pass variables in codeigniter 2

I have following
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Hello extends CI_Controller {
var $name = 'test';
function index() {
$this->name = 'Andy';
$data['name'] = $this->name;
$this->load->view('you_view', $data); // THIS WORKS
}
function you() {
$data['name'] = $this->name;
$this->load->view('you_view', $data); // BUT THIS DOESN'T WORK
}
}
My question is how to I pass the $this->name = 'Andy'; to you() ??
Since it is being set in a different method of the controller, which equates to another request in your code, you will need to store it in a session variable to have it persist across page requests.
function index() {
$this->name = 'Andy';
$data['name'] = $this->name;
$this->session->set_userdata('name', $this->name);
$this->load->view('you_view', $data); // THIS WORKS
}
function you() {
$data['name'] = $this->session->userdata('name');
$this->load->view('you_view', $data); // BUT THIS DOESN'T WORK
}
If its a value that is part of the class you can put it in the constructor
class Hello extends CI_Controller {
public function __construct() {
parent::__construct();
// will be available to any method in the class
$this->name = 'andy';
}

Resources