Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array, which started off as an empty array with

$filearray = array(array(array()));

I know how to do it if there were tag IDs set, but I seem to be struggling where I've built on it.

while (($file = readdir($dir)) !== false)
{
    //echo $file ;

    //gets the file date, string needed decoding otherwise throws error. Thanks to stackoverflow!

    $date = @filemtime($file);
    $date = date("F d Y H:i:s.", filemtime(utf8_decode("/home/a1742406/public_html/files/$file")));
    //echo $date. "<br />";

    $size = filesize("/home/a1742406/public_html/files/$file") . ' bytes';

    $filearray = array($file, $date, $size);

    print "<pre>";
    $data = implode(" ", $filearray);
    echo $data;
    //print_r($filearray);
    print "</pre>";
}

My filearray has 3 parts, file, date and size, but I want to print them separately so i can have spacing between them and put them into a table or list, but I can't seem to figure how to separate them to do it, as the above prints it all in one row. Any help would be greatly appreciated to point me in the right direction!

Thanks.

share|improve this question

3 Answers 3

up vote 0 down vote accepted

If you plan on printing them inside the <pre> pre formatted tags, then you could just use \n:

print "<pre>";
$data = implode(" ", $filearray);
echo $data . "\n"; // <-- new line, or if you want `\n\n`, whatever how much space you want between lines
print "</pre>";

If you also want file, date, and size in each separate lines, then you could just glue them with a newline:

print "<pre>";
$data = implode("\n", $filearray);
echo $data . "\n\n";
print "</pre>";

If you plan on presenting them on a table, then just build up the markup like you normally would do:

echo '<table cellpadding="10">';
echo '
    <tr>
        <th>File</th>
        <th>Date</th>
        <th>Size</th>
    </tr>
';

while (($file = readdir($dir)) !== false)
{
    // other codes inside

    echo '<tr>';
        echo "<td>$file</td>";
        echo "<td>$date</td>";
        echo "<td>$size</td>";
    echo '</tr>';

}
echo '</table>';
share|improve this answer
    
Ah the last is exactly what I'm looking for! Question though, how would I do it so that it prints where I've set it in my html? But with no set number of lines? Sorry, I'm still very new to PHP! –  Navvy Dec 1 '14 at 13:09
    
@Navvy im sorry could you paraphrase it a little bit, i can't quite understand what you want to do exactly, by the way, did you use the table (last one)? –  Ghost Dec 1 '14 at 13:14
    
No worries, i actually figured it out myself. I did use the table in the last one and worked perfectly! I didn't know I could put php anywhere in my html :) Thanks! –  Navvy Dec 1 '14 at 13:32
    
@Navvy oh okay, so you were asking me where to place it, yes it should be place where you wanted it to be :) anyways im glad this helped –  Ghost Dec 1 '14 at 13:51

Try it:

$data = implode(" ", $filearray)."<br/>";
share|improve this answer
    
I want to print file, date and size on separate lines from each line of the array. –  Navvy Dec 1 '14 at 9:07

Since the data that you refer to are in an array, you may easily display the three items separately by using iteration and some <BR> and/or newline characters as follows for a simple list:

<?php

$file = 'filename';
$date = 'thedate';
$size = 'thesize';

$arr = array('file' => $file, 
                   'date' => $date, 
                   'size' => $size);

foreach ($arr as $key => $value) {    
    echo "$key: $value<BR>\n<BR>\n";
}

//Output:

file: filename

date: thedate

size: thesize

If you wish to have the data in a table, you might wrap the data in <tr><td> tags instead, as follows:

<?php

echo "<table>\n";
foreach ($arr as $key => $value) {    
    echo "<tr><td> $key: </td><td> $value </td></tr>\n";
}
echo "</table>\n";

Do you have any need for $filearray beyond what it does in the code you show? If not you could eliminate the $filearray and use the three variables directly by joining them with newline characters with implode() and assign the result to a variable, having the code display its value. Alternatively, you could wrap each variable in <tr><td> tags and eliminate the need to use implode().

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.