Welcome Guest, Not a member yet? Register   Sign In
PAGINATING after Webform data parameters entered: Tutorial
#1

[eluser]alvaroeesti[/eluser]

Hi guys,


despite many (lols 3 weeks that I read around), the fact is that during these 3 weeks I had switched into doing something else, java and android stuff, and only after in a better mood returned to the pagination issue).

So to help anyone who may encounter the same difficulties that I did, I have decided to include how I solved my problem about pagination. Once I have it all neat and clear, might upload a video to screencast.

ESCENARIO

We all know about paginating when it is about soaking up the full table, but that is not usually what you want. You want to offer your visitors tailored results according to what they selected, and you want that paginated.

The problem is:

You can do the pagination stuff following the user guide and have the pagelinks displayed, but.., once you click on them. it's a "hasta la vista baby", you have nothing, no results. How come? What happens is the old story about the stateless nature of the web, the parameters POSTED won't hold throughout the pages, you need to put them in sessions. Sure you knew that, but the issue was: how:

THE WAY (or one of the ways...)

You don't want to see repeated code, so I am going to paste exactly what you need and not more:

1. in my MODEL, I have:

a) A query to get the results, (limit and offset included)

b) Another query to get the number of rows that that query outputs considering the parameters the user entered.

Note that you probably don't want to have the 2 queries repeated as, (and it was my case), they can be very complicated and have multiple joins. for that, you may want to check the

SQL_CALC_FOUND_ROWS in your query, followed by SELECT FOUND_ROWS();

but that is not mandatory.

c) Now I have another function which is going to enter me the value of the variable into the session variable: I give you one sample from one of my fields in the Webform, the one that asks for Price From:


Code:
public function pricefrom_handler($pricefrom)
{
  if($pricefrom)
  {
   $this->session->set_userdata('pricefrom', $pricefrom);
   return $pricefrom;
  }
  elseif($this->session->userdata('pricefrom'))
  {
   $pricefrom = $this->session->userdata('pricefrom');
   return $pricefrom;
  }
  else
  {
   $pricefrom ="";
   return $pricefrom;
  }

}

Then, in my CONTROLLER, I have:


Code:
$pricefrom = $this->resultados_search_M1->pricefrom_handler($this->input->get_post('pricefrom', TRUE));

As you see above, all I do is send that query to the function I just showed you in the Model and save the result in the variable $pricefrom.

I could say, that is all you need. But for more explanation I show you how I continued. Basically as normal, as you would normally do whenever you send your query to the model and save the results returned in your array, like this:

Code:
$data = $this->resultados_search_M1->devolverResultadosMapa($object, $paises, $regiones, $address, $transaction_type,
     $pricefrom, $priceto, $areafrom, $areato, $roomsfrom, $roomsto, $ciudades, $situation,
     $soldby, $furniture, $builtyear, $basement, $garage, $garden, $limit, $offset);


And then, also as usual:

Code:
$config['base_url'] = site_url('resultados_search_c/validateData');
   $config['total_rows'] = $total_filas;
   $config['per_page'] = $limit;
   $config['uri_segment'] = 3;
  
   $this->pagination->initialize($config);
   $content['pagelinks'] = $this->pagination->create_links();
  
   # END OF PAGINATION+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  
   $content['object'] = $object;
      
   $this->load->view('resultados_search_V1', $content);

#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++




I had seen other solutions on the web but they were very complicated and gave them up. This one is based on Andy Hawthorne solution, and I think is quite nice.


It might be interesting to know other variants, as, in my case, I really had to repeat that function that I showed you in the model for each variable, and although it was just a copy and paste and replace all, it did not look very elegant, but I could not imagine any way to try to do that by means of looping through an array and replacing the variable names through push and shift, maybe someday I try that, but this works fine.


Regards

A




Theme © iAndrew 2016 - Forum software by © MyBB