【问题标题】:How can I add previous and next buttons in php?如何在php中添加上一个和下一个按钮?
【发布时间】:2019-01-14 16:56:34
【问题描述】:

第一次使用堆栈溢出,所以如果我发布不正确,请告诉我。所以我的网站是用 cakePHP 3.0 开发的。我目前在网站上有文章,并希望有一种方法可以让用户从文章移到文章。我应该提到我使用 get() 方法进行了这项工作,但是由于我的控制器的编写方式,我需要使用带有条件的选择查询。我可以创建一篇文章,直到将来的某个日期才发布。所以这段代码应该忽略任何尚未发布的内容,用户看不到那些文章。

一般来说都是 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);

在我看来.ctp 我有这个:

<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>

我觉得我非常接近,因为我的开发站点上不再出现任何错误。但是这个链接只是把我带到了我网站的主页。

感谢您的帮助!

【问题讨论】:

  • 您设置但从不使用$storyNextID 和$storyPreviousID?这些大概应该在您的where 子句中引用?你的 $storyNext 和 $storyPrevious 变量是什么样的?

标签: php cakephp cakephp-3.0


【解决方案1】:

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

所以当你回显$storyPrevious-&gt;slug PHP 时返回一个空字符串

所以在控制器中也选择 slug

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

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

【讨论】:

    【解决方案2】:

    感谢大家的投入。这真的很有帮助。直到现在,我还没有人真正得到反馈。我接受了上述输入并重新编码了我的逻辑。我不确定为什么要尝试使用这么多变量。我还通过一些挖掘发现,有人写了类似的东西,他们称之为邻居表。我结合了你的、我的和他们的来创造这个。到目前为止,它似乎在我的开发网站上工作,如果在我上线时发生变化,我会更新我的答案。

    在我的 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>';
        }
        ?>
    

    【讨论】:

      猜你喜欢
      • 2014-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多