0

ビューページで変数を渡すことができません

これは私のコントローラーコードです

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

class Management extends CI_Controller {

        public function __construct()
       {
            parent::__construct();
            if($this->session->userdata('level') != 1){
                redirect('');
            } 
        }


        public function hotels()
        {   

                $this->load->model('model_hotels');

                $ss['most_view_hotels'] = '23';
                $this->load->view("management/header_mng_view");
                $this->load->view('management/hotels_mng_view' , $ss , true);
                $this->load->view("management/footer_mng_view");
        }

}

これがエラーです

PHP エラーが発生しました

重大度: 通知

メッセージ: 未定義の変数: most_view_hotels

ファイル名: management/hotels_mng_view.php

ライン番号: 22

hotels_mng_view.php

<?php 
  echo $most_view_hotels;
  foreach($most_view_hotels as $value):
?>
  <div class="row">
  <div class="cell en">adsf</div>
  <div class="cell en">1212</div>
  <div class="cell en">12</div>
  <div class="cell en">32</div>
  </div>
<?php endforeach;?>
4

4 に答える 4

1

コントローラーで:

$ss['most_view_hotels'] = '23';
$this->load->view("management/header_mng_view");
$this->load->view('management/hotels_mng_view',$ss);
$this->load->view("management/footer_mng_view");

あなたの見解では:

<?php echo $most_view_hotels?>
于 2012-11-01T12:14:29.867 に答える
0

Codeigniterマニュアルで述べたように:

There is a third optional parameter lets you change the behavior of the function
so that it returns data as a string rather than sending it to your browser.
This can be useful if you want to process the data in some way. If you set 
the parameter to true (boolean) it will return data. 
The default behavior is false, which sends it to your browser.

これが意味することは、あなたの行は

$this->load->view('management/hotels_mng_view' , $ss , true);

実際には、そのビューの結果をブラウザに送信するのではなく、文字列として php に返します。

あなたの実際の質問 ( undefined variable ) への答えはmanagement/hotels_mng_view.php、おそらくタイプミスに依存しています。

于 2012-11-01T12:49:14.597 に答える
0

問題は 3 番目のパラメータ、つまりtrue.

codeigniter のドキュメントにあるように:

There is a third optional parameter lets ..... 
Remember to assign it to a variable if you want the data returned

そして、割り当てていないコードでは、データを任意のコードに返しました。次のようなことを試すことをお勧めします。

public function hotels()
        {   

           $this->load->model('model_hotels');
           $data = array();
           $ss['most_view_hotels'] = '23';
           $data['header'] = $this->load->view("management/header_mng_view",,true);
           $data['body'] = $this->load->view('management/hotels_mng_view' , $ss , true);
           $data['footer'] = $this->load->view("management/footer_mng_view");
           $this->load->view("template",$data);
        }

 /*template view file*/
 <?php
    echo $header;
    echo $body;
    echo $footer;
 ?>

お役に立てれば。

于 2012-11-02T04:31:57.953 に答える
0

テンプレートでは、アイテムを次のように参照します...

$most_view_hotels

したがって、これがビューで行っていることを確認する必要がありますmanagement/hotels_mng_view.php

于 2012-11-01T12:18:59.693 に答える