F
F
funtik522016-03-03 12:59:28
Yii
funtik52, 2016-03-03 12:59:28

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));
  }

Does not display any value from the list here:
<!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>

Instead of the "name" and "id" values, it's empty:
3c5d42493dcf4dff8d159f9115c5b55d.png

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2016-03-03
@funtik52

what is returned in this line?
$users = $command->queryAll();
what is the value of $users?

F
funtik52, 2016-03-03
@funtik52

You have to do it like this:

<p><?php echo $user["name"] ?> (<?php echo $user["id"] ?>)</p>

A
Alexander, 2016-03-04
@p0vidl0

Why are you learning an outdated version of the framework from an old book?
Find a book on Yii2, there are some in the public.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question