Это заняло несколько дней, но я наконец понял это. Я хотел поделиться на случай, если у кого-то еще будет аналогичная проблема. Обратите внимание, что код может быть «неправильным» или может быть лучший способ сделать это, но он работает для меня. У меня здесь вызываются несколько более мелких функций, поэтому, если у вас есть вопросы, почему:
function findAllFolders($parentId, $folderList = array()) {
/// This function finds all the user's allowed directories and returns an array of id's.
/// Starting with the parentId, all directories within are added to the array.
// Is the folder valid?
if ($parentId > 1) { // The smallest folder id in my database is 1
// Does it still exist in the Database?
if (!mysqli_num_rows(select("folders","id=$parentId")) > 0) {
return false;
}
// If so, add it to the array.
array_push($folderList, $parentId);
// Find all folders that have this as its parent folder.
$subfolders = select("folders", "parentid=$parentId");
if (mysqli_num_rows($subfolders) > 0) {
while ($row = mysqli_fetch_assoc($subfolders)) {
array_push($folderList, $row["id"]);
}
}
}
foreach ($folderList as $folder) {
$result = select("folders", "parentid=$folder");
while ($row = mysqli_fetch_assoc($result)) {
if (!in_array($row["id"],$folderList)) {
return findAllFolders($row["id"],$folderList);
}
}
}
// Remove all duplicates.
array_unique($folderList);
return $folderList;
}
///HELPER FUNCTION:
function select($table, $condition="1", $columns="*") {
$sql = "SELECT $columns FROM $table WHERE $condition";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// note: $conStr is a global variable connection I created for my program.
return mysqli_query($conStr, $sql);
}