Fedora Linux Support Community & Resources Center
  #1  
Old 27th November 2006, 01:09 AM
cypher543's Avatar
cypher543 Offline
Registered User
 
Join Date: Dec 2005
Age: 22
Posts: 622
PHP and recursive row creation

I'm working on the website for my freelance design services, and I'm at the point of developing a portfolio page. The client images and details are stored in a SQLite database. I would like to recursively split the items returned from a query into rows of 5 using a table. The code I have for the moment is:
Code:
$result = $db->query("SELECT * FROM clients ORDER BY id DESC");
$clients = $result->fetchAll();

echo "<table width='100%' cellspacing='3'>\n";
$column = 1;
foreach ($clients as $client) {
	if ($column == 1) {
		echo "\t<tr>\n";
	}
	$column++;
	echo "\t\t<td><a href='client.php?id=" . $client['id'] . "' class='lbOn'><img src='clients/" . $client['imgname'] . ".jpg'></a></td>\n";
	if ($column == 5) {
		echo "\t</tr>\n";
		$column = 1;
	}
}
echo "</table>";
This works good, except for one thing... if the last row contains less than 5 images, then no </tr> is added, making the page invalid. Normally, this wouldn't bother me, but since this is my portfolio, I would like to make it validate. I figured I could determine at the beginning if the number of rows returned will create an even table (5 items on every row), then if it won't (less than 5 on the last row), it adds the </tr> to end automatically. But, I have no idea how to figure out if the number of rows returned will create exactly 5 items on every row.

Any ideas?
__________________
Registered Linux user #423071
Registered Linux machine #330669
Reply With Quote
  #2  
Old 21st December 2006, 06:20 AM
MicahCarrick's Avatar
MicahCarrick Offline
Registered User
 
Join Date: Oct 2004
Posts: 119
What I do, is find out how many total rows there will be (mysql_num_rows using MySQL or do a COUNT(*) as a separate query). Then, use the modulus operator to determine how many extra TDs you will need. $extra = ($total_rows % 5). Then have a 2nd loop at the end which goes through each $extra and output "<td>&nbsp;</td>"...

Code:
// print extra table cells
for ($i = 0;$i < ($total_rows % 5); $i++)  
{ 
    echo "<td>&nbsp;</td>"; 
}
__________________
Micah Carrick
www.micahcarrick.com
Reply With Quote
Reply

Tags
creation, php, recursive, row

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux and recursive algorithms? voltrem Fedora Focus 2 20th October 2008 03:36 PM
Recursive FTP DanSandberg Servers & Networking 8 17th October 2008 06:10 PM
chmod --recursive kamran_pro Using Fedora 5 18th August 2006 04:10 AM
RPM recursive dependencies!!!! nemesys Using Fedora 6 15th December 2005 03:58 PM


Current GMT-time: 04:34 (Wednesday, 19-06-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat