And I also want this support dynamic content with server-side loaded data. The data will come from the following functions:
Categories:
public function get_categories(limit = '', sort = 'row_order', has_child_or_item = 'true', ignore_status = '', level = 0;
if (where = (isset(id)) ? ['c1.id' => where = (isset(id)) ? ['c1.id' => $id, 'c1.status' => 1] : ['c1.parent_id' => 0, 'c1.status' => 1];
}
And they will call and stored in:
this−>data[′blogs′]=this->blog_model->get_blogs(offset,limit, 'id', 'DESC', blogsearch,category_id);
$this->data['fetched_data'] = fetch_details('blog_categories');
The front-end can call blogs to get the data from get_blogs, and call fetched_data to get the data from blog_categories
To render all blogs dynamically from your server-side data and dynamically populate categories for filtering, you need to:
populate the categories nav dynamically (from $this->data['fetched_data']),
render the blogs list dynamically (from $this->data['blogs']),
(Optional) implement category filtering to let users filter blogs by category.
Backend (Controller) — Example PHP (CodeIgniter-style)
You probably already have something like this in your controller:
php
$offset = 0;
$limit = 10; // can be adjusted or used for pagination
$blog_search = NULL; // or from user input
$category_id = NULL; // category filtering
// Fetch blogs and categories dynamically
$this->data['blogs'] = $this->blog_model->get_blogs($offset, $limit, 'id', 'DESC', $blog_search, $category_id);
$this->data['categories'] = $this->blog_model->get_categories(); // Adjust as necessary, use your method or fetch_details
// Alternative if you use fetch_details() for categories:
// $this->data['fetched_data'] = fetch_details('blog_categories');
// pass data to the view here
$this->load->view('blogs_list', $this->data);
Frontend View
Replace your static categories/buttons and static blogs with PHP loops.
$blog_data = [
'total' => (int),
'data' => [
[
'id' => ...,
'title' => '...',
'image_url' => '...', // Not in your current example, you might have the image filename/path in db and need a helper e.g. get_image_url()
'tags' => ['tag1', 'tag2'],
'categories' => ['Technology', 'Science'], // you might need to process this
'published_date' => '2024-06-15',
...
],
...
]
];
Make sure to prepare the tags and categories arrays for each blog when fetching data (e.g. join multiple tags/categories or fetch them separately via related tables).
Optional: Adding filter interaction (JS example)
Add event listeners to category buttons to filter blogs dynamically (either via page reload with category query parameter or via AJAX).