【发布时间】: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