Welcome Guest, Not a member yet? Register   Sign In
Need help with pagination query (Solved)
#1

[eluser]Leonel Folmer[/eluser]
Need help please, this is my controller, so the problem is when I count all news, if a news is with the status "Inactive" still showing in the pagination links without data, dont know if is clear enough.

Code:
function index($offset=0)
{
   $this->db->where('Status', 'Active');
   $this->db->order_by('date', 'desc');
   $query = $this->db->get('news',2,$offset);

   $data['news'] = $query->result();
      
   $config['base_url'] = base_url() .'news/index/';
  
   $config['total_rows'] = $this->db->count_all('news'); // how to count here just the Status "Active"?
   $config['uri_segment'] = '3';
   $config['per_page'] = '2';
  
  
   $i= $config['total_rows']/$config['per_page'];
   $config['num_links'] = round($i);

   $this->pagination->initialize($config);
   $data['pages'] = $this->pagination->create_links();
   //..
}
#2

[eluser]Bhashkar Yadav[/eluser]
Code:
$config['total_rows'] = $query->num_rows();
#3

[eluser]Leonel Folmer[/eluser]
Thank you for you time, i try but the pagination links disappear from my view.
#4

[eluser]Bhashkar Yadav[/eluser]
can you please try with the instructions given here http://ellislab.com/codeigniter/user-gui...ation.html
#5

[eluser]Leonel Folmer[/eluser]
Pagination works fine, just the count_all('news') I think is something missing, I dont want count all data, just the "Active".
#6

[eluser]InsiteFX[/eluser]
Code:
public function count_active_rows()
{
    // Your query here!

    return $query->num_rows();
}

$config['total_rows'] = $this->db->count_active_rows();
#7

[eluser]Leonel Folmer[/eluser]
Thank you, the links with no data still in my pagination. so if the status is "Inactive" the news dont show up in the page that is correct, but the links numbers still there because I use $this->db->count_all('news'). how can I change this?
#8

[eluser]InsiteFX[/eluser]
Code:
public function count_active_rows($offset = 0)
{
    // Your query here!
    $this->db->order_by('date', 'desc');
    $query = $this->db->get_where('news', array('Status' => 'Active'), 2, $offset);

    return $query->num_rows();
}

$config['total_rows'] = $this->db->count_active_rows();

All of your database code should be in a model...
#9

[eluser]Leonel Folmer[/eluser]
Thank you, I'm got this error:


A PHP Error was encountered
Severity: Notice
Message: Object of class CI_DB_mysql_result could not be converted to int
Filename: libraries/Pagination.php
Line Number: 117




A PHP Error was encountered
Severity: Notice
Message: Object of class CI_DB_mysql_result could not be converted to int
Filename: libraries/Pagination.php
Line Number: 123

#10

[eluser]Leonel Folmer[/eluser]
Thank you all for help, I solved the problem:

Code:
function index($offset=0)
{
   $this->db->where('Status', 'Active');

   $config['total_rows'] = $this->db->count_all_results('news');

   $this->db->or_where('Status', 'Active'); // just that way work, repeat or where after cont all results.

   $query = $this->db->get('news', 2, $offset);

   $dados['news'] = $query->result();

   $config['base_url'] = base_url() .'news/index/';

   $config['per_page'] = '2';
   $config['uri_segment'] = '3';
   $config['num_links'] = '2';

   $this->pagination->initialize($config);
   $data['pages'] = $this->pagination->create_links();
   //..
}

Now I will put that query in the model...




Theme © iAndrew 2016 - Forum software by © MyBB