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

课程列表分页

parent 254878f9
......@@ -4,6 +4,7 @@ namespace common\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\data\Pagination;
use yii\helpers\HtmlPurifier;
use yii\redis\ActiveRecord;
......@@ -126,26 +127,37 @@ class Course extends \yii\db\ActiveRecord
/**
* 获取课程列表
*
* @param int $classId
* @param int $page 从0开始
* @param int $page 从0开始,如果传入null则使用系统分页机制,并返回 pagination 对象
* @param int $num
* @param int $status 要获取的状态,null代表获取全部,默认为只获取审核通过的课程
* @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;
$condition = "class_id=:class_id and status=$status";
$params = [':class_id' => $classId];
$cond = ['class_id'=>$classId];
$status !== null && $cond['status'] = $status;
//获取总数
$count = self::find()->where($condition, $params)->count();
$start = $page * $num;
$count = self::find()->where($cond)->count();
$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) {
$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,
'data' => $data
];
return $return;
}
/**
......
......@@ -51,9 +51,8 @@ class MyCourseController extends \yii\web\Controller
*/
public function actionIndex()
{
$list = Course::getCourseListsByClassId($this->classStatus['id']);
return $this->render('index', ['list'=>$list['data']]);
$list = Course::getCourseListsByClassId($this->classStatus['id'], null, 6, null);
return $this->render('index', ['list'=>$list['data'], 'pagination'=>$list['pagination']]);
}
/**
......
......@@ -42,8 +42,7 @@ class TeacherManagerController extends \yii\web\Controller
*/
public function actionIndex()
{
$data = Teacher::fetchTeachers($this->classStatus['id']);
$data = Teacher::fetchTeachers($this->classStatus['id'], 0, 100);
return $this->render('index', compact('data'));
}
......
......@@ -2,8 +2,10 @@
/**
* @var $this yii\web\View
* @var $list array
* @var $pagination yii\data\Pagination
*/
use yii\helpers\Url;
use yii\widgets\LinkPager;
$this->title = '课程列表';
......@@ -12,7 +14,7 @@ $this->title = '课程列表';
*/
$qiniu = \Yii::$app->qiniu;
?>
<div class="cls-list-box">
<div class="cls-list-box clearfix">
<?php foreach ($list as $course) { ?>
<div class="col-md-4">
<div class="cls-box-main">
......@@ -33,4 +35,5 @@ $qiniu = \Yii::$app->qiniu;
</div>
</div>
<?php } ?>
</div>
\ No newline at end of file
</div>
<?=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