Question

Is there a way to open directly the page in which a record appears?

Example:

function index($id){
  $this->paginate = array(
    'limit'=>12,
    'page' => ????
  );
  $items = $this->Item->paginate();
  /**
    How can i calculate the page number, by the $id?
  */
}
Was it helpful?

Solution

If you got the ID, and you are auto_incrementing the ID in your database, you can count row before this ID:

$nbRow = $this->Item->find('count', array('conditions' => array('id <=' => $id))) ;

Then to find the page, you just have to divide (int division) by the number of item per page:

$page = ceil($nbRow / 12) ; // 12 or whatever item per page you have
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top