codeigniter controllerからviewへ値を渡す方法
controllerからviewへ行く時は、controllerに、
$this->load->view('ビューフォルダ名');
を記載しますが、controllerで設定した値を使おうとしたら、
Undefined variable:〇〇
と表示されたことはないですか???
んで、調べました!!!
非常に簡単なので使って下さい!!!
controllerには普通に定義するが、「&color(crimson){$this->load};」の箇所だけ変更↓
<?php
class Blog extends Controller {
function index(){
$data['title'] = 'My Page Title';
$data['heading'] = 'My Page Heading';
$this->load->view('blog_view', $data);
}
}
?>
viewでは以下のように記載する!!
<html> <head> <title><?php echo $titie; ?></title> </head> <body> <h1><?php echo $heading; ?></h1> </body> </html>
