What are common pitfalls to avoid when defining menu items and sub-items for a gallery in PHP?
One common pitfall to avoid when defining menu items and sub-items for a gallery in PHP is not properly organizing the menu structure, leading to confusion and difficulty in managing the gallery. To solve this, it's important to clearly define the hierarchy of menu items and sub-items using arrays or objects.
// Define menu items and sub-items for a gallery
$menu = array(
'Home' => 'index.php',
'Gallery' => array(
'Nature' => 'nature.php',
'Animals' => 'animals.php',
'Cityscapes' => 'cityscapes.php'
),
'Contact' => 'contact.php'
);
// Loop through menu items and sub-items
foreach ($menu as $key => $value) {
if (is_array($value)) {
echo "<li>$key<ul>";
foreach ($value as $subKey => $subValue) {
echo "<li><a href='$subValue'>$subKey</a></li>";
}
echo "</ul></li>";
} else {
echo "<li><a href='$value'>$key</a></li>";
}
}