Page 1 of 1

Problem with format :(

Posted: Tue Oct 28, 2003 11:28 am
by nick2
This might not be a big deal but its getting annoying...

ok I have a user panel where you can upload images and delete.. when you upload all the images display in a row going straight down..

I would like for the images to be like:

# # # #
# # # #

not:

#
#
#
#

I've been using a table hoping that it would do it that way automaticly but apprently it doesn't help.. I tried lots of stuff but failed.. maybe you could take a look?

Code: Select all

<? $dir = $_COOKIE[user];
$dh = opendir($dir); // open it for reading 
while (($file = readdir($dh)) !== false) { // loop files 
if (($file != ".") and ($file != "..") and ($file != "_vti_cnf")) {
        echo "<center><table border="0"><tr><td>
<p><a href="http://www.slices.net/$dir/$file"><img height="50" width="50" border="0" src="".$dir."/".$file.""></p>
<center><b>$file</b></center></td></tr></table>";
     } 
}
?>

Posted: Tue Oct 28, 2003 11:44 am
by scorphus
Try to print the <center>, <table> and one <tr> before the while loop. Then specify a number of columns (say 4 columns) and initialize a counter.

Inside the while loop print one <td>data, data, data</td>, implement the counter and add an if with the condition $counter % $colsNumber == 0 ($a % $b, % is the modulus operator: remainder of $a divided by $b).

When the condition equals 0 then print </tr><tr>. After the loop you close the last </tr>, the </table> and the </center>.

Try it. If you can't do it I'll post the code for ya! But try it first!

Thanks,
Scorphus.

Posted: Tue Oct 28, 2003 12:01 pm
by nick2
I really tried to understand i'm no expert sorry this is whta I got so far..

Code: Select all

<? $dir = $_COOKIE[user]; 
$dh = opendir($dir); // open it for reading 
#
print("<center><table border="0"> <tr>");
#
$counter = "5";
#
while (($file = readdir($dh)) !== false) { // loop files 
if (($file != ".") and ($file != "..") and ($file != "_vti_cnf")) { 
        echo "<td> 
<p><a href="http://www.slices.net/$dir/$file"><img height="50" width="50" border="0" src="".$dir."/".$file.""></p> 
<center><b>$file</b></center></td>"; 
If //lost here 
     } 
} 
?>

Posted: Tue Oct 28, 2003 12:32 pm
by scorphus
Good! Nice to see you tried it and got that close! Let me introduce you a simple example:

Code: Select all

<?php
$foo = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
$colNum = 4; // Set the number of columns
$count = 1; // Initialize the counter
echo "<table border="0">\n<tr>\n"; // start the table and the first row
foreach ($foo as $value) { // Loop in the array
	echo "\t<td>$value</td>\n"; // Print the data
	// Now here is the point! Each 4 times ($count % $colNum == 0 (this 4 is specified by $colNum))
	// we want PHP to start a new row (but only if it has not reached the array's end yet ($count != count($foo))):
	if ($count % $colNum == 0 && $count != count($foo))
		echo "</tr>\n<tr>\n"; // Then close the last and open a new one
	$count++;
}
echo "</tr>\n</table>";
?>
See the output:

Code: Select all

&lt;table border="0"&gt;
&lt;tr&gt;
	&lt;td&gt;1&lt;/td&gt;
	&lt;td&gt;2&lt;/td&gt;
	&lt;td&gt;3&lt;/td&gt;
	&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;5&lt;/td&gt;
	&lt;td&gt;6&lt;/td&gt;
	&lt;td&gt;7&lt;/td&gt;
	&lt;td&gt;8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
	&lt;td&gt;9&lt;/td&gt;
	&lt;td&gt;10&lt;/td&gt;
	&lt;td&gt;11&lt;/td&gt;
	&lt;td&gt;12&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
Ok! Now, don't go further my post until you try to solve your script again, based on the script I post above. Below is your script with modifications.

The % operator is a very nice trick. Take alook to the Operators chapter of the PHP Manual




Your code:

Code: Select all

<?php
$dir = '.';//$_COOKIE[user]; //  changed to run on my server...
$dh = opendir($dir); // open it for reading
echo "<div align="center"><table border="0"><tr>"; // Start printing the table (notice the <div> tag, since <center> is deprecated)
$colNum = 4; // Set the number of columns
$counter = 1; // Initialize the counter
while (($file = readdir($dh)) !== false) { // loop files
	if (($file != ".") and ($file != "..") and ($file != "_vti_cnf")) {
		echo "<td><p><a href="http://www.slices.net/$dir/$file"><img height="50" width="50" border="0" src="".$dir."/".$file.""></p><center><b>$file</b></center></td>\n";
		if ($counter % $colNum == 0) // Each 4 time this condition is true (you can change the value of $colNum to change it)
			echo "</tr><tr>\n"; // then we start a new row
		$counter++; // and keep incrementing the counter
	}
}
echo "</tr></table></div>"; // then close the tr, the table and the div.
?>
Have a nice learning!

Regards,
Scorphus.