Hi chrisdee,
Here goes the explanation.
If im not mistaken you are defining a empty array here, correct ? But I don't understand why you define an empty array ?
$sortArr = array();
Well as you can see I`m using that array inside the foreach($read as $file) loop. Even most people consider PHP as loosely typed language, Its always best (advised) to define your variables. This has more serious issue when it comes to security so I advised you to read about it in the net.
Next, where does the variable $sortType come from, and how is it used ? Why is it set == filesize ?
if($sortType == 'filesize') //you can use this for name / date etc...
As according to your original requirement you have mentioned that you need to sort it by name, size and date. That`s why I defined that variable to tell the PHP script to execute which type of sorting. Consider below example,
<a href="index.php?sort=name">Name</a> | <a href="index.php?sort=filesize">Size</a> | <a href="index.php?sort=date">Date</a>
if(isset($_GET['sort']))
$sortType = strtolower($_GET['sort']);
if($sortType == 'name')
//do the sorting according to the name
else if($sortType == 'filesize')
//do the sorting according to the size
if($sortType == 'date')
//do the sorting according to the date
I`m sure you can modify the size example for name and date easily.
What does the [] mean infront of $sortArr, and why is it set = filesize($file) ?
$sortArr[] = filesize($file);
Well this is the basic of arrays. Im strongly suggest you to read about PHP arrays more. Consider below example:
Assume you have 3 files which has sizes as 2, 4, 6 (Kb`s).
//This basically loop though the file list and store the file size in the array. I`m storing the file size because I need to sort the files according to size. So for sort by name or date store name / date in that array
foreach($read as $file)
{
$sortArr[] = filesize($file);
}
// according to my example the $sortArr will be
$sortArr[0] = 2;
$sortArr[1] = 4;
$sortArr[2] = 6;
//This is also equal to
$x = 0;
foreach($read as $file)
{
$sortArr[$x] = filesize($file);
$x++;
}
Hope this clear out your doubt`s.
Thanks,
Best regards,
Niroshan