2015-08-28 35 views
1

我有一個模板,可以將我的視圖加載到header,footer和main_content等部分。我在頁腳中有一個小部件,我想顯示特定類別的前5個帖子,例如「Current Affairs」。由於模板是以部分方式加載的,我想要一個加載這些數據並將其提供給模板的footer.php文件的函數。但是,該函數應該在構造函數中加載,以便控制器的所有其他功能不需要調用此函數。繼承人我的代碼。聲明在Codeigniter中從數據庫中獲取值的全局數組

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. 
}} 

這裏是我的template.php的內容:

<?php $this->load->view('includes/header'); ?> 

<?php $this->load->view($main_content); ?> 

<?php $this->load->view('includes/footer'); ?> 

的代碼優化或更好的編碼技術的任何建議都歡迎。

+0

爲什麼不爲你創建助手並隨時打電話給你的助手。 – Saty

+0

那麼這將是一個工作選項,但我很好奇我們如何製作一個全局數組並從數據庫中獲取值。 – cyberrspiritt

+0

顯然要存儲在數組中的數據非常大,並將其存儲在會話中是一個糟糕的主意 – cyberrspiritt

回答

1

這裏是示例代碼來存儲數據在全球陣列。

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); 
    } 
} 
+0

作爲全局的where子句這非常有幫助... thanx ajith – cyberrspiritt

1

要聲明地球陣列笨你有你的config file

$config['item_name']= array('post_category' => "Current Affairs", 'post_status' => "Published"); 

定義有關讀取配置項

$this->config->item('item_name'); 

所以,你的指數函數是

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. 
} 

已更新

您可以創建輔助文件,它

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); 
} 

你控制器

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不要忘了打電話給你的幫助文件

+0

您已寫入load_widgets()的內容,然後再次調用該函數。 – cyberrspiritt

+0

啊我的錯誤謝謝指出! – Saty

+0

我們應該可以在構造函數中調用函數load_widgets(),因爲許多其他函數都要求它自動加載。 – cyberrspiritt