-1
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Web Programming using PHP - Coursework 2 - Task 1</title>
</head>
<body>
    <header>   
        <h1>Web Programming using PHP - Coursework 2 - Task 1</h1>
    </header>
    <main>
        <?php
         $navLists = ['main'=>['home'=>'Home Page','study'=>'Study','research'=>'Research','sem'=>'Seminars'],
         'study'=>['ug'=>'Undergraduate', 'pg'=>'Post Graduate', 'res'=>'Research Degrees'],
         'research'=>['rStaff'=>'Staff','rProj'=>'Research Projects','rStu'=>'Research Students'],
         'sem'=>['current'=>'Current Year','prev'=>'Previous Years'],
         'ug'=>['cs'=>'Computer Science','ds'=>'Data Science'],
         'pg'=>['swe'=>'Software Engineering','cf'=>'Computer Forensics']
         ];

        // Function to generate navigation menu
        function generateNavMenu($menu, $navType, $activeItem = null) {
            echo '<nav>';
            foreach ($menu as $key => $value) {
                $url = $_SERVER['PHP_SELF'] . '?activeNAV=' . $navType . '&contentSelected=' . $key;
                echo '<a href="' . $url . '">' . $value . '</a> ';
            }
            echo '</nav>';
        }

        // Display main navigation menu
        generateNavMenu($navLists['main'], 'main');

        // Check if a menu item is selected
        $activeNAV = isset($_GET['activeNAV']) ? $_GET['activeNAV'] : 'main';
        $contentSelected = isset($_GET['contentSelected']) ? $_GET['contentSelected'] : null;

        // Display all submenus if necessary
        if ($activeNAV !== 'main') {
            generateNavMenu($navLists[$activeNAV], $activeNAV);
        }
        if ($contentSelected && isset($navLists[$contentSelected])) {
            generateNavMenu($navLists[$contentSelected], $contentSelected);
        }

        // Display the text of the selected item
        if ($contentSelected) {
            foreach ($navLists as $menu) {
                if (isset($menu[$contentSelected])) {
                    echo '<p>Selected: ' . $menu[$contentSelected] . '</p>';
                    break;
                }
            }
        }
        ?>
    </main> 
</body>
</html>

here is my code i have issue with my middle menu i want all menus displays but i encounter issues many times my middle menu (| Undergraduate | Post Graduate | Research Degrees |) disappears when i select any item from last menu (| Software Engineering | Computer Forensics |)

I hope my question will be solve

1 Answer 1

0

First, let me bring to your attention that the $activeItem parameter of generateNavMenu() is never used -- that indicates that a re-think and some editing is required.

Second, when a pg option is selected, let's say Computer Forensics, then $_GET['activeNAV'] will be pg and $_GET['contentSelected'] will be cf. Therefore, isset($navLists[$contentSelected]) will not evaluate as true because the $navLists array does not have a first level key called cf.

Third, I don't think I appreciate the decision to implement your hierarchical structure as a stacked, two-level array. As you can tell, once you reach a 3rd level of depth, you lose track of the ancestral data chain. Perhaps it would be wise to refactor your function to receive a dynamic number of keys; then the function itself could be called only once and potentially multiple menus printed.

There are too many diverse ways to approach this task and I don't know which parts you intent to preserve and which parts can be refactored.

Not the answer you're looking for? Browse other questions tagged or ask your own question.