AdRock AdRock - 16 days ago 10
PHP Question

PHP return an pagination object and displaying the links when called

I have this pagination class which I converted from a normal procedural function to a class since I started learning OOP. In the old procedural way, the function worked fine but I can't get this new class version to display on the screen

class Pagination {
public function __construct() {
}

/**
* This function is called whenever the there are several records to be displayed in the table
* This saves the page extending further down the page creating a long list of results
* when all the results can be spread across multiple pages
*/
public function pagination_one($total_pages, $page, $webpage) {

// Maximum number of links per page. If exceeded, google style pagination is generated
$max_links = 6;
$h=1;
if($page>$max_links) {

$h=(($h+$page)-$max_links);
}
if($page>=1) {
$max_links = $max_links+($page-1);
}
if($max_links>$total_pages) {
$max_links=$total_pages+1;
}
echo '<div class="page_numbers">
<ul>';

if($page>"1") {
echo '<li class="current"><a href="/'.$webpage.'/1">First</a></li>
<li class="current"><a href="/'.$webpage.'/'.($page-1).'">Prev</a></li> ';
}

if($total_pages!=1) {
for ($i=$h;$i<$max_links;$i++) {
if($i==$page) {
echo '<li><a class="current">'.$i.'</a></li>';
}
else
{
echo '<li><a href="/'.$webpage.'/'.$i.'">'.$i.'</a> </li>';
}
}
}

if(($page >="1")&&($page!=$total_pages)) {

echo '<li class="current"><a href="/'.$webpage.'/'.($page+1).'">Next</a></li>
<li class="current"><a href="/'.$webpage.'/'.$total_pages.'">Last</a></li>';
}
echo '</ul> </div>';
}


and elsewhere in another class I want to create a new instance of that class and pass the method in the return along with some parameters

public function paging() {
if($this->getcount != 0) {
$this->paging = new Pagination();
return $this->paging->pagination_one($this->total_pages, $this->page, 'news');
}
}


When I try I var_dump() it comes up as NULL where I expect to see some pagination on the screen.

What have i got to change to be able to display the pagination? Do I have to created some variables in the Pagination class for $total_pages, $page and $webpage and initialise them in the constructor and remove them from the pagination_one method?

Answer

I fixed it myself by removing the private variables in the class and changing the constructor.

The class now looks like this

class Pagination {

    public function __construct($total_pages, $page, $webpage) {

        $this->total_pages = $total_pages;
        $this->page = $page;
        $this->webpage = $webpage;
    }

    /**
     * This function is called whenever the there are several records to be displayed in the table
     * This saves the page extending further down the page creating a long list of results
     * when all the results can be spread across multiple pages
     */
    public function pagination_one() { 

        // Maximum number of links per page.  If exceeded, google style pagination is generated
        $max_links = 6; 
        $h = 1; 
        if($this->page > $max_links) { 

            $h=(($h + $this->page) - $max_links); 
        } 
        if($this->page >= 1) {

            $max_links = $max_links + ($this->page - 1); 
        } 
        if($max_links > $this->total_pages) {

            $max_links = $this->total_pages + 1; 
        } 

        $paging = '';

        $paging .= '<div class="page_numbers"> 
          <ul>'; 

        if($this->page > "1") { 

            $paging .= '<li class="current"><a href="/'.$this->webpage.'/1">First</a></li> 
                  <li class="current"><a href="/'.$this->webpage.'/'.($this->page - 1).'">Prev</a></li> '; 
        } 

        if($this->total_pages != 1) { 

            for ($i=$h; $i < $max_links; $i++) { 

                if($i == $this->page) { 

                    $paging .= '<li><a class="current">'.$i.'</a></li>'; 
                } 
                else { 

                    $paging .= '<li><a href="/'.$this->webpage.'/'.$i.'">'.$i.'</a> </li>'; 
                } 
            } 
        } 

        if(($this->page >= "1" ) && ($this->page != $this->total_pages)) {

            $paging .= '<li class="current"><a href="/'.$this->webpage.'/'.($this->page + 1).'">Next</a></li> 
                  <li class="current"><a href="/'.$this->webpage.'/'.$this->total_pages.'">Last</a></li>'; 
        } 

        $paging .= '</ul> </div>'; 

        return $paging;
    }
}