How to make a dynamic view with db data?


This is my first time posting here and it may sound like a weird/stupid question, but I feel completely lost right now ! Please give me a hand ! :(

I want to have on my website a dynamic page where one can consult information about people registered in a SQL table, preferably in an ergonomic fashion.

Here is what I’d like : B)


Capture du 2014-11-18 14:30:34.png

On the left, this is a list of people that are in my table. I want to display on the right information about one person when the user clicks on him in the list on the left.

In order to achieve this, I tried to use the following jquery script: (called on click on any of the names on the left)


function showStudent(str) {

    if (str == "") {




    if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari

        xmlhttp = new XMLHttpRequest();

    } else { // IE5 & 6

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");


    xmlhttp.onreadystatechange = function() {

        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

	    document.getElementById("txtHint").innerHTML = xmlhttp.responseText;


    }"GET", "phpscripts/getstudent.php?q=" + str, true);



This part worked out well, except when I try to call this external php script:



use yii\helpers\Html;

use yii\widgets\LinkPager;

use yii\data\Pagination;

use yii\db\Query;

$q = intval($_GET['q']);

$queryAS = (new Query())



    ->where(['id_student' => $q]);

$pagination = new Pagination([

    'defaultPageSize' => 10,

    'totalCount' => $query->count(),


$myAS = $query->offset($pagination->offset)



$queryPS = (new \yii\db\Query())



$myPS = $queryPS->all();



    <?php foreach ($myPS as $PS):

        foreach ($myPA as $PA):

	    if ($PS['id'] == $PA['id_problem_serie']): ?>


	    	<?= Html::encode("{$PS->name}") ?>


	    <?php endif;


    endforeach; ?>

    <?= LinkPager::widget(['pagination' => $pagination]) ?>


I get the following error for each yii class I try to use in my php script:

[Tue Nov 18 13:51:00.057269 2014] [:error] [pid 21178] [client ::1:47118] PHP Fatal error:  Class 'yii\\db\\Query' not found in /local/vanneste/public_html/basic/web/phpscripts/getstudent.php on line 9, referer: localhost/~vanneste/basic/web/index.php?r=teacher%2Fdashboard

At this point I didn’t have any clue anymore of what was going on… Is this even the correct way to make a dynamic page like the one I’d like ? What am I not understanding ?

Thank you so much for helping me out ! :)

Hi and welcome to Yii.

I would suggest to start from a non dynamic rendering (i.e. fetching the page shows the content you want) and then you can manipulate the content dynamically via AJAX in a second step.

This would probably make things a bit more easier to understand and learn.

This also would give you a better understanding of the MVC pattern and how to exploit and use it to your advance to obtain whatever you need (e.g. AJAX calls and whatnot).

the steps you probably need are the following, the code for them can be automatically generated via the interface Gii.

Here I’m also assuming you already have a database table.

  1. create the ActiveRecord model that represents your database table

  2. create a controller for your model (again, using Gii)

  3. create the view that will contain the list of users you want to display

  4. create an action within your controller for fetching the initial data that should fill the view you have just created: the ActiveRecord provides some simple and intuitive methods to do that.

  5. render the view and pass the fetched ActiveRecords to it and adjust the view to display the right fields. Using a ListView widget is probably the best solution to create this.

If you haven’t gone through it yet, the official guide is the best place to start from:

Hello and thank you The Peach for your answer, it’s been very helpful :)

I did as you suggested, and it is working now!


Screen Shot 2014-11-28 at 19.48.47.png

As you can see I have my list of students on the left, and on the right some data from multiple tables of my db.

However, I am still facing a problem I anticipated, and made me want to dynamically query the database with a php script : I have way too much data to just store it all in my page. Below you can see the generated html source code for the page shown above.


Screen Shot 2014-11-28 at 19.48.55.png

For each student, I already have roughly 2000 characters, and I am using almost no data. In production, I will need 10 times this amount for each student, for over 100 students… This is hundreds of Mo, maybe even a few Go of data. And the user is probably only going to look into 10% or 5% of it…

Therefore IMO I really need to dynamically query my tables, but I still don’t know how I can call properly my php script, or even if it is the right way to use. Can you point me the right direction ?

Thanks a lot!

Have a nice day

Nobody’s got any hint for me? :unsure:

I’ve searched the web for hours and tried to make it work with a php script as shown in my first post, but I might be getting it all wrong as I couldn’t have it to work after trying repeatedly… A little help would really be welcome!

How do you dynamically query a db in order to display data in a view?

Thank you