How do I format an array which holds the folders of a folder (Watchfolder) in a basic html table.
This is the basic function
function dirToArray($dir) {
$result = array();
$cdir = scandir($dir);
foreach ($cdir as $key => $value)
{
if (!in_array($value,array(".","..")))
{
if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
{
$result[$value] = $this->dirToArray($dir . DIRECTORY_SEPARATOR . $value);
}
else
{
$result[] = $value;
}
}
}
return $result;
}
I call it like this;
$watchFolderPath = 'C:\\xampp\\htdocs\\mop\\Rp\\';
$watchFolder = $this->dirToArray($watchFolderPath);
if I print_r this it gives me the correct values, in this case folders with its subfolder. Looks like this:
array:6 [▼
345 => array:2 [▶]
789 => array:2 [▶]
"folder1" => array:2 [▶]
"folder2" => array:2 [▶]
"folder3" => array:2 [▶]
"folder4" => []
]
I do not know how to format this properly.
You can simply echo
HTML tags mixed with the array results:
echo "<table>";
for ($row = 0; $row < count($watchFolder); $row++)
echo "<tr>";
echo "<td>" . $watchFolder[$row] . "</td>";
for ($col = 0; $col < 2; $col++) {
echo "<td>" . $watchFolder[$row][$col] . "</td>";
}
echo "</tr>";
}
echo "</table>";
Arrange the table as you see fit - <tr>
tags are rows, <td>
tags are data items.
Edit:
To handle folders with no subfolder, perhaps try something similar to this inside the loop.
if is_array($watchFolder[$row]) {
for ($col = 0; $col < 2; $col++) {
echo "<td>" . $watchFolder[$row][$col] . "</td>";
}
}