Hi, I am a new user of codeigniter (2 weeks) and this is my first post
With CI3, i build a forum app.
A single topic shows posts that have posts.topic_id = topics.id
I want each posts on a single topic to have a permalink : I go to post #3 on topic-slug with this url:
http://example.com/forum/forum-slug/topic-slug/#3
I have a problem with pagination and posts permalink.
My pagination looks like this (20 posts per page) :
http://example.com/forum/forum-slug/topic-slug/ --> first page
http://example.com/forum/forum-slug/topic-slug/20 --> second page
http://example.com/forum/forum-slug/topic-slug/40 --> third page
Posts permalinks are given to topic view with the $posts object.
How would I give each post a permalink depending on where it is in the pagination page number :
Post #5 would have permalink
http://example.com/forum/forum-slug/topic-slug/#5
Post #25 (on the page 2 of pagination) would have permalink
http://example.com/forum/forum-slug/topic-slug/20/#25
Post #45 (on page 3 of pagination) would have permalink
http://example.com/forum/forum-slug/topic-slug/40/#45
I want it to be a dynamic attribution of posts permalinks, because if I want to change pagination $config['per_page'], I want all posts permalinks to change accordingly.
I am thinking of it, but I cannot find a good solution.
On my forum Controller, i have :
PHP Code:
public function topic($slug) {
// set basic variables
$slug = $this->uri->segment(3, 0);
$forum_slug = $this->uri->segment(2, 0);
$topic_id = $this->forum_model->get_topic_id_from_topic_slug($slug);
// get topic object
$data['topic'] = $this->forum_model->get_topic($topic_id);
// pagination
$limit = 15;
$this->load->library('pagination');
$config['base_url'] = base_url() . 'forum/' . $forum_slug . '/' . $slug . '/';
$config['total_rows'] = $this->forum_model->count_posts($topic_id);
$config['per_page'] = $limit;
$config['uri_segment'] = 4;
$config['num_links'] = 2;
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$data['pagination'] = $this->pagination->create_links();
// get all posts from this topic, limited by pagination
$posts = $this->forum_model->get_post($topic_id, $limit, $page);
// loop through posts and create post objects
foreach ($posts as $post) {
$author_id = $post->author_id;
$author = $this->user_model->get_user($author_id);
$data['posts'][] = (object)[
'id' => $post->id,
'date' => date("m-d-Y", strtotime($post->date)) . ' at ' . date("H:i:s", strtotime($post->date)),
'content' => $post->content,
'permalink' => base_url() . 'forum/' . $forum_slug . '/' . $slug . '/#' . $post->id, // this is the post permalink with it anchor
'edit_link' => base_url() . 'forum/' . $forum_slug . '/' . $slug . '/edit/#' . $post->id,
'report_link' => base_url() . 'forum/' . $forum_slug . '/' . $slug . '/report/#' . $post->id,
'author' => (object)[
'id' => $post->author_id,
'name' => $this->user_model->get_username_from_user_id($post->author_id),
'avatar' => base_url() . 'uploads/avatars/' . $author->avatar,
'permalink' => base_url(). 'user/' . $this->user_model->get_username_from_user_id($post->author_id),
'count_posts' => $this->forum_model->count_user_posts($post->author_id),
'count_topics' => $this->forum_model->count_user_topics($post->author_id)
]
];
}
// load view and give it datas
$this->load->view('header', $data);
$this->load->view('forum/topic/topic_single', $data);
$this->load->view('footer');
}
on my single topic view :
PHP Code:
<?php foreach ($posts as $post) : ?>
<article class="col-md-12 post">
<div class="row">
<div class="col-xs-2 col-md-1 text-center">
<p>
<img src="<?= $post->author->avatar ?>" alt="<?= $post->author->name ?>" class="img-circle" style="max-width: 100%; width: 100%;">
<small><a href="<?= $post->author->permalink ?>"><?= $post->author->name ?></a><br><?= $post->author->count_topics ?> topics<br><?= $post->author->count_posts ?> posts</small>
</p>
</div>
<div class="col-xs-10 col-md-11">
<div class="panel panel-default">
<div class="panel-body">
<header class="post-header">
<small><?= $post->date ?></small>
<!-- POST PERMALINK BELOW : -->
<small><a href="<?= $post->permalink ?>" id="<?= $post->id ?>" style="float: right">#<?= $post->id ?></a></small>
</header>
<div class="post-content">
<?= $post->content ?>
</div><!-- .post-content -->
<footer class="post-footer">
<a href=""><small>Report</small></a>
·
<a href=""><small>Edit</small></a>
</footer>
</div><!-- .panel-body -->
</div><!-- .panel -->
</div>
</div><!-- .row -->
</article><!-- .post -->
<?php endforeach; ?>
<div class="bottom-pagination col-md-12">
<?= $pagination ?>
</div>