0

I'm new to OOP. I created this class called Site that is extended but many other classes with common queries and excutions. This particular class "Pagination" , I need its methods to be accessed from other instances and access to data sent to it "internally". It's probably bad written but I'd like some help.

<?php

    class Site {
        public $lang;
        public $user_agent;
        public $user_lang;
        public $url;
        public $bots;

        public $total_items;
        public $itemsPerPage;
        public $page;

 class Products extends Site {      
            function getProducts($last = false,  $id = false, $cat = false, $active_only = false, $lang_pref = false, $itemsPerPage = false, $page =false){

            //code

            //prepare paging 
            if(!empty($itemsPerPage)){

                //count total product
                $count = mysql_query(
                "SELECT COUNT(*)  
                FROM products_list
                 ".@$cat_to_choose."
                 ".@$lang."
                ")
                or die(mysql_error());

                $count = mysql_fetch_row($count);
                $total_items = $count[0];

                // set vars for Pagination class
                $this->total_items = $total_items;
                $this->itemsPerPage = $itemsPerPage;
                $this->page = mysql_real_escape_string($page); 

                //send data to class Pagination
                $pagination = new  Pagination();
                $start = $pagination->createPagination();

                $limit = "LIMIT ".$start.", ".$itemsPerPage;

            }

                      //code
        }




            //other classes

    class Pagination extends Site {


         function createPagination(){

            // get total pages by dividing 
            $this->total_pages = $this->total_items/$this->itemsPerPage;

            //round to highest integer
            $this->total_pages= ceil($this->total_pages);

            switch($this->page){
                case "0":
                case null:
                case "1":
                    $start = 0;
                    break;
                default :
                    $start = ($this->page-1)*($this->itemsPerPage);

                    break;

            }

            return $start;

        }

        public function htmlPagination(){
            if($this->page == 0){
                $this->page = 1;
            }
            $pagination = array(
                    "total_pages" => $this->total_pages,
                    "current_page" => $this->page,
                    "total_items" => $this->total_items
            );

            return $pagination;

        }

    }
    }

PHP CODE

$products_object= new Products();
$products = $products_object->getProducts(false,false,$cat, true, $site->lang, $itemsperpage, @$_GET["pag"]);

Once I did this, how do I access htmlPagination with the data processed in the Products instance?

1 Answer 1

1

You could set the pagination as a field of the products object and retrieve it with a get method or by defining it as public and reading it directly.

In products:

class Products 
{
    ...
    private $pagination;

    public function getProducts(...)
    {
        ...
        $this->pagination = new Pagination();
        ...
    }

    public function getPagination()
    {
        return $this->pagination;
    }
}

Then later:

$product->getPagination()->htmlPagination();

to retrieve the html pagination.

Sign up to request clarification or add additional context in comments.

2 Comments

the problem is that I use the Pagination methods for other classes too
each of them could implement such a method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.