Reputation: 1
im newbie in Yii and have some troubles. My first problem: My view displays nothing.
View(news/index.php):
<p>Hi</p>
Second problem: I can't transmit variable from database to my layout(layouts/main.php) or view (watch first problem). My controller:
<?php
class NewsController extends Controller
{
public function actionIndex()
{
$result = News::model()->findAll('id');
$this->render('index', array('result'=>$result));
}
}
from main.php (layouts/main.php):
<div id = "content">
<?php
echo $content;
echo $result; //or $this->mydatafrombase;
?>
</div>
Or im trying to get variable in view news/index.php:
<p>Hi</p>
<?php
echo $this->mydatafrombase;
?>
Thanks for helping.
Upvotes: 0
Views: 862
Reputation: 31
I think your problem is $result variable at layout is wrong.
1.If you want use $result at layout. You must make a global variable at NewsController. And set value for it at action.
Controller:
<?php
//controller: NewsController
class NewsController extends Controller
{
public $result;
public function actionIndex(){
$this->result = News::model()->findAll();
$this->render('index');
}
}
View:
<!-- layout.php-->
<div id="content">
<?php
echo $content; //content of view of your action will be render at here
var_dump($this->result); // variable global of class NewsController
?>
</div>
2.If you want user $result variable at view of your action
Controller:
<?php
class NewsController extends Controller
{
public function actionIndex(){
$result = News::model()->findAll();
$this->render('index', array('result'=>$result));
}
} ?>
View:
<!-- news/index.php -->
<p>Hi</p>
<?php
var_dump($result);
?>
<!-- layout.php.-->
<div id="content">
<?php
echo $content; //content of view of your action will be render at here
?>
</div>
Upvotes: 1
Reputation: 326
If you want a layout to load a value from variable. And if a user is logged in.
You can use CWebUser::setState() and CWebUser::getState();
CController::render() only pass variable into index.php.
in your case. It would be simply to put $result into index.php
<?php
echo $result;//echo $this->mydatafrombase;
//yes simply $result would work. not $this->result. $this in this context refer to NewsController.
?>
Upvotes: 0