如何在php中添加上一个和下一个按钮?

如何在php中添加上一个和下一个按钮?,php,cakephp,cakephp-3.0,Php,Cakephp,Cakephp 3.0,第一次用户的堆栈溢出,所以如果我张贴错误,请让我知道。所以我的网站是用CakePHP3.0开发的。我目前在该网站上的文章,并希望为用户从一篇文章到另一篇文章的方式。我应该提到的是,我使用get方法实现了这一点,但由于我的控制器的编写方式,我需要使用带有条件的select查询。我能够创建一篇文章,直到将来某个日期才发布。因此,这段代码应该忽略任何尚未发布的内容,因为用户看不到这些文章 PHP的超级新手,更不用说cakePHP MVC框架了,请耐心听我说: 在“公共视图”功能内的控制器中,我有以下内

第一次用户的堆栈溢出,所以如果我张贴错误,请让我知道。所以我的网站是用CakePHP3.0开发的。我目前在该网站上的文章,并希望为用户从一篇文章到另一篇文章的方式。我应该提到的是,我使用get方法实现了这一点,但由于我的控制器的编写方式,我需要使用带有条件的select查询。我能够创建一篇文章,直到将来某个日期才发布。因此,这段代码应该忽略任何尚未发布的内容,因为用户看不到这些文章

PHP的超级新手,更不用说cakePHP MVC框架了,请耐心听我说:

在“公共视图”功能内的控制器中,我有以下内容:

//get story id for next and previous buttons
    $todays_date = date('Y-m-d H:i:s');
    $this->loadModel('Story');
    $storyID = $story->id;
    $storyNextID = $storyID + 1;
    $storyPreviousID = $storyID - 1;
    $storyNext = $this->Story->find()->select('Story.id')->where(['Story.pub_date <' => $todays_date])->first();
    $this->set('storyNext', $storyNext);
    $storyPrevious = $this->Story->find()->select('Story.id')->where(['Story.pub_date <' => $todays_date])->first();
    $this->set('storyPrevious', $storyPrevious);
<div class="next row" align="center">
    <?php
    if(!empty($storyPrevious)) {
        echo '<a class="btn btn-secondary" style="width:50%" href="' .BASE_URL. '/' .$storyPrevious->slug. '" role="button">Previous Story</a>';
    }
    if(!empty($storyNext)) {
        echo '<a class="btn btn-secondary" style="width:50%" href="' .BASE_URL. '/' .$storyNext->slug. '" role="button">Next Story</a>';
    }
    ?>
    </div>
在我看来,ctp我有以下几点:

//get story id for next and previous buttons
    $todays_date = date('Y-m-d H:i:s');
    $this->loadModel('Story');
    $storyID = $story->id;
    $storyNextID = $storyID + 1;
    $storyPreviousID = $storyID - 1;
    $storyNext = $this->Story->find()->select('Story.id')->where(['Story.pub_date <' => $todays_date])->first();
    $this->set('storyNext', $storyNext);
    $storyPrevious = $this->Story->find()->select('Story.id')->where(['Story.pub_date <' => $todays_date])->first();
    $this->set('storyPrevious', $storyPrevious);
<div class="next row" align="center">
    <?php
    if(!empty($storyPrevious)) {
        echo '<a class="btn btn-secondary" style="width:50%" href="' .BASE_URL. '/' .$storyPrevious->slug. '" role="button">Previous Story</a>';
    }
    if(!empty($storyNext)) {
        echo '<a class="btn btn-secondary" style="width:50%" href="' .BASE_URL. '/' .$storyNext->slug. '" role="button">Next Story</a>';
    }
    ?>
    </div>
我觉得我已经非常接近了,因为我的开发站点上不再出现任何错误。但是这个链接只是把我发送到我网站的主页


谢谢你的帮助

您的代码中可能有很多地方可以改进**。但是你被重定向到主页的主要原因是你只是在选择你的故事的id,但是在你的视图中你需要回显这个片段

因此,当您回显$storyPrevious->slug PHP时,将返回一个空字符串

因此,在控制器中也选择slug

$this->Story->find()
    ->select(['id', 'slug')
    ->where(['Story.pub_date <' => $todays_date])
    ->first();

**例如在视图中使用帮助程序和使用Cakephp命名约定

谢谢大家的输入。这真的很有帮助。到目前为止,我还没有人能真正得到反馈。我接受了上面的输入并重新编码了我的逻辑。我不知道为什么我要使用这么多变量。我还通过一些挖掘发现,有人写了类似的东西,他们称之为邻居表。我把你的,我的和他们的结合起来创造了这个。看起来它现在正在我的开发网站上工作,如果我把它带到现场,我会更新我的答案

在我的StoryController中:

    $todays_date = date('Y-m-d H:i:s');
    $this->loadModel('Story');
    $id = $story->id;
    $storyNext = $this->Story->find()
        ->select(['id', 'slug'])
        ->order(['id' => 'ASC'])
        ->where(['id >' => $id, 'Story.pub_date <' => $todays_date])
        ->first();
    $this->set('storyNext', $storyNext);
    $storyPrevious = $this->Story->find()
        ->select(['id', 'slug'])
        ->order(['id' => 'DESC'])
        ->where(['id <' => $id, 'Story.pub_date <' => $todays_date])
        ->first();
    $this->set('storyPrevious', $storyPrevious);
在我看来.ctp:

    <?php
    if(!empty($storyPrevious)) {
        echo '<a href="' .BASE_URL. '/' .$storyPrevious->slug.'">Previous Story</a>';
    }
    if(!empty($storyNext)) {
        echo '<a href="' .BASE_URL. '/' .$storyNext->slug.'">Next Story</a>';
    }
    ?>
您设置了,但从未使用$storyNextID和$storyPreviousID?这些应该在where子句中引用?您的$storyNext和$storyPrevious变量是什么样子的?