Display in table + pagination

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
leyen
Forum Newbie
Posts: 5
Joined: Sat Mar 25, 2006 4:45 am

Display in table + pagination

Post by leyen »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm working on a project that involves displaying images of items on a table with four columns.

Right now, it's in a four column table and i can only manage to insert the rowbreak "</tr><tr>" tag like so:

(Note: this is only a small section of the entire script)

Code: Select all

for ($er = 0; $er < mysql_num_rows($result); $er++)
{
	$row = mysql_fetch_array($result);

	$sql2 = "SELECT `name`, `desc` FROM `*_*_*_items` WHERE `item_id` =". $row['item_id'];

	if ( !($result2 = $db->sql_query($sql2)) )
	{
		message_die(GENERAL_MESSAGE, 'Fatal Error Getting Item List');
	}

	$row2 = mysql_fetch_array($result2);

	if ($er == 3) {
	$trinsert = '</tr><tr>';
	} else if ($er == 7) {
	$trinsert = '</tr><tr>';
	} else if ($er == 11) {
	$trinsert = '</tr><tr>';
	} else if ($er == 15) {
	$trinsert = '</tr><tr>';
	} else if ($er == 19) {
	$trinsert = '</tr><tr>';
	} else if ($er == 23) {
	$trinsert = '</tr><tr>';
	} else if ($er == 27) {
	$trinsert = '</tr><tr>';
	} else if ($er == 31) {
	$trinsert = '</tr><tr>';
	} else if ($er == 35) {
	$trinsert = '</tr><tr>';
	} else if ($er == 39) {
	$trinsert = '</tr><tr>';
	} else if ($er == 43) {
	$trinsert = '</tr><tr>';
	} else if ($er == 47) {
	$trinsert = '</tr><tr>';
	} else if ($er == 51) {
	$trinsert = '</tr><tr>';
	} else if ($er == 55) {
	$trinsert = '</tr><tr>';
	} else {
	$trinsert = '';
	}	
	
	$items = '<td align=center width="25%"><img src="shop/images/' . $row2['name'] . '.png" width=50 height=50 alt="' . $row2['desc'] . '"  border=1></a><br><span class="gensmall">' . $row2['name'] . '</span></td>' . $trinsert;

	$template->assign_block_vars('item_listrow', array(
		'ITEMROW' => $items)
	);
}
You may have guessed it by now. This is a script designed to be used in a PhpBB forum. As you can see, a bug in the script exists when the total number of items exceed 60 items (NOTE: Count starts from 0, not 1. Hence when $er=59, there are 60 items). The table will shrink, while the last row increases in columns for every extra item.

How can I fix this?

I am also thinking of paging it such that there is only 60 items available per-page. How can i do this?


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
jrd
Forum Commoner
Posts: 53
Joined: Tue Mar 14, 2006 1:30 am

Post by jrd »

You sure have a lot of else if. Have thought about using a loop? :)
try this with your current code?

Code: Select all

if ($er <4) { 
   $trinsert = '</tr><tr>';
   } else if ($er <8) {
   .....
   } else if ($er < 56) {
   $trinsert = '</tr><tr>';
   } else if ($er < 60) {
   $trinsert = '</tr>';
   } else {
   $trinsert = '';
   }

PS : http://www.plus2net.com/php_tutorial/php_paging_adv.php has a good tutorial on paging, and rather easy to incorporate into existing code.



.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Useful Posts wrote:• Multi-column formatted output: PHP & MySQL formatting problem
• Multi-column formatted output 2: Create Member Directory with Alphabetical Listings
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

feyd wrote:
Useful Posts wrote:• Multi-column formatted output: PHP & MySQL formatting problem
• Multi-column formatted output 2: Create Member Directory with Alphabetical Listings
Seems like were quoting useful posts far too often.. I don't think many people know about this ;)
Post Reply