Jump to content

My OOP pagination functions aren't working... New to OOP!


Foser

Recommended Posts

I want my code to do a pagination.

 

 

OOP file: paging.php

<?php
class Paging
{
var $GETPAGE;
var $max_ammount;
var $limit_db;
var order_db;
function Paging($GETPAGE,$max_ammount,$limit_db,$order_db){

if (!isset($GETPAGE) or $GETPAGE == 1){
$max_ammount;
$min_value = 0;
}

if ($GETPAGE == 2) { 
$max_ammount;
$min_value = $GETPAGE;}

if ($GETPAGE > 2){ 
$max_ammount;
$min_value = $getpage * $max_ammount - $max_ammount;}

$db_limiter = mysql_query("SELECT * FROM {$limit_db} ORDER BY {$order_db} LIMIT $min_value , $max_value");
}
function max_min_value(){
return $max_ammount;
return $min_value
}

function limit_db(){
return $db_limiter;
}
}
?>

 

 

code:

<?php
require('paging.php');
mysql_connect(localhost,root,password);
mysql_select_db(tutorial1);
$page_table = "pagination";
$db_order = "id";
$max_ammount = 4;
$paging = new paging($_GET['page'],$max_ammount,$page_table,$db_order);
$max_min = $paging->max_min_value();
while ($row_info = mysql_fetch_assoc($paging->limit_db())){
echo "ID Number: {$row_info['id']}<br>Name: {$row_info['name']}<br>Age: {$row_info['age']}<br>Referer: {$row_info['referer']}<br>-----------------------------------------------------<br>";
}
?>

error

if ($_GET['page'] > 2){ $getpage = $_GET['page']; $max_value = 2; $min_value = $getpage * 2 - 2;} $query2 = mysql_query("SELECT * FROM pagination ORDER BY id");
Fatal error: Call to undefined method Paging::max_min_value() in C:\WAMP\www\Tutorials\OOP_Testing\paging\index.php on line 9

function max_min_value(){
return $max_ammount;
return $min_value
}

 

A return call is a sign for the function to end.... You can't use two returns like that; however, you could return the variables in an array or with a delimiter.

 

Also, functions in classes, just like not in classes, have their own variable scope...

 

To refer to a class' varialble, you should use the $this syntax.

 

function max_min_value(){
return array($this->max_ammount, $this->min_value);
}

 

That also means that your other methods need some altercations:

 

<?php
class Paging {
var $GETPAGE;
var $max_ammount;
var $limit_db;
var $order_db; //dunno why, but your $ was missing
var $min_value; //added this so you could use it class wide later
var $this->db_limiter; //added for the same reason as min_value
function Paging($GETPAGE,$max_ammount,$limit_db,$order_db){
	//it looks like you're trying to use most of your variables in other methods, so you might as well assign them to class variables.
	$this->GETPAGE = $GETPAGE;
	$this->max_ammount = $max_ammount;
	$this->limit_db = $limit_db;
	$this->order_db = $order_db;
	if (!isset($GETPAGE) or $GETPAGE == 1){
		//$max_ammount; //I'm not quite sure why you have this....
		$this->min_value = 0;
	}

	if ($GETPAGE == 2) {
		$this->min_value = $GETPAGE;
	}

	if ($GETPAGE > 2){
		$this->min_value = $getpage * $max_ammount - $max_ammount;
	}

	$this->db_limiter = mysql_query("SELECT * FROM {$limit_db} ORDER BY {$order_db} LIMIT $min_value , $max_value");
}

function max_min_value(){
	return array($this->max_ammount, $this->min_value);
}

function limit_db(){
	return $this->db_limiter;
}
}
?>

I still seem to have an error, I think im outputing it wrong... Seems to not think that there is a mysql query...

 

error:

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\WAMP\www\Tutorials\OOP_Testing\paging\index.php on line 9

 

 

output file (index.php)

<?php
require('paging.php');
mysql_connect(localhost,root,password);
mysql_select_db(tutorial1);
$page_table = "pagination";
$db_order = "id";
$max_ammount = 4;
$paging_system = new paging($_GET['page'],$max_ammount,$page_table,$db_order);
while ($row_info = mysql_fetch_assoc($paging_system->db_limiter)){
echo "ID Number: {$row_info['id']}<br>Name: {$row_info['name']}<br>Age: {$row_info['age']}<br>Referer: {$row_info['referer']}<br>-----------------------------------------------------<br>";
}
?>

 

 

OOP Page:

<?php
class Paging
{
var $GETPAGE;
var $max_ammount;
var $limit_db;
var $order_db;
function Paging($GETPAGE,$max_ammount,$limit_db,$order_db){

$this->GETPAGE = $GETPAGE;
$this->max_ammount = $max_ammount;
$this->limit_db = $limit_db;
$this->order_db;
if (!isset($GETPAGE) or $GETPAGE == 1){
$this->min_value = 0;
}

if ($GETPAGE == 2) { 
$this->min_value = $GETPAGE;}

if ($GETPAGE > 2){ 
$this->min_value = $getpage * $max_ammount - $max_ammount;}

$this->db_limiter = mysql_query("SELECT * FROM {$limit_db} ORDER BY {$order_db} LIMIT $min_value , $max_value");
}
function max_page_value(){
return $this->max_ammount;
}

function min_page_value(){
return $this->min_value;}
function page_limit_db(){
return $this->db_limiter;
}
}
?>

 

edited!

 

error:

Fatal error: Call to undefined method Paging::db_limiter() in C:\WAMP\www\Tutorials\OOP_Testing\paging\index.php on line 9

 

index.php

 

<?php
require('paging.php');
mysql_connect(localhost,root,password);
mysql_select_db(tutorial1);
$page_table = "pagination";
$db_order = "id";
$max_ammount = 4;
$paging_system = new paging($_GET['page'],$max_ammount,$page_table,$db_order);
while ($row_info = mysql_fetch_assoc($paging_system->db_limiter())){
echo "ID Number: {$row_info['id']}<br>Name: {$row_info['name']}<br>Age: {$row_info['age']}<br>Referer: {$row_info['referer']}<br>-----------------------------------------------------<br>";
}
?>

 

 

OOP CODE:

<?php
class Paging
{
var $GETPAGE;
var $max_ammount;
var $limit_db;
var $order_db;
function Paging($GETPAGE,$max_ammount,$limit_db,$order_db){

$this->GETPAGE = $GETPAGE;
$this->max_ammount = $max_ammount;
$this->limit_db = $limit_db;
$this->order_db;
if (!isset($GETPAGE) or $GETPAGE == 1){
$this->min_value = 0;
}

if ($GETPAGE == 2) { 
$this->min_value = $GETPAGE;}

if ($GETPAGE > 2){ 
$this->min_value = $getpage * $max_ammount - $max_ammount;}

$this->db_limiter = mysql_query("SELECT * FROM {$limit_db} ORDER BY {$order_db} LIMIT $min_value , $max_value");
}
function max_page_value(){
return $this->max_ammount;
}

function min_page_value(){
return $this->min_value;}
function page_limit_db(){
return $this->db_limiter;
}
}
?>

 

 

You have no method called db_limiter(), you have a variable called $db_limiter and a method called page_limit_db() that returns $db_limiter.

 

// using the variable, removed trailing parenthesis.
while ($row_info = mysql_fetch_assoc($paging_system->db_limiter)){

// using the page_limit_db() method
while ($row_info = mysql_fetch_assoc($paging_system->page_limit_db())){

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.