TYPOlight Open Source CMS > TYPOlight forum

Switch to german forum

Index > Developer's corner > Pagination in a custom module

chrisT20
User
Avatar
Hi,

I'am writing a custom module, the databasetable contains a lot of data, so I need a pagination. I've build the following code, but it doesn't works very fine.

iconphp:
 
	protected function compile()
	{
		if(!$_GET['seite']) {
			$page = 1;
		} else {
			$page = $_GET['seite'];
		}
 
		$limit = 1;
		echo $min = $limit * $page;
	 	echo $max = ($limit * $page) + 1;
 
		$objData = $this->Database->execute("SELECT * FROM test_table LIMIT ".$min.",".$max);
		$arrData = array();
 
		while ($objData->next())
		{
			$arrData[] = array
			(
			'name' => $objData->name
			);
		}	
		print_r($arrData);
	   return $this->Template->datas = $arrData;
	}
 

The first site works fine, but when I'am calling page 2 or less, the query gives me all datasets from 0 to the $max, but not only the $min to the $max. Does anybody have a solution which is more cleaner then mine?

Regards Chris
17/06/2009 21:41
thyon
User
Avatar
Posts: 1756
Cape Town, South Africa
Just look at the News Reader Module, where it uses the Comments section, this shows you exactly how to use the Pagination Class to "page" your Query and parse the $this->Input->get('page') values and also how to render the page/1/2/3/4/next automatically using the standard templates.
thyon | iMac 24" 3.06GHz, OSX Leopard, Safari, Camino, Coda
Manuals: QuickPoll, FormAuto, EventsAttend, Galleries, Invitations, Catalog, Catalog Ext, Catalog Tutorials, Catalog Multi-language Tutorial
17/06/2009 22:09
chrisT20
User
Avatar
Ok, thx!... But the build in Pagination doens't support more parameters than the page parameter, i think i must fix it myself, by modifying the pagination class. :(
18/06/2009 23:30
lindesbs
User
Avatar
Posts: 631
Oer-Erkenschwick, Germany
Take a look into the Database class TL_ROOT/system/libraries/Database.php

Nearby line 468 you have the functio called "limit"

This will limit your query to your area, and you can use a query like that :

iconphp:
$objData = $this->Database->prepare("SELECT * FROM test_table")->limit($min,$max)->execute();

It´s easier, to let TL Framework do the (damned) database stuff. For an example, take a look inside the comments module. ContentComments.php

Another hint : DON`T USE $_GET
:|
It´s unfiltered stuff, use instead $this->Input->get
ModulProjektverwaltung : http://dev.typolight-forge.org
ICQ: 21816772
Hosting : http://www.ktrion.de
Ich entwickle meine Module in der Freizeit, wer Spenden moechte oder eine Auftragsarbeit hat, bitte melden.
19/06/2009 03:52
chrisT20
User
Avatar
Ok, very great, works fine :) ... yes, the $_GET was only for testing, i never use anything like this without escaping.

Thanks and regards

Chris
19/06/2009 09:53