Commit 7efe5423 authored by 杨培's avatar 杨培

课程列表分页

parent 254878f9
...@@ -4,6 +4,7 @@ namespace common\models; ...@@ -4,6 +4,7 @@ namespace common\models;
use Yii; use Yii;
use yii\behaviors\TimestampBehavior; use yii\behaviors\TimestampBehavior;
use yii\data\Pagination;
use yii\helpers\HtmlPurifier; use yii\helpers\HtmlPurifier;
use yii\redis\ActiveRecord; use yii\redis\ActiveRecord;
...@@ -126,26 +127,37 @@ class Course extends \yii\db\ActiveRecord ...@@ -126,26 +127,37 @@ class Course extends \yii\db\ActiveRecord
/** /**
* 获取课程列表 * 获取课程列表
*
* @param int $classId * @param int $classId
* @param int $page 从0开始 * @param int $page 从0开始,如果传入null则使用系统分页机制,并返回 pagination 对象
* @param int $num * @param int $num
* @param int $status 要获取的状态,null代表获取全部,默认为只获取审核通过的课程
* @return array * @return array
*/ */
public static function getCourseListsByClassId($classId, $page=0, $num=10) public static function getCourseListsByClassId($classId, $page=0, $num=10, $status=self::STATUS_OK)
{ {
$status = self::STATUS_OK; $cond = ['class_id'=>$classId];
$condition = "class_id=:class_id and status=$status"; $status !== null && $cond['status'] = $status;
$params = [':class_id' => $classId];
//获取总数 //获取总数
$count = self::find()->where($condition, $params)->count(); $count = self::find()->where($cond)->count();
$start = $page * $num; $return = ['count'=>$count, 'data' =>[]];
if ($page === null) {
$pagination = new Pagination(['totalCount' => $count, 'pageSize'=>$num, 'forcePageParam'=>false]);
$offset = $pagination->offset;
$return['pagination'] = $pagination;
} else {
$offset = $page * $num;
}
if ($count > 0) { if ($count > 0) {
$data = self::find()->select('id,user_id,price,name,intro,preview_pic,recommend,play_count,comment_count,favorite_count,created_at')->where($condition, $params)->limit($num)->offset($start)->with('teachers')->asArray()->all(); $return['data'] = self::find()
->select('id,user_id,price,name,intro,preview_pic,recommend,play_count,comment_count,favorite_count,created_at')
->where($cond)->limit($num)->offset($offset)->with('teachers')->asArray()->all();
} }
return [
'count' => $count, return $return;
'data' => $data
];
} }
/** /**
......
...@@ -51,9 +51,8 @@ class MyCourseController extends \yii\web\Controller ...@@ -51,9 +51,8 @@ class MyCourseController extends \yii\web\Controller
*/ */
public function actionIndex() public function actionIndex()
{ {
$list = Course::getCourseListsByClassId($this->classStatus['id']); $list = Course::getCourseListsByClassId($this->classStatus['id'], null, 6, null);
return $this->render('index', ['list'=>$list['data'], 'pagination'=>$list['pagination']]);
return $this->render('index', ['list'=>$list['data']]);
} }
/** /**
......
...@@ -42,8 +42,7 @@ class TeacherManagerController extends \yii\web\Controller ...@@ -42,8 +42,7 @@ class TeacherManagerController extends \yii\web\Controller
*/ */
public function actionIndex() public function actionIndex()
{ {
$data = Teacher::fetchTeachers($this->classStatus['id']); $data = Teacher::fetchTeachers($this->classStatus['id'], 0, 100);
return $this->render('index', compact('data')); return $this->render('index', compact('data'));
} }
......
...@@ -2,8 +2,10 @@ ...@@ -2,8 +2,10 @@
/** /**
* @var $this yii\web\View * @var $this yii\web\View
* @var $list array * @var $list array
* @var $pagination yii\data\Pagination
*/ */
use yii\helpers\Url; use yii\helpers\Url;
use yii\widgets\LinkPager;
$this->title = '课程列表'; $this->title = '课程列表';
...@@ -12,7 +14,7 @@ $this->title = '课程列表'; ...@@ -12,7 +14,7 @@ $this->title = '课程列表';
*/ */
$qiniu = \Yii::$app->qiniu; $qiniu = \Yii::$app->qiniu;
?> ?>
<div class="cls-list-box"> <div class="cls-list-box clearfix">
<?php foreach ($list as $course) { ?> <?php foreach ($list as $course) { ?>
<div class="col-md-4"> <div class="col-md-4">
<div class="cls-box-main"> <div class="cls-box-main">
...@@ -33,4 +35,5 @@ $qiniu = \Yii::$app->qiniu; ...@@ -33,4 +35,5 @@ $qiniu = \Yii::$app->qiniu;
</div> </div>
</div> </div>
<?php } ?> <?php } ?>
</div> </div>
\ No newline at end of file <?=LinkPager::widget(['pagination'=>$pagination])?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment