Search code examples
phpcakephpcakephp-2.0

How can I find the range of a field covered by each page of a paginated set?


I have a model Task(id, start_date, end_date, description). I use Paginator like

    $this->Paginator->settings = array(
        'Task'=>array(
            'contain'=>$contain,
            'limit'=> $limit,
            'conditions'=>$conditions,
            'order'=>'Task.start_date ASC',
            'page'=> $page,
    ));

What I'm after is to be able to know the range of start_date covered by each page of the paged set. Instead of page numbers (i.e. in view generated by $this->Paginator->numbers()) I'd like to create links like "2 weeks ago" and "Today" that jump to the page containing the first Task with start_date > NOW()-14Days, for example.

I fully understand I could alter my $conditions and set a range on the start_date, but I want the whole set.

Open to other ideas on how to achieve the same result, or any pointers in the right direction.


Solution

  • I ended up running my search twice, once paginated, once not. I then went through the unpaged results

    $pgStarts = array();    
    $pgCounter = 1;
    
    foreach($tasks as $k => $task){
        if(($k % $limit) == 0){
            $pgStarts[$pgCounter] = $task['Task']['start_time'];
            $pgCounter++;
        }
    }
    

    Once I had the array of start dates for each page I could easily change the way the paging numbers were labeled. I was even able to break down the key event day by hour (most tasks are on this day).

    If today were Feb 2. Also, vertical align in Twitter Bootstrap is killing me

    I'm happy with the result, as I think it gives the paging more context. I'll continue to look for a better way of doing it, but this will do for now.