0

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)
)
6
  • What's your question? Commented May 11, 2022 at 14:57
  • This should work with any number of categories, why isn't it future-proof? Commented May 11, 2022 at 14:58
  • How you can turn this to a HTML list Commented May 11, 2022 at 15:32
  • echo "<li>{$categorie['Name']} - {$category['Parent']}</li>"; Commented May 11, 2022 at 15:38
  • You shouldn't use 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 like SELECT r1.Name AS parent_name, r2.Name AS child_name, ... See stackoverflow.com/questions/33531324/… Commented May 11, 2022 at 15:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.