To fix category-based pagination in your code, you can make some modifications. It seems like you are trying to implement pagination for a blog category page. Here's the modified code with comments to help you understand the changes:
phppublic function frontendBlogCategoryView($category_id)
{
// ... Your previous code ...
if ($b_cat_data) {
$url = "blog_category/" . $b_cat_data->category_slug;
$total_rows = $this->M_frontend->count_data_multi_conditional_join_like("b_blog", "b_id", "b_blog_category", "blog_id", ["b_status" => 1, "b_b_cat_status" => 1, "cat_id" => $b_cat_data->cat_id]);
// Calculate the total number of pages based on $par_page_data (items per page)
$total_pages = ceil($total_rows / $par_page_data);
// Get the current page number from the query string or default to 1
$page = $this->input->get('page') ? $this->input->get('page') : 1;
// Calculate the offset based on the current page
$offset = ($page - 1) * $par_page_data;
// Fetch blog posts for the current page
$data['blogList'] = $this->M_frontend->multipleJoinTable_like_limit("b_blog", "b_id", "b_blog_category", "blog_id", ["b_status" => 1, "b_b_cat_status" => 1, "cat_id" => $b_cat_data->cat_id], $par_page_data, $offset);
// ... Your other data retrieval logic ...
// Pass pagination information to the view
$data['pagination'] = [
'total_rows' => $total_rows,
'total_pages' => $total_pages,
'current_page' => $page,
'base_url' => $url,
];
} else {
$total_rows = 0;
$data['blogList'] = "";
$data['category'] = "";
$data['tagList'] = "";
// Handle the case where no data is found for the category
// You might want to display an appropriate message in your view.
}
// ... Your remaining code ...
$this->load->view($this->main_layout, $data);
}
With these modifications, your code should now handle category-based pagination correctly. It calculates the total number of pages, extracts the current page from the query string, and calculates the appropriate offset for fetching blog posts for the current page. Pagination information is also passed to the view for rendering pagination links.
Certainly! Here's the full code with the modifications for category-based pagination:
phppublic function frontendBlogCategoryView($category_id)
{
$data = $this->engine->store_nav('blog', 'frontendBlogCategoryView', 'Blog Category');
$b_cat_data = $this->M_frontend->get_single_row_information("b_category", ["cat_id" => $category_id, "cat_status" => 1]);
$data["seoPageData"] = $this->M_frontend->get_single_row_information("seo_contents", ["sc_slug" => "blog", "sc_status" => 1]);
$par_page_data = 3; // Number of items per page
$offset = pagination_offset($par_page_data); // Function to calculate the offset
if ($b_cat_data) {
$url = "blog_category/" . $b_cat_data->category_slug;
$total_rows = $this->M_frontend->count_data_multi_conditional_join_like("b_blog", "b_id", "b_blog_category", "blog_id", ["b_status" => 1, "b_b_cat_status" => 1, "cat_id" => $b_cat_data->cat_id]);
// Calculate the total number of pages based on $par_page_data (items per page)
$total_pages = ceil($total_rows / $par_page_data);
// Get the current page number from the query string or default to 1
$page = $this->input->get('page') ? $this->input->get('page') : 1;
// Calculate the offset based on the current page
$offset = ($page - 1) * $par_page_data;
// Fetch blog posts for the current page
$data['blogList'] = $this->M_frontend->multipleJoinTable_like_limit("b_blog", "b_id", "b_blog_category", "blog_id", ["b_status" => 1, "b_b_cat_status" => 1, "cat_id" => $b_cat_data->cat_id], $par_page_data, $offset);
$data['tagList'] = $this->M_frontend->get_data_multi_conditional('b_tag', ['t_status' => 1]);
$data['category'] = $this->M_frontend->get_data_multi_conditional('b_category', ['cat_status' => 1]);
// Pass pagination information to the view
$data['pagination'] = [
'total_rows' => $total_rows,
'total_pages' => $total_pages,
'current_page' => $page,
'base_url' => $url,
];
} else {
$total_rows = 0;
$data['blogList'] = "";
$data['category'] = "";
$data['tagList'] = "";
// Handle the case where no data is found for the category
// You might want to display an appropriate message in your view.
}
$data["total_rows"] = $total_rows;
$path = $this->viewPath . 'blog_view';
$data['side_menu'] = $this->load->view($this->side_menu, $data, TRUE);
$data['main_content'] = $this->load->view($path, $data, TRUE);
$this->load->view($this->main_layout, $data);
}
Make sure to adapt the code to your specific framework and database schema as needed. This code should correctly handle category-based pagination and display the appropriate blog posts for each page.