Separating large amounts of data into pages?

C++, C#, Java, PHP, ect...
Post Reply
User avatar
MAruz
Posts: 117
Joined: Fri Nov 20, 2009 12:31 pm

Separating large amounts of data into pages?

Post 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?
PHP, Java, JavaScript, HTML, CSS, XML, MySQL / Oracle
Photoshop, Illustrator
www.cuddly-zombie.com
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: Separating large amounts of data into pages?

Post 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.
New Site Coming Soon! Stay tuned :D
Falken
Posts: 438
Joined: Fri May 08, 2009 8:03 pm

Re: Separating large amounts of data into pages?

Post by Falken »

Another good tutorial for php paging, http://www.php-mysql-tutorial.com/wikis ... g-php.aspx if you don't like videos
--- Game Database ---
For all your game information needs!
Visit us at: http://www.gamedatabase.eu today!
User avatar
MAruz
Posts: 117
Joined: Fri Nov 20, 2009 12:31 pm

Re: Separating large amounts of data into pages?

Post 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 :)
PHP, Java, JavaScript, HTML, CSS, XML, MySQL / Oracle
Photoshop, Illustrator
www.cuddly-zombie.com
User avatar
MAruz
Posts: 117
Joined: Fri Nov 20, 2009 12:31 pm

Re: Separating large amounts of data into pages?

Post by MAruz »

Worked wonders =D Thanks again.
PHP, Java, JavaScript, HTML, CSS, XML, MySQL / Oracle
Photoshop, Illustrator
www.cuddly-zombie.com
Post Reply

Return to “Coding”