Jump to content

Class not returning variable values


Go to solution Solved by AdRock,

Recommended Posts

I have converted an old pagination function I've had for years to a class as I've recently started to learn OOP

 

The problem is that it doesn't display any anything except the <ul></ul>

 

 

I've tried returning a variable but it comes back empty.

 

I know it's something to do with my variables but I can't see what

 


class Pagination {
 
private $total_pages;
private $page;
private $webpage;
 
public function __construct() {
 
$this->total_pages;
$this->page;
$this->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($total_pages, $page, $webpage) { 
 
// 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 $this->page;
}
}
 
$paging = new Pagination();
 
var_dump( $paging->pagination_one(3, 1, 'news'));
<?php
 
class Pagination {
 
private $total_pages;
private $page;
private $webpage;
 
public function __construct() {
 
$this->total_pages;
$this->page;
$this->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($total_pages, $page, $webpage) { 
 
// 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 $this->page;
}
}
 
$paging = new Pagination();
 
var_dump( $paging->pagination_one(3, 1, 'news'));

 

 

 
 

 

Edited by AdRock
Link to comment
https://forums.phpfreaks.com/topic/282278-class-not-returning-variable-values/
Share on other sites

  • Solution

Sorted it.  I had to remove the private variables and change the constructor to this

 

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


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

Uhm no, you very much did *not* have to remove the private properties, they must be in there  because those are what you are filling in the construnctor.

 

I would also suggest that you cut the single method up into managable (and testable) submethods (private methods) that do a single and simple task each, like calculate how many links to display, calculate what the next and previous linkst are etc. The whol business the the $h should never be where it is now, it makes the code difficult to follow and therefore bug-sensitive.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.