Search code examples
phphtmlgetphp-include

Include different homepages dynamically with PHP


I have created template with multiple sections or segments, so for example i have navbar.php, homepage.php, about-us.php, contact.php... And basically whole website is cut in parts and on index.php i just have includes, like include('navbar.php'), include('homepage.php'). Also i have different homepages like slider-homepage.php, image-homepage.php, video-background-homepage.php... So what i need is ability to dynamically include these different HOMEPAGES with navigation links. So if i click on slider-homepage link i want it to grab and include slider-homepage.php part and put it on index and the rest of includes always stay the same. Is this possible or i need to create different php file for each of these homepages.

index.php

include('home_main.php');
//include('home_text_slider.php');
//include('home_text_typer.php');
//include('home_video_background.php');
//include('home_video_background_slider.php');
include('about.php');
//include('split-section-two.php');
include('split-section-video.php');
include('services.php'); 
include('parallax-a.php');

navbar.php

<li><a href="#">Home Main</a></li>
<li><a href="#">Home Text Slider</a></li>
<li><a href="#">Home Text Typer</a></li>
<li><a href="#">Home Video Background</a></li>
<li><a href="#">Home Video Background Slider</a></li>

Solution

  • Eventhough I do not recommend this kind of implementation of programming a website, you could do the following

    Add a variabele to your links like:

    <li><a href="index.php?page=slider">Home Text Slider</a></li>

    In your index.php

    switch($_GET['page'])
    {
       default:
       case 'homepage':
         include('home_main.php');
         break;
    
       case 'slider':
         include('home_text_slider.php');
         break;
    }