Help with looping through a table

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
jleampark
Forum Newbie
Posts: 16
Joined: Wed Jul 27, 2005 6:46 am

Help with looping through a table

Post by jleampark »

I have 9 folders, each having Word documents in them -- anywhere from 2 docs to 15.

I want a script that loops through each folder. Each time, it gets the documents from the folder, formats them with the name of the doc and a hyperlink and them displays them in a table that is at least 3 columns wide. The rows will vary for each folder: 2 documents will have one row, 15 documents will have 5, etc.

I can get everything but the logic for the table. It is driving me batty. :banghead: I either get a bunch of columns and one row or one row and a bunch of columns.

Here is what I have so far (and is this the most efficient way of doing it?):

Code: Select all

<?php

for ($year=2012; $year>=2004; $year--) {
	echo "<table border='1' width='90%'><tr><th class='columnheader' colspan='3'>".$year."</th></tr>\n";

	$file=scandir($year);
	$result=count($file)-2;
	$columns = 3;
	foreach ($file as $key => $value) {
			$split = str_split($value, 2);
			$NoteMonth = $split[0];
			$NoteDay = $split[1];
			if (($NoteMonth == ".")||($NoteMonth == "..")) {
				} else {
						echo "<tr>";
						echo "<td><a href='/econ/epd/chiefnotes/".$year."/".$value."'>".(date("F d", mktime(0,0,0,$NoteMonth,$NoteDay, 0)))."</a></td>";
						echo "</tr>\n";
						}
						
					}
}
echo "</table>\n";
			
?>
Thanks for any and all help!

Joe
xtiano77
Forum Commoner
Posts: 72
Joined: Tue Sep 22, 2009 10:53 am
Location: Texas

Re: Help with looping through a table

Post by xtiano77 »

Basically, you are trying to get the script to read the sub-folders and files inside a folder, then display them in the form of a table[s], each with the name of the folder or folders followed by the files inside each folder. Like a directory, correct?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Help with looping through a table

Post by califdon »

If you want 3 columns and you have a variable number of entries, you have to keep track of what column you're in, and if the next entry would be column 4, you simply insert a "</tr><tr>" to force a new row before you send it to the browser. So you need to have a column counter variable, check it and increment it before you output anything for that item, and reset it to 1 each time it reaches 4.
jleampark
Forum Newbie
Posts: 16
Joined: Wed Jul 27, 2005 6:46 am

Re: Help with looping through a table

Post by jleampark »

I found my answer and am posting it here for two reasons: (1) see if anyone can improve it and (2) maybe it can help someone else.

Code: Select all

<?php
$columns = 4;
$i = 1;
for ($year=2012; $year>=2004; $year--) 
	{
		echo "<table class='notes'><tr><td class='columnheader' colspan='3'>".
		$year."</td></tr>\n";
		if($i == $columns) 
			{
				echo "</tr>\n";
				$i = 1;
			}
		if($i == 1) echo "<tr>";


		$file=scandir($year);
		$result=count($file)-2;

		foreach ($file as $key => $value) 
			{
				$split = str_split($value, 2);
				$NoteMonth = $split[0];
				$NoteDay = $split[1];
				if (($NoteMonth == ".")||($NoteMonth == "..")) 
					{
					} 
					else 
						{
						if($i == $columns) 
							{
								echo "</tr>\n";
								$i = 1;
							}
								if($i == 1) echo "<tr>";
								echo "<td><link info".
								$year."/".$value."'>".
								(date("F d", mktime(0,0,0,$NoteMonth,$NoteDay, 0)))."</link info></td>\n";
								$i++;
					}
			}
					$i = 1;
		
	echo "</table><br />\n";
}
?>
Post Reply