Page 1 of 1

Help with looping through a table

Posted: Wed Apr 11, 2012 11:55 am
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

Re: Help with looping through a table

Posted: Wed Apr 11, 2012 5:09 pm
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?

Re: Help with looping through a table

Posted: Wed Apr 11, 2012 6:21 pm
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.

Re: Help with looping through a table

Posted: Thu Apr 12, 2012 6:54 am
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";
}
?>