Anyone know what is wrong with my pagination code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
frank_solo
Forum Newbie
Posts: 5
Joined: Wed Mar 09, 2011 4:56 pm

Anyone know what is wrong with my pagination code?

Post by frank_solo »

Ok so before I wanted to sort and I got it to sort on one column which is fine. Now what I'm trying to do is to give it next page or previous page options. I keep getting an error which states, "Fatal error: Call to a member function tabular_output() on a non-object on line 24, which reads $dbase->tabular_output($sql); Can anyone tell me what the problem is? Thanks.

Code: Select all

if ($_POST){

	$county = $_POST['county'];
	$rooms = $_POST['type'];
	$rent = $_POST['rent'];
	$rent_min = $rent - 500;
}
	
	$dbase = mysql_connect ( ' ', ' ', ' ' );
	mysql_select_db ( ' ', $dbase );
	
	$pagesize = 1;
	
	$recordstart = (int) $_GET['recordstart'];
	
	$recordstart = (isset($_GET['recordstart'])) ? $recordstart : 0;
			
	if($county){
		$sql = "SELECT * FROM `apartments` WHERE `county` = '".$county."' AND `rooms` = '".$rooms."' AND `rent` < '".$rent."' AND `rent` > '".$rent_min."' ORDER BY `rent` LIMIT $recordstart, $pagesize";
	}else{
		$sql = "SELECT * FROM `apartments`";
	}
		
		$dbase->tabular_output($sql);
		
		// Retrieve total rows in order to determine whether 'next' link should appear
		
		$result = $dbase->query("SELECT count(rent) AS count FROM `apartments`");
		
		list($totalrows) = $result->fetch_row();
		
	    // Create the 'previous' link
	
	if ($recordstart > 0) {   
	$prev = $recordstart - $pagesize;   
	$url = $_SERVER['PHP_SELF']."?recordstart=$prev";
	      printf("<a href='%s'>Previous Page</a>", $url);
		  }
	// Create the 'next' link
	
	if ($totalrows > ($recordstart + $pagesize)) {
		   $next = $recordstart + $pagesize;   
		   $url = $_SERVER['PHP_SELF']."?recordstart=$next";
		      printf("<a href='%s'>Next Page</a>", $url);
			  }
			  
	//include the class
	
require_once("/home/genesis/php/HTML/Table.php");

//set table attributes
$attributes = array("width"=>"600","border"=>"1", "align"=>"center");

//create the table class
$table = new HTML_Table($attributes);

//build our first row
$contents = array("County", "Town", "Phone Number", "Rooms", "Baths", "Rent");
$attributes = array("bgcolor"=>"#336699", "align"=>"center", "id"=>"results");
$table->addRow($contents, $attributes, "TH");

//loop through and add our data
$attributes = array("bgcolor"=>"#COCOCO", "align"=>"center");

	$res = mysql_query($sql, $dbase);
	
		while($row = mysql_fetch_assoc($res)) {
        $contents = array($row['county'], $row['town'], $row['phone'], $row['rooms'], $row['bath'], $row['rent'],);
		$table->addRow($contents, $attributes);
}
$table->altRowAttributes(1, null, array("class"=>"alt"));
$table->display();
?>
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Anyone know what is wrong with my pagination code?

Post by danwguy »

Where are you defining the function tabular_output() ?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Anyone know what is wrong with my pagination code?

Post by social_experiment »

Before you can use a method from a class you need to instantiate a new object.

Code: Select all

<?php
 $yourObj = new YourObj;
 $yourObj->tabular_output($sql); 
?>
$dbase in this case is a resource link.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply