下一个& Yii数据库中的先前id记录 [英] get next & previous id record in database on Yii

查看:81
本文介绍了下一个& Yii数据库中的先前id记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要下一个& Yii框架中数据库中的上一个id记录,以使导航按钮成为下一个和后一个?

I need next & previous id record in database on Yii framework to make navigation buttons next and back ?

推荐答案

我创建了一个函数来获取您想要的ID.我建议您在模型中声明它:

I made a function to get those ids your looking for. I suggest you to declare it in the model:

public static function getNextOrPrevId($currentId, $nextOrPrev)
{
    $records=NULL;
    if($nextOrPrev == "prev")
       $order="id DESC";
    if($nextOrPrev == "next")
       $order="id ASC";

    $records=YourModel::model()->findAll(
       array('select'=>'id', 'order'=>$order)
       );

    foreach($records as $i=>$r)
       if($r->id == $currentId)
          return isset($records[$i+1]->id) ? $records[$i+1]->id : NULL;

    return NULL;
}

要使用它,您要做的就是:

So to use it all you have to do do is this:

YourModel::getNextOrPrevId($id /*(current id)*/, "prev" /*(or "next")*/); 

它将返回下一个或上一个记录的相应ID.

It will return the corresponding id of the next or previous record.

我没有对其进行测试,因此请尝试一下,如果出现问题,请告诉我.

I didn't test it, so give it a try and if something goes wrong please let me know.

这篇关于下一个& Yii数据库中的先前id记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆