Reputation: 23
First time user of stack overflow so if I am posting incorrectly please let me know. So my website is developed in cakePHP 3.0. I currently have articles on the site and would like a way for users to move from article to article. I should mention I had this working with the get() method but because of the way the my controller is written I need to use a select query with a condition. I am able to create an article and not post it until a future date. So this code is supposed to ignore anything that hasn't been published yet to users don't see those articles.
Super newbie with PHP in general let alone the cakePHP MVC framework so bear with me :)
In my controller within the public view function I have this:
//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);
And in my view.ctp I have this:
<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>
I feel like I'm super close since I am no longer getting any errors on my development site. But the link just sends me to the homepage of my website.
Thanks for the help!
Upvotes: 0
Views: 243
Reputation: 23
Thanks for the input everyone. It was really helpful. I have no one to really get feedback from, until now. I took the above input and re coded my logic a bit. I am not sure why I was trying to use so many variables. I also found through some digging, someone who wrote something similar, they called it a neighbors table. I combined yours, mine and theirs to create this. Looks like its working on my dev site as of now and Ill update my answer if that changes when I bring it live.
In my 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);
And in my 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>';
}
?>
Upvotes: 0
Reputation: 9398
there are probably many things that you can improve in your code**. But the main reason you are redirected to your main page is that you are just selecting the id of your Story but then in your view you need to echo the slug.
So when you are echoing $storyPrevious->slug
PHP returns an empty string
So in the controller select the slug too
$this->Story->find()
->select(['id', 'slug')
->where(['Story.pub_date <' => $todays_date])
->first();
** such as using helpers in your view and using Cakephp naming conventions
Upvotes: 1