Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Maping a directory

Status
Not open for further replies.

iamregistered

Programmer
Oct 3, 2006
27
SE
Greetings all,

I have a directory containing following files
Test.rar
Test.txt
Test.jpg

Body.rar
Body.txt

What I am trying to do is read these files ad them to an array, sort it by name and then printing it onto a table in a smiliar way to this:
Code:
<tr>
<td>test.rar</td>
<td>test.txt</td>
<td>test.jpg</td>
</tr>
<tr>
<td>body.rar</td>
<td>body.txt</td>
<td>null</td>
</tr>

However, I am able to create the arry and sort it. Also I can print out them in correct order, but my problem lies in a correct output to the table.
This is the code I am currently using.
Code:
if( is_dir( $path ) )
{
    $handle = opendir( $path );
    while( $file = readdir( $handle ) )
    {
        if( $file != "." && $file != ".." )
        {
            $file_parts = pathInfos($path.$file);
            /*pathInfos retrievs  filename (file), dir name(root/folder/),basename(file.ext) and extensio(ext) */
            $map[$file_parts['basename']] = $file_parts['filename'];
        }
    }
    closedir( $handle );
}
foreach( $map as $val )
{
    $val = str_replace( "_"," ",$val);
    echo $val."<br>";
}

Thansk for any help!

/Martin

**
En solnedgång här
Med björkens dalande löv
Mörkret närmar sig
**
 
Would something like this work? (I'm not exactly sure how the $map as $val works, but the logic will get you what you need)

Code:
$prev_file = ""
echo "<table><tr>";
foreach( $map as $val )
{
    $val = str_replace( "_"," ",$val);
    if($val[$file_parts['basename']] != $prev_file)
       echo"</tr><tr>"
    echo "<td>$val</td>";
    $prev_file = $val[$file_parts['basename']];
}
</tr></table>

Thats a quick shot at it. Basically the idea is to check the basename becuase in you example the first three times its "test" then when it changes to body It will create a new row for you.

Do you need the "null" values or were they just a place holder? Tables in HTML do not have to be "square". for eg this is a valid HTML table (below):

_____________________
|_____|_______|_____|
|_________|_________|
|___________________|
|__|__|__|__|__|__|_|
 
I think I exaggerated what I was trying to demonstrate. You can use row and col span to specify how many "cells" one "<td>" tag takes up. A better example would look like this...
__________________
| a |____b____|____|
|___|____|____|____|

Cell "a" is 2 rows "tall" whereas "b" is 2 cols wide, and the rest are all one standard size cell.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top