如何在 php 中添加上一个和下一个按钮?
How can I add previous and next buttons in php?
第一次使用堆栈溢出,所以如果我 post 有误,请告诉我。所以我的网站是用cakePHP 3.0开发的。我目前在该站点上有文章,并且希望用户可以从一篇文章移动到另一篇文章。我应该提到我使用 get() 方法进行了此操作,但由于我的控制器的编写方式,我需要使用 select 查询和条件。我可以创建一篇文章,而不是 post 它直到将来的某个日期。所以这段代码应该忽略任何尚未发布的内容,因为用户不会看到这些文章。
超级新手PHP一般更不用说蛋糕了PHP MVC框架所以多多包涵:)
在我的控制器中 public 视图函数中,我有这个:
//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);
在我的 view.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>
我觉得自己非常接近,因为我的开发站点上不再出现任何错误。但是 link 只是将我转到我网站的主页。
感谢您的帮助!
您的代码中可能有很多地方可以改进**。但是你被重定向到主页的主要原因是你只是 selecting 你的故事的 id 但在你看来你需要回应 slug。
所以当你回显 $storyPrevious->slug
PHP returns 一个空字符串时
所以在控制器中 select 鼻涕虫也是如此
$this->Story->find()
->select(['id', 'slug')
->where(['Story.pub_date <' => $todays_date])
->first();
** 例如在您的视图中使用助手和使用 Cakephp 命名约定
感谢大家的投入。这真的很有帮助。直到现在,我还没有人能真正得到反馈。我接受了上面的输入并重新编码了我的逻辑。我不确定为什么要尝试使用这么多变量。我还通过一些挖掘发现,有人写了类似的东西,他们称之为邻居table。我结合了你的、我的和他们的来创造这个。看起来它目前在我的开发站点上运行,如果在我上线时发生变化,我会更新我的答案。
在我的 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);
在我的 view.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>';
}
?>
第一次使用堆栈溢出,所以如果我 post 有误,请告诉我。所以我的网站是用cakePHP 3.0开发的。我目前在该站点上有文章,并且希望用户可以从一篇文章移动到另一篇文章。我应该提到我使用 get() 方法进行了此操作,但由于我的控制器的编写方式,我需要使用 select 查询和条件。我可以创建一篇文章,而不是 post 它直到将来的某个日期。所以这段代码应该忽略任何尚未发布的内容,因为用户不会看到这些文章。
超级新手PHP一般更不用说蛋糕了PHP MVC框架所以多多包涵:)
在我的控制器中 public 视图函数中,我有这个:
//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);
在我的 view.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>
我觉得自己非常接近,因为我的开发站点上不再出现任何错误。但是 link 只是将我转到我网站的主页。
感谢您的帮助!
您的代码中可能有很多地方可以改进**。但是你被重定向到主页的主要原因是你只是 selecting 你的故事的 id 但在你看来你需要回应 slug。
所以当你回显 $storyPrevious->slug
PHP returns 一个空字符串时
所以在控制器中 select 鼻涕虫也是如此
$this->Story->find()
->select(['id', 'slug')
->where(['Story.pub_date <' => $todays_date])
->first();
** 例如在您的视图中使用助手和使用 Cakephp 命名约定
感谢大家的投入。这真的很有帮助。直到现在,我还没有人能真正得到反馈。我接受了上面的输入并重新编码了我的逻辑。我不确定为什么要尝试使用这么多变量。我还通过一些挖掘发现,有人写了类似的东西,他们称之为邻居table。我结合了你的、我的和他们的来创造这个。看起来它目前在我的开发站点上运行,如果在我上线时发生变化,我会更新我的答案。
在我的 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);
在我的 view.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>';
}
?>