简体   繁体   中英

Displaying data from database in tabular form in Yii

I have some datas stored in my table Jobs. How can I display them in a tabular form in my view section? I have visited the Yii forums but have not got any specific options to perform the action.

The action for view job:

public function actionViewJob() {
    $criteria = "";
    $model = new ViewJob();

    /* What Should I do Here */

    $this->render('viewjob', array(
        'model' => $model
    ));
}

And the corresponding view to list data from database:

/* What should I do Here.  */
<h1>View Jobs</h1>

<div class="flash-success"></div>
<div class="form">
    <table width="100%" border="0" cellspacing="0" cellpadding="0" id="filtertable" style="float: left;">
        <thead>
            <tr>
                <th width="11%" class="rowtop">No</th>
                <th width="24%" class="rowtop">Title</th>
                <th width="10%" class="rowtop">Description</th>
                <th width="10%" class="rowtop">No_Vacancy</th>
                <th width="10%" class="rowtop">Contact Email</th>
                <th width="10%" class="rowtop">Edit</th>
                <th width="10%" class="rowtop">Delete</th>
            </tr>
        </thead>
        <tbody>
            <?php // foreach ($posts as $post): ?>
            <tr>
                <td width="11%">
                    <?php ?>
                </td>
                <td width="24%">
                    <?php ?>
                </td>
                <td width="14%">
                    <?php ?>
                </td>
                <td width="14%">
                    <?php ?>
                </td>
                <td width="14%">
                    <?php ?>
                </td>
                </a>
                </td>
                </a>
                </td>
            </tr>
            <?php //endforeach; ?>
        </tbody>
    </table>
    <!-- form -->

In your action, call your view.

public function actionViewJob() {
        $model = new Job('search');

        $params =array('model'=>$model,
        );

        $this->render('viewjob', $params);
    }

Get your data as a CActiveDataProvider object.

In your model, create a search function

public function search() {
    $criteria=new CDbCriteria;

    // TODO: Add other search criteria here
    $criteria->compare('username','Tom',true);

    return new CActiveDataProvider('Jobs', array(
        'criteria'=>$criteria,
        'sort'=>array(
            'defaultOrder'=>'username ASC',
        ),
    ));
}

Then, in your view

$this->widget('zii.widgets.grid.CGridView', array(
        'dataProvider' => $model->search(),
        'filter' => $model,
        'columns' => array(
            array(
                'name' => 'title',
                'type' => 'raw',
                'value' => 'CHtml::encode($data->title)'
            ),
            array(
                'name' => 'description',
                'type' => 'raw',
                'value' => 'description',
            ),
        ),
    ));

At first you should get table data in controller via CActiveDataProvider. And then in view you can use widget - CGridView. Using this widget you can set params that you need. Columns, filters etc. Also you can paste your code, and we will try to decide your problem. Yii is very easy :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM