How to make the menu switch through include to php

Here is the menu

<li>Новостройки</li> <li>Нежилые помещения</li> <li>Дома и коттеджи</li> <li>Дачи</li> <li>Земельные участки</li> 

    3 answers 3

    you need to change or create from scratch - the extension of your file from html to php - index.php

    For example at the moment so

     <html> <body> <ul> <li>Новостройки</li> <li>Нежилые помещения</li> <li>Дома и коттеджи</li> <li>Дачи</li> <li>Земельные участки</li> </ul> </body> </html> 

    take the menu code

     <ul> <li>Новостройки</li> <li>Нежилые помещения</li> <li>Дома и коттеджи</li> <li>Дачи</li> <li>Земельные участки</li> </ul> 

    and save in the new file also with the php extension for example - menu.php

    now the index.php file will look like this

     <html> <body> <?php include("menu.php"); ?> </body> </html> 

    in more detail - http://www.php.su/include ()

    • It remains only to explain why you need to save the bare HTML in a file with the php extension :-) Or that - include('menu.html') will not work? - user6550
    • will be - this is as an example - soledar10

    This is what the menu.php file looks like, which offers to connect soledar10

     <?php $itemId = isset($_GET['itemId']) ? $_GET['itemId'] : 0; // выбранный пункт меню $menuItems = array( // все пункты меню 1 => 'Новостройки', 2 => 'Нежилые помещения', ); echo '<ul>'; foreach($menuItems as $menuItemId => $menuItem) { echo '<li><a href="?itemId=', $menuItemId, '" ', ($menuItemId == $itemId ? 'class="active"':''), // если активный совпадает с текущим, то выделить его с помощью класса CSS '>', $menuItem, '</a></li>'; } echo '</ul>'; ?> 
    • The problem in forming the base link in the line <a href="?itemId=', $menuItemId, '"> : it also needs to be somehow transferred to menu.php, but using global variables is the last century - copist

    If I correctly understood that:

     <html> <body> <ul> <li><a href="?menu=menu1">меню1</a></li> <li><a href="?menu=menu2">меню2</a></li> <li><a href="?menu=menu3">меню3</a></li> </ul> </body> </html> 

    well, switch

     <?php switch($_GET['menu']) { case 'menu1': include("menu1.php"); break; case 'menu2': include("menu2.php"); break; case 'menu3': include("menu3.php"); break; } ?>