Welcome Guest, Not a member yet? Register   Sign In
Problem with WHERE and pagination
#1

[eluser]yoelrodguez[/eluser]
Hello as I can maintain the value of a WHERE in a pagination. Which disappears when I change of page

my code
Data Table
$this->db->select($sql_query);
$this->db->where($campo, $value);
$q = $this->db->get($from,$per_page,$segment);
$data["customers"]=array();
if ($q->num_rows() > 0)
{
foreach ($q->result() as $row)
{
$data["customers"][$row->id]["id"] = $row->id;
$data["customers"][$row->id]["nombre"] = $row->nombre;
}
}
return $data["customers"];

Pagination:

$this->load->library('pagination');
$config['base_url'] = base_url().'index.php/cinmueble/index';
$config['total_rows'] = $total;
$config['per_page'] = $per_page;
$config['num_links'] = $num_links;
$config['first_link'] = $first_link;
$config['last_link'] = $last_link;
$this->pagination->initialize($config);



#2

[eluser]Otemu[/eluser]
Can you paste your code so we can try to analyse and hopefully provide a solution for your problem
#3

[eluser]yoelrodguez[/eluser]
my code
Data Table
$this->db->select($sql_query);
$this->db->where($campo, $value);
$q = $this->db->get($from,$per_page,$segment);
$data[“customers”]=array();
if ($q->num_rows() > 0)
{
foreach ($q->result() as $row)
{
$data[“customers”][$row->id][“id”] = $row->id;
$data[“customers”][$row->id][“nombre”] = $row->nombre;
}
}
return $data[“customers”];

Pagination:

$this->load->library(‘pagination’);
$config[‘base_url’] = base_url().‘index.php/cinmueble/index’;
$config[‘total_rows’] = $total;
$config[‘per_page’] = $per_page;
$config[‘num_links’] = $num_links;
$config[‘first_link’] = $first_link;
$config[‘last_link’] = $last_link;
$this->pagination->initialize($config);
#4

[eluser]Otemu[/eluser]
You haven't set $total anywhere and have left out a few variables that need to be defined did you forget to include this???
Pagination Class needs to know total number for it to work
#5

[eluser]yoelrodguez[/eluser]
if I have all this is the code

$this->db->select($sql_quey);
$this->db->where($campo, $value);
$q = $this->db->get($from);
$total = $q->num_rows() ;
#6

[eluser]Otemu[/eluser]
duplicate removed
#7

[eluser]Otemu[/eluser]
Hi I give you example code of pagination so you may use it or redefine for your purpose

Controller
Code:
//retrieve total rows
$numrows = $this->M_MyTable->getAllRecords();
$config['base_url'] = base_url().'mySite/';
$config['total_rows'] = $numrows;
$config['per_page'] = '2';
$config['full_tag_open'] = '<p>';
$config['full_tag_close'] = '</p>';

$this->pagination->initialize($config);

//url segment is the actual pagination number which will be used in the model for the offset  
$data['myRecords'] = $this->M_MyTable->getTheRecords($config['per_page'],$this->uri->segment(2));
$this->load->vars($data);
$this->load->view('myViewPage');
Model
Code:
//total number of recordss
function getAllRecords() {
$data = array();
$this->db->select('field1, field2, field3');
$this->db->from('myTable');
$this->db->limit(200);
$Q = $this->db->get();
$total = $Q->num_rows();
$data = $total;
$Q->free_result();
return $data;
}

//retrieve records
function getTheRecords($lmt, $off) {
$data = array();
$this->db->select('field1, field2, field3');
$this->db->from('myTable');
$this->db->limit($lmt);
$this->db->offset($off);
$Q = $this->db->get();
if ($Q-> num_rows() > 0){
foreach ($Q-> result_array() as $row){
  $data[] = $row;
  }
}  
$Q->free_result();
return $data;
}
Views
Code:
foreach($myRecords as $row){
echo $row['field1'].'<br />'.$row['field1'].'<br />'.$row['field1'].'<br />';
}
echo $this->pagination->create_links();

Hope this all makes sense and helps
#8

[eluser]yoelrodguez[/eluser]
Thanks I will try and tell
#9

[eluser]yoelrodguez[/eluser]
Pagination is ok now but when I change the WHERE SELECT page is giving me empty instance

example:

SELECT * FROM table WHERE id = '1 '

To change the page

SELECT * FROM table WHERE id =''
#10

[eluser]Otemu[/eluser]
Sorry I am not sure what you are trying to achieve?? Please explain in more detail.




Theme © iAndrew 2016 - Forum software by © MyBB