dd

使用Yii框架加入”上一篇” “下一篇”功能

jerry Yii 2015年08月09日 收藏

首先是扩展Mode类,生成两个相应查

public function nextpost()
{
    return 
self::model()->find(array('condition'=>'id>:id','params'=>array(':id'=>$this->id),'order'=>'t.id
 ASC'));
}

public function prevpost()
{
    return 
self::model()->find(array('condition'=>'id<:id','params'=>array(':id'=>$this->id),'order'=>'t.id
 DESC'));
}

然后在相应view里加入代码:

<div>
    上一篇:
    <?php
    $prevpost=$model->nextpost();
    echo $prevpost?CHtml::link($prevpost->Label, $prevpost->Link):'没有了';
    ?>
</div>
<div>
    下一篇:
    <?php
    $nextpost=$model->prevpost();
    echo $nextpost?CHtml::link($nextpost->Label, $nextpost->Link):'没有了';
    ?>
</div>
dd