0

I am making a script that should sort folders and show them in rows and columns on the web. It should be 4 columns and just continue down until no more folders are found. I got everything except that magic part where it sorts the objects in rows and columns. My idea is that i make two variables: $hor(column) $ver(row) and then i use css to place the objects that the script is making. Does anyone have an idea on how to handle this? Javascript is not an option due to the requirements

This is my script so far:

<?php


error_reporting( E_ALL );
ini_set( 'display_errors', 1 );


$directory = '/path/to/directory';
$scanned = array_diff( scandir( $directory ), array( '..', '.' ) );
$count = count( $scanned );
echo $count;

for ( $i = 2; $i <= $count + 1; $i++ ) {
    $x = $i - 2;

    // Magic code that sorts into horizontal and vertical numbers

    echo '<div id="' . $x . '" class="' . $hor . ' ' . $ver . '"><a href="/' . $scanned[ $i ] . '"><div class="inner"><p>' . $scanned[ $i ] . '</p></div></a></div>';
}

?>
5
  • 1
    You should at least explain how you want the elements distributed in rows and columns: how many columns? A fixed amount, or something dynamically determined? Sorted from left to right first, and then from top to bottom, or first from top to bottom, and only then from left to right? Commented Jul 15, 2017 at 20:15
  • Yes of couse, i have edited the question Commented Jul 15, 2017 at 20:19
  • Can you add the CSS you planned to use for this? Commented Jul 15, 2017 at 20:23
  • @mplungjan JS is not an option Commented Jul 15, 2017 at 20:24
  • @trincot i will have to make it first, but i think something like this: .hor1{left: a;} .hor2{left: b;} .hor3{left: c;} .hor4{left: d;} and .ver1{top: x;} .ver2{top: y;} .ver3{top: z;} Commented Jul 15, 2017 at 20:31

2 Answers 2

1

If i got you right, this is might what you are trying to achieve.

The init counter tells the loop wich entry in the array it should start at and the increment counter tells the loop how many entries to skip. You can at any point change the number of columns by adding/removing a loop and change the increment counter in all loops. When adding a loop you have to increas the value of the init counter by 1.

for (init counter; test counter; increment counter)

To gain the $ver and $hor we are using the init counter to calculate the position.

    $directory = "/path/to/directory";
    $scanned = array_diff( scandir( $directory ), array( "..", "." ) );
    $count = count( $scanned );

    for ( $i = 2; $i <= $count + 1; $i += 4 ) {
        $x = $i - 2;
        $v = $i + 2;
        $ver = $v / 4;
        $hor = 1;
        echo "<div id=\"" . $x . "\" class=\"ver" . $ver . " hor" . $hor . "\"><a href=\"/" . $scanned[ $i ] . "\"><div class=\"inner\"><p>" . $scanned[ $i ] . "</p></div></a></div>";
    }

    for ( $i = 3; $i <= $count + 1; $i += 4 ) {
        $x = $i - 2;
        $v = $i + 1;
        $ver = $v / 4;
        $hor = 2;
        echo "<div id=\"" . $x . "\" class=\"ver" . $ver . " hor" . $hor . "\"><a href=\"/" . $scanned[ $i ] . "\"><div class=\"inner\"><p>" . $scanned[ $i ] . "</p></div></a></div>";
    }

    for ( $i = 4; $i <= $count + 1; $i += 4 ) {
        $x = $i - 2;
        $v = $i;
        $ver = $v / 4;
        $hor = 3;
        echo "<div id=\"" . $x . "\" class=\"ver" . $ver . " hor" . $hor . "\"><a href=\"/" . $scanned[ $i ] . "\"><div class=\"inner\"><p>" . $scanned[ $i ] . "</p></div></a></div>";
    }

    for ( $i = 5; $i <= $count + 1; $i += 4 ) {
        $x = $i - 2;
        $v = $i - 1;
        $ver = $v / 4;
        $hor = 4;
        echo "<div id=\"" . $x . "\" class=\"ver" . $ver . " hor" . $hor . "\"><a href=\"/" . $scanned[ $i ] . "\"><div class=\"inner\"><p>" . $scanned[ $i ] . "</p></div></a></div>";
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Use scandir(); for your directory which return your files as array then loop through them using foreach print each file as a table cell use counter to end the row if it is 4 columns with in it.

$files=scandir($directory);
$i=0;
echo "<table>";
echo"<tr>";
foreach ($files as $file){
echo "<td>".$file."</td>";
$i++;
if(($i%4)==0){
echo"</tr>";
}
}
if(($i%4)==3) echo "<td></td><td></td><td></td></tr>";
if(($i%4)==2) echo "<td></td><<td></td></tr>";
if(($i%4)==1) echo "<td></td></tr>";
echo "</table>";

1 Comment

Can you please explain your answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.