[eluser]Ablankzin[/eluser]
I try to get the numbers of rows in my SQL query, but the code returns all rows in database:
Code: $this->db->select('field1, field2, field2, field2');
$this->db->like('field2',$this->input->post('post_field2'));
$data['records'] = $this->db->get('table');
$rows = $this->db->get('table')->num_rows();
It return all rows in my database!
Please help me!
Sorry my bad english!
[eluser]InsiteFX[/eluser]
Try this.
Code: $this->db->select('field1, field2, field2, field2');
$this->db->like('field2',$this->input->post('post_field2'));
$data['records'] = $this->db->get('table');
$rows = $this->db->count_all('table');
InsiteFX
[eluser]Matthieu Fauveau[/eluser]
This would be the right way of doing this :
Code: $this->db->select('field1, field2, field2, field2');
$this->db->like('field2',$this->input->post('post_field2'));
$query = $this->db->get('table');
$rows = $query->num_rows();
if ($rows > 0) {
$data['records'] = $query->result();
}
else {
$data['records'] = FALSE;
}
[eluser]Ablankzin[/eluser]
I try to get the number of rows to make pagination, but pagination doesn't run my code is:
Code: $config['base_url'] = $this->config->item('base_url').'/index.php/login/search/';
$config['total_rows'] = $this->db->count_all('table');
$config['per_page'] = 20;
$config['num_links'] = 2;
$this->db->select('field1, field2, field3, field4');
$this->db->like('filed1',$this->input->post('field1_post'));
$data['records'] = $this->db->get('table');
but sow 15 pagination links and not show the next result if click in pagination links!
[eluser]pickupman[/eluser]
This should get you going.
Code: //This should probably be in the model
$this->db->select('field1, field2, field3, field4');
$this->db->like('filed1',$this->input->post('field1_post'));
$query = $this->db->get('table'); //Get all matching rows
$config['base_url'] = site_url('/login/search/'); //easier way to create your links
$config['total_rows'] = $query->num_rows();
$config['per_page'] = 20;
$config['num_links'] = 2;
//This should probably be in a model
$this->db->select('field1, field2, field3, field4');
$this->db->like('filed1',$this->input->post('field1_post'));
$this->db->limit($config['per_page'],$this->uri->segment(3,0)); // you need a limit,offset for pagination
$query = $this->db->get('table');
$data['records'] = FALSE; //shorter syntax to set a default records variable
if($query->num_rows() > 0)
$data['records'] = $query->result(); //save pagination result from DB
//rest of your controller goes here
[eluser]Ablankzin[/eluser]
This code run, but if i clik in number two in pagination it show the page with all records in my database, and this code generate 200+ links of pagination, because my table have more than 5300 records.
[eluser]pickupman[/eluser]
That many pagination links would make since. 5300 / 20 per page = 265 links. You can either
1.) Change $config['per_page'] to a number greater than 20.
2.) Add $config['num_links'] to the desired number of links to show before and after the current page
Also please try adding after the first DB call:
Code: $query->free_result();
|