Good day! There is a large OOP class: $db .
It has a function:

 public function paginate ($table, $page, $fields = null){ $offset = $this->pageLimit * ($page - 1); $res = $this->withTotalCount()->get ($table, Array ($offset, $this->pageLimit), $fields); $this->totalPages = ceil($this->totalCount / $this->pageLimit); return $res; } 
  1. I get the value from this function $clients=$db->paginate('clients', 1);
  2. But at the same time it is necessary in parallel to pull out the value of totalPages from it. How to do it?
  • $ pages = $ db-> totalPages - ilyaplot
  • It does not work out that way. It is at the very beginning of the class given as public $ totalPages = 0; It is necessary to pull its value from this function. - aiba
  • After executing pagination, the variable will take a different value and will not change until the next call to pagination - ilyaplot
  • I tried. does not display data. - aiba
  • one
    Yes, no, she is public. But in principle, the answer was found below as an array! Although I liked your answer more! Thanks for all the help! :-) - aiba

2 answers 2

You can return an array:

 public function paginate ($table, $page, $fields = null){ $offset = $this->pageLimit * ($page - 1); $res = $this->withTotalCount()->get ($table, Array ($offset, $this->pageLimit), $fields); $this->totalPages = ceil($this->totalCount / $this->pageLimit); return [ "res" => $res, "total_pages" => $this->totalPages ]; } 

Receiving:

 $clients = $db->paginate('clients', 1); $res = $clients['res']; $count = $clients['count]; 

You can return the number of the link:

 public function paginate ($table, $page, $fields = null, &$count){ $offset = $this->pageLimit * ($page - 1); $res = $this->withTotalCount()->get ($table, Array ($offset, $this->pageLimit), $fields); $this->totalPages = ceil($this->totalCount / $this->pageLimit); return $res; } 

Receiving:

 $count = 0; $clients = $db->paginate('clients', 1, null, $count); 
  • Good suggestion about an array! That's just the implementation does not work! public function paginate ($table, $page, $fields = null){ $offset = $this->pageLimit * ($page - 1); $res = $this->withTotalCount()->get ($table, Array ($offset, $this->pageLimit), $fields); $df = $this->totalPages = ceil($this->totalCount / $this->pageLimit); return [ "res" => $res, "last" => $df ]; } public function paginate ($table, $page, $fields = null){ $offset = $this->pageLimit * ($page - 1); $res = $this->withTotalCount()->get ($table, Array ($offset, $this->pageLimit), $fields); $df = $this->totalPages = ceil($this->totalCount / $this->pageLimit); return [ "res" => $res, "last" => $df ]; } Maybe you need to write an array somehow differently? - aiba
  • What version of php ? try replacing return array("res" => $res, "last" => $df); - Stanislav Grotto
  • Everything, earned an array! In the old and new form, it works.
  • It just wasn't possible to merge my requests, because there are calculations between them.
  • Thanks for the help!

    public function paginate ($ table, $ page, $ fields = null) {$ offset = $ this-> pageLimit * ($ page - 1); $ res = $ this-> withTotalCount () -> get ($ table, Array ($ offset, $ this-> pageLimit), $ fields); $ dg = $ this-> totalPages = ceil ($ this-> totalCount / $ this-> pageLimit); return ["res" => $ res, "last" => $ dg]; }

    $ res = $ db-> paginate ('clients', $ pagenum); $ last = $ res ['last'];

    $ res = $ db-> paginate ('clients', $ pagenum); $ clients = $ res ['res'];