문제

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?
  */
}
도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top