D
D
des1roer2015-02-26 14:36:00
Yii
des1roer, 2015-02-26 14:36:00

Yii output table from database?

I'm trying to display a neat label in column2.
I do so.

<?php 
    $connection=Yii::app()->db;
    $sql="SELECT 
        `F_ID`,
        `F_Name`
        FROM 
        `tbl_department`;";
    $dataReader=$connection->createCommand($sql)->query();
    // привязываем первое поле (username) к переменной $username
    $dataReader->bindColumn(1,$F_ID);
    // привязываем второе поле (email) к переменной $email
    $dataReader->bindColumn(2,$F_Name);
    echo '<table>';
    while($dataReader->read()!==false)
    {
        // переменные $username и $email получают значения полей username и email текущей строки        
        echo '<tr><td>'.$F_ID.'</td><td>'.$F_Name.'</td></tr>';       
    }
    echo '</table>';
    ?>

I don't know how to do it with yii

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Ashgaliyev, 2015-02-26
@des1roer

To do this, you can use the CGridView widget

$sql = 'SELECT  F_ID, F_Name FROM tbl_department;';
$rawData = Yii::app()->db->createCommand($sql);
$count = Yii::app()->db->createCommand('SELECT COUNT(*) FROM (' . $sql . ') as count_alias')->queryScalar(); 
 
 
        $model = new CSqlDataProvider($rawData, array( 
                    'keyField' => 'F_ID', 
                    'totalItemCount' => $count,
                    'sort' => array(
                        'attributes' => array(
                            'F_ID'
                        ),
                        'defaultOrder' => array(
                            'F_ID' => CSort::SORT_ASC, 
                        ),
                    ),
                    'pagination' => array(
                        'pageSize' => 10,
                    ),
                ));
 
        $this->render('anActionView', array(
            'model' => $model,
        ));

In the view file:
$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'a-grid-id',
    'dataProvider' => $model,
    'ajaxUpdate' => true, 
    'filter' => null, 
    'columns' => array(
        array(
            'header' => 'Айди',
            'name' => 'F_ID',
        ),
        array(
            'header' => 'Имя',
            'name' => 'F_Name',
        ),
    ),
));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question