tags:

views:

44

answers:

2

I'm using this code to populate a Table:

<style type="text/css">
table, td
{
    border-color: #600;
    border-style: solid;
}

table
{
    border-width: 0 0 1px 1px;
    border-spacing: 0;
    border-collapse: collapse;
}

td
{
    margin: 0;
    padding: 4px;
    border-width: 1px 1px 0 0;
    background-color: #FFC;
}
</style>

<table>
<tr>
<th>Files</th>
</tr>
<?php
foreach(new DirectoryIterator('/home/nathanpc/public_html') as $directoryItem) {
    if($directoryItem->isFile()) {
        printf('<td><tr><a href="/%1$s">%1$s</a></tr></td>', $directoryItem->getBasename());
    }
} ?>
</table>

But when I try it, what I got was values outside the table, and all disorganized. Here it is on the server: http://surl.x10.mx/list.php

+5  A: 

Should be instead:

printf('<tr><td><a href="/%1$s">%1$s</a></td></tr>', $directoryItem->getBasename());

tr means "table row", these should encapsulate the tds, not the other way around.

Artefacto
Sorry, this is what midnight coding does. I've just mixed up the things.
Nathan Campos
+2  A: 

You Don't Want to Use a Table there!

You named your file 'list.php', did you? So, why not actually use a list?

printf('<li><a href="/%1$s">%1$s</a></li>', $directoryItem->getBasename());

Apart from that being shorter, it is semantic correct - which your table isn't.

nikic
This is tabular data and perfectly fine in a table. Enough with the anti-table hysteria. I agree though that a `<ul>` is more beautiful and shorter - as long as there's only one column.
Unicron
@Unicron well, there **is** only one column :)
Gordon
@Gordon true :) but there could easily be more, for example adding a date or file size column or something.
Unicron