Re: Pagination & Filtering Conditions

2008-12-03 Thread Juan Sierra

I post here the way I solved in case someone found it useful as well.
I was reading several of the solutions available on the group but none
of them was working 100% for me, so I tried to figure it out the best
I could:

in the controller:

class ExpensesController extends AppController
{
...
  var $paginate = array(
 'limit' => 15,
 'order' => array(
 'Expense.stamp' => 'asc'
 )
 );
...
function index ()
{
  //set current month and year by default
  $month = date ("m");
  $year = date ("Y");

  //do we receive something from the form?
  if (
 !empty ($this->params['data']['filter']['month']['month'])
&&
 !empty ($this->params['data']['filter']['year']['year']))
 {
// yes, we forget about paging stuff
$month = $this->params['data']['filter']['month']
['month'];
$year = $this->params['data']['filter']['year']
['year'];
 }
 else
 {
//no, then maybe we are paging
if (
!empty ($this->params ['named']['filter.month']) &&
!empty ($this->params ['named']['filter.year']))
{
   $month = $this->params ['named']['filter.month'];
   $year = $this->params ['named']
['filter.year'];
}
 }

  $this->params['pass']['filter.month'] = $month;
  $this->params['pass']['filter.year'] = $year;

  $data = $this->paginate('Expense', "Expense.stamp LIKE '%$year-
$month%'");// AND Expense.stamp LIKE '%-$month-%'");
  $this->set(compact('data'));
}
...
}

in the view:

create('Expense', array('type' => 'post', 'action' =>
'index')); ?>
month ("filter.month", $this->params ["pass"]
["filter.month"], array (), false); ?>
year ("filter.year", 2000, 2020, $this->params ["pass"]
["filter.year"], array (), false); ?>
end('View'); ?>

...

params['pass']['page']); //read it somewhere in
this group, it was useful ?>
Go to Page: numbers(array('url' => $this->params
['pass'])); //it's important to pass this params.pass?>

If you see something I'm doing badly wrong or unnecesary please
advise. thanks

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Pagination & Filtering Conditions

2008-11-27 Thread Juan Sierra

Hi there,

I'm trying to do a very simple thing, I'm sure there is something
important missing - I just started a few days ago with CakePHP.

I want to display an expenses table with pagination and two controls
for filtering the year and month.

The filtering works fine, the pagination as well, but no together.

let's say I apply a date filter in the query, then the view displays
correctly the results, with the correct page numbers (Page 1 | 2) etc.
then I click on "Page 2" and the filter is not applied anymore showing
the whole table.

This is my controller:

expenses_controller.php
   function index ()
   {
  $this->pageTitle = 'Your company Expenses';

  $month = $this->data['Expense']['month'];
  $year = $this->data['Expense']['year'];

  $data = $this->paginate('Expense', array ("Expense.stamp LIKE '%
$year-$month%'"));

  $this->set(compact('data'));
   }

And this is my view:

index.ctp
...

create('Expense', array('type' => 'post', 'action' =>
'index')); ?>
month ("", $this->data ["Expense"]["month"], array (),
false); ?>
year ("", 2000, 2020, $this->data ["Expense"]["year"],
array (), false); ?>
end('View'); ?>
...
(iterate results)
...
Go to Page: numbers(); ?>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---