0

Good morning everyone. I have the following method which creates me some trouble. Since i have 19 products in that category it return me:

Notice: Undefined variable: pages.

I checked to see from where the problem comes and it's because of $total_pages which returns me 0.95. How could i fix this problem and how could i make it when it reaches at the end of pages to stop because for the moment i have 2 - 3 pages that are empty with no products on them.

public function getTotalProductsByCategory($page_number, $categoryId) {
    $this->database->query('SELECT COUNT(ProductId) FROM products WHERE CategoryId = :categoryId');
    $this->database->bind(':categoryId', $categoryId);
    $count = $this->database->resultSet();

    $total_pages = ceil($count['0']['COUNT(ProductId)'] - 1) / 20;
    for($i = max(1, $page_number - 5); $i <= min($page_number + 5, $total_pages); $i++) {
        $pages[] = $i;
    }
    return $pages;
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Bogdan
  • 693
  • 7
  • 26

1 Answers1

0

Define $pages as an array before using indexing syntax to assign to it:

$pages = array()

Do this outside of the loop.

Brian Warshaw
  • 22,657
  • 9
  • 53
  • 72