Jump to content

Array to horizontal and vertical loop table


hori76

Recommended Posts

Hi, I'm trying to make a table with an array of filenames I get from a folder on my webserver with opendir()

I already got it so far that the files show in an alphabetical order, but now I want it to show in a horizontal and vertical loop in an html table. Now all the data shows in one line underneath eachother. I want a horizontal line and after that it has to jump to the next line.

Now my code is this:
[code]
<?php
if ($dir = @opendir('images/cars/' . $_GET['car_folder'] . ''))
{
while (($file = readdir($dir)) !== false)
{
if($file != ".." && $file != ".")
{
$filelist[] = $file;
}
}
closedir($dir);
}
?>
<?php
asort($filelist);
while (list ($key, $val) = each ($filelist)) {
echo "<table width=\"100px\" class=\"tekst2\"><tr><td>";
echo "<a href=\"images/cars/";
echo $_GET['car_folder'];
echo "/$val\"><img src=\"images/cars/";
echo $_GET['car_folder'];
echo "/$val\" width=\"100\" height=\"75\" border=\"0\"></a></td></tr></table>";
} ?>
[/code]

Can someone help me with this, I tried a couple of scripts but it's not always the right way. I'm not so good at coding so...
You do realize that you were creating a table for every record?! Bad design. Anyway, here you go:

[code]<?php
asort($filelist);

$maxColumns = 10;
$column = 0;

//open the table
echo "<table width=\"100px\" class=\"tekst2\">";

while (list ($key, $val) = each ($filelist)) {

//Open new row
if ($column==0) { echo "<tr>"; }

$column++;
echo "<td><a href=\"images/cars/".$_GET['car_folder']."/$val\">";
echo "<img src=\"images/cars/" . $_GET['car_folder'];
echo "/$val\" width=\"100\" height=\"75\" border=\"0\"></a></td>";

//close the row
if ($column==$maxColumns) {
    echo "</tr>";
    $column = 0;
}
}

//Add remaining table cells and close row if needed
if ($column!=0) {
for ($column; $column<$maxColumns; $column++) {
    echo "<td>&nbsp;</td>";
}
echo "</tr>";
}

//close the table
echo "</table>";
?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.