public function find_children($categoryId, $conn) { $this->categoryId = $categoryId; //finding left and right $sql = "SELECT CategoryName,dpth, rgt, lft FROM nested_category WHERE CategoryId={$categoryId}"; $result = $conn->query($sql); if (is_null($result)) { return; } $row = $result->fetch_assoc(); if (is_null($row)) { return; } $this->categoryName = array_shift($row); $myDpth = array_shift($row); $myRgt = array_shift($row); $myLft = array_shift($row); $sql = "SELECT categoryName, categoryId FROM nested_category WHERE lft>{$myLft} and rgt<{$myRgt} and dpth={$myDpth}+1"; $result = $conn->query($sql); if (is_null($result)) { return; } while ($row = mysqli_fetch_array($result)) { $b = new SimpleClass($row["categoryName"], $row["categoryId"]); $b->find_children($row["categoryId"], $conn); array_push($this->child_categories, $b); } }