Page 1 of 1

Separating large amounts of data into pages?

Posted: Sat Feb 13, 2010 11:28 am
by MAruz
Basically I have a table full of NPCs that the player can hire for my racing game. Right now, every single entry in that table is printed on one page.

I've been trying to work out how to add a "X entries per page" feature, where the number X could be changed by the user to suit his own needs.
As it is now, I have a while loop that runs through an array that contains all NPCs, and prints to a table.

Ignore the NPC names, I've not implemented a way of generating different names yet :p
npcs.jpg
The problem here is that I need page one to show the 25 first entries in the array on page one, 26-50 on page 2, and so on until last item is shown.

I've fiddled some with the code layout shown below, which enables me to print only the first 25 entries, but the problem starts with the "next page", where I need to print entries 26 through 50 etc.

Code: Select all

$npc_data = getAllNPCs();
$counter = 0;
$row_limit = 25;
while($rows = mysql_fetch_array($npc_data)) {
	// set up local variables to fetch current array data
	$counter++;

	// Print fetched data to a HTML table

	if($counter % $row_limit == 0) {
		// stop printing, we've reached the row limit
	}
}
Anyone got any good ways of doing what I want?

Re: Separating large amounts of data into pages?

Posted: Sat Feb 13, 2010 12:57 pm
by Torniquet
you want pagination :)

see part 1 http://www.youtube.com/watch?v=wC0uc_TkdR0

and part 2 http://www.youtube.com/watch?v=kwACXikD ... re=related


its real easy to do.

Re: Separating large amounts of data into pages?

Posted: Sat Feb 13, 2010 1:41 pm
by Falken
Another good tutorial for php paging, http://www.php-mysql-tutorial.com/wikis ... g-php.aspx if you don't like videos

Re: Separating large amounts of data into pages?

Posted: Sat Feb 13, 2010 1:51 pm
by MAruz
Aha, never heared that expression before. If I had I probably would have found some ways of doing this :p my searches didn't turn up anything like this.
Thanks for the help :)

Re: Separating large amounts of data into pages?

Posted: Sat Feb 13, 2010 3:36 pm
by MAruz
Worked wonders =D Thanks again.