yii 中条目的下一个/上一个按钮的最佳方式 [英] best way for next/previous buttons for entries in yii

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

问题描述

我想知道为数据库中的条目创建下一个/上一个按钮的最佳方法是什么.假设我们有一个包含图像的表格,每个图像都有一个 ID 和一个路径.如果当前图像的 ID 等于9",我如何获取下一个和前一个 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?

我已经在这里找到了解决方案https://stackoverflow.com/a/8874382但这似乎是最新的选择.如果我们的数据库中有超过 10000 个条目(图像),这将使服务器不堪重负.首先,它选择所有条目,然后尝试将所有结果复制到一个数组中.此后,将在数组中搜索当前 ID.很慢.

I already found a solution for this right here https://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.

我正在考虑为查询设置限制,但在此之后如何获取下一张和上一张图片?

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.

另外,同一个link还有一个思路,就是在当前图片的model中设置prev_id和next_id.但是这里我们有同样的问题,我们如何找到它们?即使我们找到了它们,在当前图片中存储有关上一张/下一张图片的数据是否正确?这似乎不正确,但我相信当您需要访问该数据时它会更方便.

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.

谢谢!

推荐答案

如果找到,此查询将仅返回下一行/上一行.更好的?(应该在你的模型类中实现).

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 中条目的下一个/上一个按钮的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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