2016-07-04 5 views
2

マイコントローラは次のようである:機能拡張コントローラでデータを取得するには?

<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

class Dashboard extends EX_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
    } 

    public function index() 
    { 
     $this->data['news'] = $this->dashboard_model->get_news(); 
     $this->load->view('dashboard/index_view', $this->data); 
    } 

マイEX_Controllerは、このようなものである:

<?php 
class EX_Controller extends CI_Controller 
{ 
    public $data; 
    public function __construct() 
    { 
     parent::__construct(); 

     $this->load->model('notification_model'); 
     $this->get_notification(); 
    } 

    public function get_notification() 
    { 
     $session = $this->session->userdata('login'); 
     $this->data['notification'] = $this->notification_model->get($session); 
     $this->data['count_notification'] = $this->notification_model->get_count_notification($session['customer_id']); 
    } 
} 
?> 

マイインデックスビューは次のようである:

<?php 
    foreach ($notification as $value) { 
?> 
     <li> 
     <?php 
      echo '<a href='.base_url().'notification/notif/'.$value['notification_id'].'>'; 
     ?> 
       <span class="time"> 
       <?php 
        echo time_elapsed($value['notification_created_date']); 
       ?> 
       </span> 
       <span class="details"> 
        <span class="label label-sm label-icon label-info"> 
         <i class="fa fa-bookmark"></i> 
        </span> <?php echo $value['notification_message']; ?> 
       </span> 
      </a> 
     </li> 
<?php 
    } 
?> 

実行されると、エラーが存在する:

Message: Undefined variable: count_notification 
Message: Undefined variable: notification 

はEXコントローラーでget_notification関数を呼び出すことはできません、私は機能get_notification(EX Controller)に入れ

は、すべてのコントローラ

に私の問題を解決するために、任意のソリューションを読んでいるように見えますか?

+0

を? – meda

+0

@meda、 '$ data'ではなく' $ this-> data'です。私は 'function get_notification'(EX_Controller)に' $ this-> data ['notification'] 'と' $ this-> data ['count_notification'] 'を取得します。その後、index_viewに送信します。 –

+0

はい、あなたのコードでそのプロパティを宣言した場所は表示されません。また、 'public $ data;' – meda

答えて

1

この問題を解決するには、ちょうどこの使用することです: `$のdata`が定義されている

$this->load->model('notification_model'); 
$this->get_notification(); 
+1

'public $ data;'なしで試してみます。それは働いている。だから、私はちょうどこれを使用します: '$ this-> load-> model( 'notification_model'); $ this-> get_notification(); ' –

+0

また、お気軽に編集してください。 – meda

+0

これを使ってください:' $ this-> load-> model( 'notification_model'); $ this-> get_notification(); ' '$ data'がなければ、うまくいきます。私はあなたの答えを更新していた –

関連する問題