Answer the question
In order to leave comments, you need to log in
How to correctly display the result of a query from the database to the "views" file index.php generated by the Yii library?
I am clarifying. This is not the first day I have been guided by a book to learn the web. I carry out all the examples in the same way as in the book. Just such an action with a request:
public function actionIndex()
{
// renders the view file 'protected/views/site/index.php'
// using the default layout 'protected/views/layouts/main.php'
$this->pageTitle = "Пользователи";
$command = Yii::app()->db->createCommand();
$command->select()->from("users")->order("name");
$users = $command->queryAll();
$this->render("index", array("users" => $users));
}
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
</head>
<body>
<center>
<h1>Пользователи</h1>
<?php foreach($users as $user) { ?>
<p><?php echo $user->name ?> (<?php echo $user->id ?>)</p>
<?php } ?>
</center>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
what is returned in this line?
$users = $command->queryAll();
what is the value of $users?
You have to do it like this:
<p><?php echo $user["name"] ?> (<?php echo $user["id"] ?>)</p>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question