hori76 Posted November 28, 2006 Share Posted November 28, 2006 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]<?phpif ($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... Link to comment https://forums.phpfreaks.com/topic/28740-array-to-horizontal-and-vertical-loop-table/ Share on other sites More sharing options...
Psycho Posted November 28, 2006 Share Posted November 28, 2006 You do realize that you were creating a table for every record?! Bad design. Anyway, here you go:[code]<?phpasort($filelist);$maxColumns = 10;$column = 0;//open the tableecho "<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 neededif ($column!=0) { for ($column; $column<$maxColumns; $column++) { echo "<td> </td>"; } echo "</tr>";}//close the tableecho "</table>";?>[/code] Link to comment https://forums.phpfreaks.com/topic/28740-array-to-horizontal-and-vertical-loop-table/#findComment-131575 Share on other sites More sharing options...
hori76 Posted November 29, 2006 Author Share Posted November 29, 2006 Yes, I know, I was messing up the code.Thank you very much! It works like a charm! Link to comment https://forums.phpfreaks.com/topic/28740-array-to-horizontal-and-vertical-loop-table/#findComment-131979 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.