Hi for school we have to make a website and we have to use a category with a self join I want it future proof so i can add more categories in the future.
function getCategories(){
global $Conection;
$sql = $Conection->query("SELECT * FROM Categorie r1 left JOIN dbo.Categorie r2 ON r1.Id= r2.Parent");
$data = $sql->fetchAll();
return $data;
}
$categorieList= getCategories();
function renderCategories()
{
global $categorieList;
foreach ($categorieList as $categorie) {
var_dump($categorie);'
// here code for a ul
}
}
CREATE TABLE Categories
(
ID int NOT NULL,
Name varchar(100) NULL,
Parent int NULL,
CONSTRAINT PK_Categories PRIMARY KEY (ID)
)
echo "<li>{$categorie['Name']} - {$category['Parent']}</li>";SELECT *when you do a self-join. You'll get each column twice, but they'll have the same names, so only one of them will be in$categorie. You should use something likeSELECT r1.Name AS parent_name, r2.Name AS child_name, ...See stackoverflow.com/questions/33531324/…