kenwang 2015-11-09 17:11:07 4959次浏览 3条评论 0 2 0

模型 SupesiteTest.php

namespace app\models;

use yii\db\ActiveRecord;

class SupesiteTest extends ActiveRecord
{
	public static function tableName()
	{
		return 'supe_lexicon';
	}
}

控制器 TestController.php

namespace app\controllers;

use yii\web\Controller;
use yii\data\Pagination;
use app\models\SupesiteTest;

class TestController extends controller
{
	public function actionIndex()
	{
		 //调用模型
		 $query = SupesiteTest::find();

		 //分页对象
		 $pagination = new Pagination([
		 	'defaultPageSize' => 10, 
		 	'totalCount' => $query->count() 
		 	]);

		 //查询数据   设置LIMIT   和  偏移量
		 $data = $query->limit($pagination->limit)
		 	->offset($pagination->offset)
		 	->all();

		 //渲染模板   
		 return $this->render('index',[
		 	"data" 			=> $data ,		//当前页数据
		 	"pagination" 	=> $Pagination  //分页对象
		 	]);
	}
}

视图 index.php

<?php

use yii\helpers\Html;
use yii\widgets\LinkPager;
 
?>
 	<table class="table">
 		<tr><th>ID</th><th>KeyWrods</th></tr>
<?php foreach($data as $val) { ?>
	<tr >
		<td><?= $val->id;?></td>
		<td><?= $val->keyWords;?></td>
	</tr>
<?php } ?>
</table>

<?php
echo LinkPager::widget(['pagination' => $pagination]);
?>
觉得很赞
您需要登录后才可以评论。登录 | 立即注册