Reputation: 946
I have a template that loads my view into parts like header, footer and main_content. I have a widget in the footer where i would like to display top 5 posts of a particular category, say "Current Affairs". Since the template is loaded in parts, i want a function that loads this data and provide it to the footer.php file of the template. However, this function should be loaded in the constructor so that all the other functions of the controller doesnt need to call this function. Heres my code.
class Home extends CI_Controller {
// public $data['latest_current_affairs'] = array(); // Tried this, doesnt work.
function __construct()
{
parent::__construct();
$this->load->model('Add');
$this->load->model('Fetch');
$this->load->library('form_validation');
$this->load->helper('date');
// $this->load_widgets(); //works fine if we print an array
}
public function load_widgets()
{
$where = array('post_category' => "Current Affairs", 'post_status' => "Published");
$orderby = NULL;
$limit = 5;
$data['latest_current_affairs'] = $this->Fetch->selectWhereLimit('post', $where, $orderby, $limit);
// print_r($data); //works fine if printed through here.
}
public function index()
{
$data['main_content'] = 'home';
$this->load_widgets();
$this->load->view('includes/template', $data);
print_r($data); //Here the data prints only the main_content index but not the latest_current_affairs index of the array.
}}
Here's the content of my template.php:
<?php $this->load->view('includes/header'); ?>
<?php $this->load->view($main_content); ?>
<?php $this->load->view('includes/footer'); ?>
Any suggestions for code optimization or better coding techniques are welcome.
Upvotes: 1
Views: 1356
Reputation: 22532
To declare globe array in codeigniter you have to define in your config file
$config['item_name']= array('post_category' => "Current Affairs", 'post_status' => "Published");
For fetching config item
$this->config->item('item_name');
So your index function would be
public function index()
{
$where=$this->config->item('item_name');
$orderby = NULL;
$limit = 5;
$data['latest_current_affairs'] = $this->Fetch->selectWhereLimit('post', $where, $orderby, $limit);
$data['main_content'] = 'home';
$this->load->view('includes/template', $data);
print_r($data); //Here the data prints only the main_content index but not the latest_current_affairs index of the array.
}
UPDATED
You can create helper file for it
function load_widgets()
{
$CI = get_instance();
$CI->load->model('Fetch');
$where=$CI->config->item('item_name');
$orderby = NULL;
$limit = 5;
return $latest_current_affairs = $CI->Fetch->selectWhereLimit('post', $where, $orderby, $limit);
}
And you Controller
function __construct()
{
parent::__construct();
$this->load->model('Add');
$this->load->model('Fetch');
$this->load->library('form_validation');
$this->load->helper('date');
$result=$this->load_widgets();
print_r($result);
}
ans don't forget to call your helper file
Upvotes: 1
Reputation: 258
Here is the sample Code To store data in global array.
class Check extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->data['f1'] = $this->f1();
}
function f1()
{
return "response1";
}
function f2()
{
$this->data['f2'] = "response2";
print_r($this->data);
}
}
Upvotes: 1