Codeigniter pagination displays extra num links - codeigniter

When I search for a pages in my categories it displays the result of what pages are in that category.
For testing I have set the limit per page to 1 on my search_for
function.
Question/Problem: If there a only 2 pages that show up in result for some reason the pagination links display 5 links where it should only display 2 pagination links.
functions for model are on controller for testing.
I think the problem lies where the db->like and db->or_like in model. Why does it display 5 pagination links when should only display 2 if only 2 results found.
Controller
<?php
class Category_search extends Catalog_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$data['title'] = "Search Category";
$data['categories'] = array();
$results = $this->get_categories();
foreach ($results as $result) {
$data['categories'][] = array(
'category_id' => $result['category_id'],
'name' => $result['name']
);
}
$this->load->library('form_validation');
$this->form_validation->set_rules('page', 'Category');
$this->form_validation->set_rules('name', 'Name');
if ($this->form_validation->run() == FALSE) {
$this->load->view('theme/default/template/pages/search_category_view', $data);
} else {
redirect('pages/category_search/search_for' . '/' . $this->input->post('category'));
}
}
public function search_for($offset = NULL) {
$data['title'] = "Caregory Results";
$data['heading_title'] = "Search" .' - '. $this->get_category_name($this->uri->segment(4));
$data['pages'] = array();
$this->load->library('pagination');
$limit = 1;
$config['base_url'] = base_url('pages/category_search/search_for') .'/'. $this->uri->segment(4);
$config['total_rows'] = $this->get_total_pages();
$config['per_page'] = $limit;
$config['use_page_numbers'] = TRUE;
$config['uri_segment'] = 5;
$config['num_links'] = "16";
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
$this->pagination->initialize($config);
$page = ($this->uri->segment(5)) ? $this->uri->segment(5) : 0;
$results = $this->get_pages_within($config['per_page'], $page);
$this->load->model('catalog/tool/model_tool_image');
foreach ($results as $result) {
$data['pages'][] = array(
'category_id' => $result['category_id'],
'parent_id' => $result['parent_id'],
'name' => $result['name'],
'description' => $result['description'],
'image' => $this->model_tool_image->resize($result['image'], 280, 200)
);
}
$data['pagination'] = $this->pagination->create_links();
$this->load->view('theme/default/template/pages/search_category_search_for_view', $data);
}
// Todo move model functions to new model file when complete
public function get_categories() {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'category c', 'LEFT');
$this->db->join($this->db->dbprefix . 'category_description cd', 'cd.category_id = c.category_id', 'LEFT');
$query = $this->db->get();
return $query->result_array();
}
public function get_pages_within($limit, $offset) {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'page_to_category p2c', 'LEFT');
$this->db->join($this->db->dbprefix . 'page p', 'p.page_id = p2c.page_id', 'LEFT');
$this->db->join($this->db->dbprefix . 'page_description pd', 'pd.page_id = p2c.page_id', 'LEFT');
$this->db->like('p2c.parent_id', (int)$this->uri->segment(4));
$this->db->or_like('p2c.category_id', (int)$this->uri->segment(4));
$this->db->limit($limit, $offset);
$query = $this->db->get();
return $query->result_array();
}
public function get_total_pages() {
return $this->db->count_all($this->db->dbprefix . 'page');
}
public function get_category_name($category_id = 0) {
$this->db->where('category_id', (int)$category_id);
$query = $this->db->get($this->db->dbprefix . 'category_description');
if ($query->num_rows() > 0) {
$row = $query->row();
return $row->name;
} else {
return FALSE;
}
}
}

I suppose 5 is all the table rows with no where/like filter and it probably come from this function that count everything:
public function get_total_pages() {
return $this->db->count_all($this->db->dbprefix . 'page');
}
Here you should add the same where clause.

Simple fix thanks to #KyleK suggestion I copied the code from the get_pages_within function and then returned return $query->num_rows();
All working
public function get_total_pages() {
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'page_to_category p2c', 'LEFT');
$this->db->join($this->db->dbprefix . 'page p', 'p.page_id = p2c.page_id', 'LEFT');
$this->db->join($this->db->dbprefix . 'page_description pd', 'pd.page_id = p2c.page_id', 'LEFT');
$this->db->like('p2c.parent_id', (int)$this->uri->segment(4));
$this->db->or_like('p2c.category_id', (int)$this->uri->segment(4));
$query = $this->db->get();
return $query->num_rows();
}

Related

How to show continued numbered of data with pagination

I am trying to display data in an ordered list with pagination in codeigniter. The numbered works fine in first page number 1 until limit, but in the next page it counts down start to 1, not continuing from previous page.
here is the controller code :
public function index()
{
// init params
$params = array();
$limit_per_page = 2;
$start_index = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$total_records = $this->model_pesan->get_total();
$data['setting'] = $this->setting_model->get_file();
if ($total_records > 0)
{
// get current page records
$params["results"] = $this->model_pesan->get_current_page_records($limit_per_page, $start_index);
$params["ruangan"] = $this->ruangan_model->get_file();
$config['base_url'] = base_url() . 'home/index';
$config['total_rows'] = $total_records;
$config['per_page'] = $limit_per_page;
$config["uri_segment"] = 3;
$config['full_tag_open'] = '<nav><ul class="pagination justify-content-center">';
$config['full_tag_close'] = '</ul></nav>';
$config['num_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['num_tag_close'] = '</span></li>';
$config['cur_tag_open'] = '<li class="page-item active"><span class="page-link">';
$config['cur_tag_close'] = '<span class="sr-only">(current)</span></span></li>';
$config['next_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['next_tagl_close'] = '<span aria-hidden="true">»</span></span></li>';
$config['prev_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['prev_tagl_close'] = '</span></li>';
$config['first_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['first_tagl_close'] = '</span></li>';
$config['last_tag_open'] = '<li class="page-item"><span class="page-link">';
$config['last_tagl_close'] = '</span></li>';
$this->pagination->initialize($config);
// build paging links
$params["links"] = $this->pagination->create_links();
}
$this->load->view('template/header_user', $data);
$this->load->view('user/home', $params);
$this->load->view('template/footer_user', $data);
}
and this is the model code :
public function get_current_page_records($limit, $start)
{
$query = $this->db->query('set #row_number = 0');
$this->db->select('(#row_number:=#row_number + 1) as num ,reservasi.id_reservasi,DATE_FORMAT(reservasi.tanggal, "%d-%m-%Y") as tanggal, reservasi.acara, reservasi.waktu_mulai, reservasi.waktu_selesai, reservasi.keterangan, ruangan.nama_ruangan, user.nama, reservasi.organisasi, reservasi.notelp');
$this->db->from('reservasi');
$this->db->join('user', 'reservasi.id_user = user.id');
$this->db->join('ruangan', 'reservasi.id_ruangan = ruangan.id_ruangan');
$this->db->limit($limit, $start);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
return false;
}
public function get_total()
{
return $this->db->count_all_results('reservasi');
}
the ouput should be =
first page :
1
2
3
4
then in the next page :
5
6
7
8
Try to set the row_number to $start+1 instead of 0 on the model, so it will not start from 0 again on every page requested :
$query = $this->db->query('set #row_number = '.$start+1);

Why do i get the same values in all pages in CI pagination?

I'm kinda new in CI and i'm trying to create a proper pagination for my page.
I have the following model and controller and even if i manage to get the pagination links, when i visit the other pages i get the same entries again and again.
I also have applied an Ajax "GET" filtering for fetching results from db and i managed to reuse the url parameters when i change pages.
The problem is that i don't know if i should try to create an infinite scroll function or maintain the default CI pagination.
I'd like to hear your opinions too!
Thank you in advance!
public function get_filtered_pets($limit, $start){
$this->db->order_by('pet_created_at', 'DESC');
$this->db->join('users', 'users.user_id = pets.pet_user_id');
$this->db->join('pet_categories', 'pet_categories.pet_category_id = pets.category_id');
$pet_data = array();
if ( $this->input->get('petStatus') ) {
$pet_data['pet_status'] = $this->input->get('petStatus');
}
if ( $this->input->get('petCategory') ) {
$pet_data['category_id'] = $this->input->get('petCategory');
}
if ( $this->input->get('petGender') ) {
$pet_data['pet_gender'] = $this->input->get('petGender');
}
if ( !empty($pet_data) ) {
$this->db->where($pet_data);
}
$query = $this->db->get('pets');
if ($start > 0) {
$this->db->last_query();
}
return $query->result_array();
}
And my controller
public function index(){
$query = $this->db->get('pets');
$pet_num = $query->num_rows();
$config = array();
$config["base_url"] = base_url() . "pets/index/";
$config["total_rows"] = $pet_num;
$config["per_page"] = 12;
$config['use_page_numbers'] = TRUE;
$config['reuse_query_string'] = TRUE;
$config['attributes'] = array('class' => 'page-link');
$config["full_tag_open"] = '<ul class="pagination">';
$config["full_tag_close"] = '</ul>';
$config["first_link"] = "«";
$config["first_tag_open"] = "<li>";
$config["first_tag_close"] = "</li>";
$config["last_link"] = "»";
$config["last_tag_open"] = "<li>";
$config["last_tag_close"] = "</li>";
$config['next_link'] = '>';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '<li>';
$config['prev_link'] = '<';
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tag_close'] = '<li>';
$config['cur_tag_open'] = '<li class="page-item active"><a class="page-link" href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data['pets'] = $this->pet_model->get_filtered_pets($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
//View files
$this->load->view('templates/header', $data);
$this->load->view('pets/index', $data);
$this->load->view('templates/footer', $data);
}
You are not using the value of $limit and $start nowhere. See Limiting or Counting Results.
You should:
public function get_filtered_pets($limit, $start){
$this->db->limit($limit, $start);
/* ... */
return $query->result_array();
}
And also, when calling this function you have to multiply: per_page * page, to get the offset (starting index).
$this->pet_model->get_filtered_pets($config["per_page"], $page * $config["per_page"])
To count results correctly, I normally have two functions on model: "get($search, $offset)" and "count($search)".
The function count has exactly the same code on get, except the select fields.
See this code, I think you will get it: https://pastebin.com/dZG90Lzm.
ps: There's ways to optimize it and avoid duplicate code.

how to send value through function in codeigniter

public function index_pagination(){
$this->load->library("pagination");
$config = array();
$config["base_url"] = base_url("index/index");
$config["total_rows"] = $this->index_model->record_count();
$config["per_page"] = 2;
$config["uri_segment"] = 3;
// Styling pagination
$config["full_tag_open"] = "<ul class='pagination'>";
$config["full_tag_close"] = "</ul>";
$config["next_tag_open"] = "<li>";
$config["next_tag_close"] = "</li>";
$config["prev_tag_open"] = "<li>";
$config["prev_tag_close"] = "</li>";
$config["num_tag_open"] = "<li>";
$config["num_tag_close"] = "</li>";
$config["cur_tag_open"] = "<li class='active'><a>";
$config["cur_tag_close"] = "</li></a>";
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["page"] = $this->index_model->fetch_products($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
}
public function index(){
$this->index_pagination();
$this->load->view('frontend/index', $data);
}
How can I pass $data["page"] and $data["links"] from index_pagination() function to my view file through index function?
You can do like this..
<?php
public function index(){
$data = array();
$data = $this->index_pagination();
$this->load->view('frontend/index', $data);
}
public function index_pagination(){
$this->load->library("pagination");
$config = array();
$config["base_url"] = base_url("index/index");
$config["total_rows"] = $this->index_model->record_count();
$config["per_page"] = 2;
$config["uri_segment"] = 3;
// Styling pagination
$config["full_tag_open"] = "<ul class='pagination'>";
$config["full_tag_close"] = "</ul>";
$config["next_tag_open"] = "<li>";
$config["next_tag_close"] = "</li>";
$config["prev_tag_open"] = "<li>";
$config["prev_tag_close"] = "</li>";
$config["num_tag_open"] = "<li>";
$config["num_tag_close"] = "</li>";
$config["cur_tag_open"] = "<li class='active'><a>";
$config["cur_tag_close"] = "</li></a>";
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["page"] = $this->index_model->fetch_products($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
return $data;
}
I wrote two simple function in my controller, and used it. This is may not answer of your question but by using this at least you go DRY.
/**
* This function used provide the pagination resources
* #param {string} $link : This is page link
* #param {number} $count : This is page count
* #param {number} $perPage : This is records per page limit
* #return {mixed} $result : This is array of records and pagination data
*/
function paginationCompress($link, $count, $perPage = 10) {
$this->load->library ( 'pagination' );
$config ['base_url'] = base_url () . $link;
$config ['total_rows'] = $count;
$config ['uri_segment'] = SEGMENT;
$config ['per_page'] = $perPage;
$config ['num_links'] = 5;
$config ['full_tag_open'] = '<nav><ul class="pagination">';
$config ['full_tag_close'] = '</ul></nav>';
$config ['first_tag_open'] = '<li class="arrow">';
$config ['first_link'] = 'First';
$config ['first_tag_close'] = '</li>';
$config ['prev_link'] = 'Previous';
$config ['prev_tag_open'] = '<li class="arrow">';
$config ['prev_tag_close'] = '</li>';
$config ['next_link'] = 'Next';
$config ['next_tag_open'] = '<li class="arrow">';
$config ['next_tag_close'] = '</li>';
$config ['cur_tag_open'] = '<li class="active"><a href="#">';
$config ['cur_tag_close'] = '</a></li>';
$config ['num_tag_open'] = '<li>';
$config ['num_tag_close'] = '</li>';
$config ['last_tag_open'] = '<li class="arrow">';
$config ['last_link'] = 'Last';
$config ['last_tag_close'] = '</li>';
$this->pagination->initialize ( $config );
$page = $config ['per_page'];
$segment = $this->uri->segment ( SEGMENT );
return array (
"page" => $page,
"segment" => $segment
);
}
function userListing()
{
$this->load->model('user_model');
$this->load->library('pagination');
$count = $this->user_model->userListingCount();
$returns = $this->paginationCompress ( "userListing/", $count, 5 );
$data['userRecords'] = $this->user_model->userListing($returns["page"], $returns["segment"]);
$this->load->view('includes/header');
$this->load->view("users", $data);
$this->load->view('includes/footer');
}
Try this with your piece of code.
You can go for this repository to understand more https://github.com/kishor10d/Admin-Panel-User-Management-using-CodeIgniter

Codeigniter Perpage Link Not Showing First In URL Pagination links

I am making my table head links sortable and orderable how ever my per_page does not show up first
http://localhost/project-1/forum/category/1/&order=asc?per_page=1
It should show like
http://localhost/codeigniter/forum/category/1/?per_page=1&order=asc
Question when I click on the pagination links if the have been order or sorted how can I make sure that when I click on a pagination link even though has been order/sorted will still allways show ?per_page first in url.
$url = '';
if ($this->input->get('sort')) {
$url .= '&sort=' . $this->input->get('sort');
}
if ($this->input->get('order')) {
$url .= '&order=' . $this->input->get('order');
}
$config["base_url"] = base_url('forum/category') .'/'. $category_id .'/'. $url;
Controller
<?php
class Forum extends MX_Controller {
public function __construct() {
parent::__construct();
$this->load->model('catalog/forum/forum_model');
$this->load->library('pagination');
}
public function category() {
$this->document->set_title('Forums');
$category_id = $this->uri->segment(3);
if ($this->input->get('sort')) {
$sort = $this->input->get('sort');
} else {
$sort = 'message';
}
if ($this->input->get('order')) {
$order = $this->input->get('order');
} else {
$order = 'asc';
}
$url = '';
if ($this->input->get('per_page')) {
$url .= '?per_page=' . $this->input->get('per_page');
}
if ($this->input->get('sort')) {
$url .= '&sort=' . $this->input->get('sort');
}
if ($this->input->get('order')) {
$url .= '&order=' . $this->input->get('order');
}
$config["base_url"] = base_url('forum/category') .'/'. $category_id .'/'. $url;
$config["total_rows"] = $this->forum_model->total_category($category_id);
$config["per_page"] = 1;
$config['page_query_string'] = TRUE;
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
$this->pagination->initialize($config);
$start = ($this->input->get('per_page')) ? $this->input->get('per_page') : '';
$filter_data = array(
'limit' => $config["per_page"],
'start' => $start,
'category_id' => $category_id,
'sort' => $sort,
'order' => $this->input->get('order')
);
$data['threads'] = array();
$results = $this->forum_model->get_threads_for_forum($filter_data);
foreach ($results as $result) {
$data['threads'][] = array(
'thread_id' => $result['thread_id'],
'user_id' => $result['user_id'],
'username' => $result['username'],
'subject' => $result['subject'],
'link' => site_url('thread') . '-' . $result['thread_id'],
'total' => $this->forum_model->total_threads($result['thread_id']),
'date_created' => date('d-m-Y', strtotime($result['date_created'])),
'user_link' => site_url('user') . '-' . $result['user_id']
);
}
$data['header'] = Modules::run('catalog/common/header/index');
$data['footer'] = Modules::run('catalog/common/footer/index');
$data['menu'] = Modules::run('catalog/common/menu/index');
$data['pagination'] = $this->pagination->create_links();
$data['back'] = site_url('forum');
$data['thread'] = site_url('newthread') . '?fid=' . $category_id;
$url = '';
if ($order == 'asc') {
$url .= '?order=desc';
} else {
$url .= '?order=asc';
}
$data['message'] = site_url('forum/category') .'/'. $category_id .'/'. $url;
$data['sort'] = $sort;
$data['order'] = '';
$this->load->view('default/template/forum/forum_thread_view', $data);
}
}
Set your default category id if url does not contain:
if ($this->uri->segment(3) == 'null') {
$category_id = 1; //set default for null
} else {
$category_id = $this->uri->segment(3);
}
Edit your config['base_url'] and add set $config['suffix'] if $_GET is not
empty.
New Pagination config:
$config["base_url"] = base_url('forum/category'); //no need custom category id
if (count($_GET) > 0)
$config['suffix']='?'.http_build_query($_GET,'',"&");//to encode requested data
$config["per_page"] = 1;
$config['page_query_string'] = TRUE;
$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";
I don't see any:
$config[‘reuse_query_string’] = TRUE;
This will allow to use mixed type of urls. This was not possible before CI 3.0.

Methods inside model getting executed more than twice in codeigniter

I have a Model, that i am call inside a Controller function. I am calling viewcategory function at page loads and calling some module function but why its getting executed 2-3 times ?
CategoryPost Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CategoryPost extends CI_Controller {
function viewcategory($name)
{
$this->load->database();
$this->load->helper("url");
$this->load->helper('form');
$this->load->library('table');
$this->load->library('pagination');
$this->load->model('categorypostmod');
$this->load->model("site_model");
$page = $this->uri->segment(5);
$categCount = $this->categorypostmod->getCategorycount($name);
$config['base_url'] = "http://localhost/b3/index.php/CategoryPost/viewcategory/" . $name . "/page/";
$config['per_page'] = 2;
$config['num_links'] = 5;
log_message('info', 'count is ' . $categCount);
$config['total_rows'] = $categCount;
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['use_page_numbers'] = TRUE;
$config['next_link'] = 'Next';
$config['next_tag_open'] = '<li class="next page">';
$config['next_tag_close'] = '</li>';
$config['prev_link'] = ' Previous';
$config['prev_tag_open'] = '<li class="prev page">';
$config['prev_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li class="page">';
$config['num_tag_close'] = '</li>';
$config['uri_segment'] = 5;
$data['query'] = $this->categorypostmod->getCategorypost($name, $config['per_page'], $page);
// $records = $this->db->get('post', $config['per_page'], $page);
$this->pagination->initialize($config);
$this->load->view('script');
$this->load->view('head');
$this->load->view('cat_content_list', $data);
$this->load->view('aside');
$this->load->view('bottom');
$this->load->model('Aside_mod');
if($this->Aside_mod->check_cat_exists($name)){
$this->Aside_mod->update_cat_count($name, $this->Aside_mod->get_cat_count($name)+1);
}
else{
$this->Aside_mod->add_cat_views($name);
}
}//end function
}//end class
Model
class Aside_mod extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
function save_cat($name){
if($this->check_cat_exists($name)){
$this->update_cat_count($name,$this->Aside_mod->get_cat_count($name)+1);
}
else{
$this->add_cat_views($name);
}
}
function getpopular_categ(){
$this->db->select('tags');
$this->db->from('post');
return $this->db->count_all_results();
}
function add_cat_views($name){
$data = array(
'tagname' => $name,
'count' =>0
);
return $this->db->insert('tagcount', $data);
}
function update_cat_count($name,$countval){
$data = array(
'count' =>$countval
);
$this->db->where('tagname', $name);
$this->db->update('tagcount', $data);
}
function check_cat_exists($name){
$exist=false;
$this->db->select('tagname');
$this->db->where('tagname',$name);
$query = $this->db->get('tagcount');
if($query->num_rows() > 0){
log_message('info', 'Exists ');
$exist=true;
}
return $exist;
}
function get_cat_count($name){
$this->db->where('tagname',$name);
$query=$this->db->get('tagcount');
$co=0;
if ($query->num_rows() > 0){
foreach ($query->result() as $row)
{
$co= $row->count;
}
}
return $co;
}
function getCategorypost($catname,$lim1,$lim2){
$this->db->cache_on();
$this->db->select('*')->from('post')->like('post.tags',$catname)->limit($lim1, $lim2);
$query = $this->db->get();
return $query;
}
}
Only going by what you have provided so far... Looking at your controller- you are calling the model 2-3 times depending upon the result of your IF statement near the end!
So it's doing what you are telling it to do!
It would make more sense to refactor your Controller so you just make a single call to the model and have all that if,else code performed in the model.
So in your Model - create a new method to contain the code ( as a first iteration and could be refined)
public function save_cat($name)
{
if($this->check_cat_exists($name)) {
$this->update_cat_count($name, $this->get_cat_count($name)+1);
}
else {
$this->add_cat_views($name);
}
}
Then your controller becomes...
<snip>
$this->load->view('aside');
$this->load->view('bottom');
$this->load->model('aside_mod');
$this->aside_mod->save_cat($name);
}//end function
}//end class

Resources