如何引用下一个和上一个数组值


How to refer to next and previous array value?

我正在尝试制作一个简单的博客。在页面内容的底部,我放置了分页,它显示了指向下一个和上一个博客条目的链接,博客标题位于我从名为"pages"的数据库表中获取的链接中,列"menu_name"。为此,我首先通过名为"Blog"的类中的函数进行查询并创建一个数组,然后设置一些变量:

$sql = "SELECT * FROM pages ";
$sql .= "ORDER BY id DESC ";
$pages = Blog::find_by_sql($sql);
$blog_page = reset($pages);
$page_id = Blog::find_by_id(isset($_GET['page']) ? $_GET['page'] : $blog_page->id);

然后我为上一个博客链接编写了代码,如下所示:

public function pagination_previous(){
    global $address; //$address = "list_blogs.php?page=";
    global $page_id;
    global $pagination; //a class for storing functions that handle pagination
    global $pages;
    if($pagination->has_next_page()){
        echo "'n        <a class='"olderblog'" href=" . $address;
        echo $pagination->next_page();
        echo " title='"Newer Blog'">&laquo; ";
        foreach($pages as $page => $value){
            if($page_id->id + 1 == $value->id){
                echo $value->menu_name;
            }
        };
        echo "</a> ";   
    }else{
        echo "";
    }   
}

这是函数 has_next_page()

public function has_previous_page(){
    return $this->previous_page() >= 1 ? true : false;  
}

这是previous_page():

public function previous_page(){
    global $blog;
    return $this->current_page - 1;
}

这很好用,直到我决定创建管理区域,从中我可以删除博客条目。当我删除带有id的博客条目时,问题就开始了,比如说6。由于数据库表"pages"中没有更多id=6的项目,因此在缺失页面之前和之后的页面上指向下一页或上一页的链接为空,但仍指向page=6,因为has_previous_page()从$current_page中减去1。我半途而废地想出了摆脱这种混乱的方法,方法是像这样设置 pagination_next() 函数:

public function pagination_next(){
    global $address;
    global $pagination;
    global $page_id;
    global $pages;
    if($pagination->has_previous_page()){
        foreach($pages as $page => $value){
            if($page_id->id - 1 == $value->id){
                echo "'n        <a class='"newerblog'" href=" . $address;
                echo $value->id;
                echo " title='"Older Blog'">";
                echo $value->menu_name;
                break;
            }
        }
        echo " &raquo;</a> ";   
    }else{
        echo "";
    }   
}

但是,由于 foreach 循环中的 if 条件,这仍然不起作用。我需要在 pagination_previous() 和 pagination_next() 中设置此条件,以根据其数组索引 ($page) 而不是 id ($value->id) 引用下一个和上一个数组项,但我无法弄清楚这一点。数组$pages如下所示:

Array
(
[0] => Blog Object
    (
        [id] => 8
        [subject_id] => 2
        [menu_name] => Aliquam sed interdum
        [position] => 4
        [visible] => 1
        [created] => 
        [content] => 
    Aliquam sed interdum tortor. Vivamus et blandit velit...
[1] => Blog Object
    (
        [id] => 7
        [subject_id] => 2
        [menu_name] => Vestibulum ante ipsum
        [position] => 3
        [visible] => 1
        [created] => 
        [content] => 
    Vestibulum ante ipsum primis in faucibus orci luctus et ultrices...
[2] => Blog Object
    (
        [id] => 5
        [subject_id] => 2
        [menu_name] => Praesent massa mi
        [position] => 2
        [visible] => 1
        [created] => 
        [content] => 
    Praesent massa mi, pretium et leo vitae, aliquam...
[3] => Blog Object
    (
        [id] => 4
        [subject_id] => 2
        [menu_name] => Nam interdum tincidunt
        [position] => 1
        [visible] => 1
        [created] => 
        [content] => 
    Nam interdum tincidunt purus quis lacinia. Aliquam erat volutpat.
)

有人可以帮助我解决这个问题吗?

谢谢大家的回答,但不幸的是,他们都没有帮助我解决问题。在用尽所有选项之后,我明确得出结论,在不弄乱整个代码的情况下解决此问题的唯一方法是弄清楚如何在 if 条件中引用下一个或上一个现有的 $value->id,而无需添加或减去 1 进入下一页,因为这会导致数据库中缺少 id 时出现空链接。如果有人能解决这个难题,我将永远感激那个人。

您可以将previous_page函数转换为类似以下内容:

public function previous_page( )
{
    global $blog;
    $prev = -1; // Initialize
    $arrayKeys = array_keys( $blog );
    rsort( $arrayKeys );    //  Sort keys array in reverse order
    while( list( $key ,$cursor ) = each( $arrayKeys ) )
    {
        if( $cursor == $this->current_page )
        {
            if( list( $key ,$prev ) = each( $arrayKeys ) ) return $prev;
            break;
        }
    }
    return $prev;
}

以下可能会提供更多的理解:

//  Test data
$array = array( 0 => 'Obj 0' ,1 => 'Obj 1' ,2 => 'Obj 2' ,3 => 'Obj 3' ,4 => 'Obj 4' , );

$current = 3;   //  Try with any valid key of the array

//  Get the keys into an array
$arrayKeys = array_keys( $array );
//  Search for next
$next = -1; // Initialize
sort( $arrayKeys ); //  Sort the keys array
while( list( $key ,$cursor ) = each( $arrayKeys ) )
{
    if( $cursor == $current )
    {
        if( list( $key ,$cursor ) = each( $arrayKeys ) )
        {
            $next = $cursor;
        }
        break;
    }
}
//  Search for previous
$prev = -1; // Initialize
rsort( $arrayKeys );    //  Sort keys array in reverse order
while( list( $key ,$cursor ) = each( $arrayKeys ) )
{
    if( $cursor == $current )
    {
        if( list( $key ,$cursor ) = each( $arrayKeys ) )
        {
            $prev = $cursor;
        }
        break;
    }
}
if( $prev <> -1 )   echo '<br>Prev : ' ,$array[$prev];
if( $next <> -1 )   echo '<br>Next : ' ,$array[$next];

我建议使用sql来获取上一篇和下一篇博客条目

上篇文章

select * from pages where id = ( select max(id) from pages where id < <current_post_id> )

下一篇文章

select * from pages where id = ( select min(id) from pages where id > <current_post_id> )

全功能选项可能是在博客对象上有一个持久的序列索引字段。分页可以检查上一个/下一个序列索引。你可以编写你的函数来盲目地假设它们是真正顺序的(并确保在删除帖子时重新索引它们)以便于+1/-1实现,或者编写一个函数来找到下一个最高/最低(可能来自数据库)。这将允许您任意移动它们并确保存在。将顺序与任意对象 ID 分离。

在数组的每个循环使用函数更新数组索引之前array_values它将重新索引您的数组,因此现在您的逻辑将保持不变并正常运行。