为在Yii中的条目下一个/ previous按钮最好的办法 [英] best way for next/previous buttons for entries in yii

查看:181
本文介绍了为在Yii中的条目下一个/ previous按钮最好的办法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是要创建下一代/ previous按钮,对数据库中的一个条目的最佳方式。
比方说,我们有一个表图像和每一个形象有一个ID和路径。
如果当前图像有ID等于9我怎样才能得到下和previous的ID?

I would like to know what is the best way to create next/previous buttons for an entry in the database. Let's say we have a table with images and every image has an ID and a Path. If the current image has ID equal to "9" how can I get the next and the previous IDs?

我已经找到了这一权利在这里的解决方案
http://stackoverflow.com/a/8874382
但是,这似乎是最新的选择。如果我们拥有超过10000项查询(图像),这将淹没服务器。首先,它选择所有项,然后尝试所有的结果复制在数组中。在此之后,该阵列中搜索当前ID。这是非常缓慢的。

I already found a solution for this right here http://stackoverflow.com/a/8874382 but this seems to be the latest choice. If we have over 10000 entries (images) in our database, this will overwhelm the server. First of all, it selects all entries and then it tries to copy all of the results in an array. After this, the array is searched for the current ID. It's very slow.

我在想设置为查询限制,但我怎样才能在这之后的下一个和previous图片?

I was thinking about a limit set to the query but how can I get the next and previous pictures after this?

另外一个(好,我认为)的解决办法是获得当前的画面和递减的ID /新ID找到递增,直到另一个图像。我认为这是比链接提供的功能要快。

Another (better, I think) solution would be to get the ID of the current picture and decrement/increment it until another image with the new ID is found. I think this is faster than the function provided in the link.

另外,在同一链路还有另一种想法,设置prev_id和NEXT_ID在当前画面的模式。但在这里,我们有同样的问题,我们如何找到他们呢?即使我们找到他们,这是正确的约preV /下一张数据存储在当前照片?它似乎并不是正确的,但我敢肯定它更方便,当你需要访问这些数据。

Also, in the same link there is another idea, to set the prev_id and next_id in the model of the current picture. But here we have the same problem, how do we find them? And even if we find them, is it correct to store data about prev/next pictures in the current picture? It does not seem to be right but I'm sure it's more convenient when you need to access that data.

感谢您!

推荐答案

该查询只会返回下一个/ previous排,若发现。更好?
(应该在你的模型类来实现)。

This query will only returns the next/previous row, if found. Better? (Should be implemented in your model class).

public function getNextId()
{
    $record=self::model()->find(array(
            'condition' => 'id>:current_id',
            'order' => 'id ASC',
            'limit' => 1,
            'params'=>array(':current_id'=>$this->id),
    ));
    if($record!==null)
        return $record->id;
    return null;
}
public function getPreviousId()
{
    $record=self::model()->find(array(
            'condition' => 'id<:current_id',
            'order' => 'id DESC',
            'limit' => 1,
            'params'=>array(':current_id'=>$this->id),
    ));
    if($record!==null)
        return $record->id;
    return null;
}

这篇关于为在Yii中的条目下一个/ previous按钮最好的办法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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