pear2007 2016-05-10 16:36:35 5441次浏览 1条评论 6 2 0
/*
 * 获取上一篇
 */
public function getPrev(){
    return self::find()->where(['and','id<'.$this->id])->orderBy(['id'=>SORT_DESC])->one();
    //return self::find()->where(['and','channelid='.$this->channelid,'id<'.$this->id,'status=1'])->one();
}

/*
 * 下一篇
 */
public function getNext(){
    return self::find()->where(['and','id>'.$this->id])->one();
    //return self::find()->where(['and','channelid='.$this->channelid,'id>'.$this->id,'status=1'])->one();
}
觉得很赞
  • 评论于 2016-05-10 16:38 举报

    上面为模型部分

    <div class="row">
        <div class="col-sm-12 col-md-6">   <?php if($prev){ ?>   <a href="<?= Yii::$app->urlManager->createUrl(['sites/view','id'=>$prev->id]); ?>">上一篇: <?= $prev->sitename; ?> </a>
        <?php }else{ echo "暂无";} ?>
        </div>
        <div class="col-sm-12 col-md-6">
            <?php  // var_dump($next);?>
            <?php if($next){ ?>  <a href="<?= Yii::$app->urlManager->createUrl(['sites/view','id'=>$next->id]); ?>"> 下一篇:<?=  $next->sitename; ?>  </a>
            <?php }else{ echo "暂无";} ?>
        </div>
    </div>
    

    视图部分

    public function actionView($id)
    {
        $model=$this->findModel($id);
        $prev = $model->getPrev();
        $next = $model->getNext();
        return $this->render('view', [
            'model' => $this->findModel($id),
            'prev' => $prev,
            'next' => $next,
        ]);
    }
    

    控制器部分

    , , 觉得很赞
您需要登录后才可以评论。登录 | 立即注册