0

I have searched all over the web trying to figure this out and am now trying to get a direct answer from some experienced users. I hope I can explain myself completely.

I know HTML and CSS and some PHP and Javascript, but no mean an expert. This is my questions:

When creating a website by hand (no Drupal, or Wordpress or predesigned templates), The first thing I do is create an index.php file that shows my HTML page layout. The second thing I do is create my links.inc.php file that will show all the links to my pages, ex: Home, About Us, Contact Us. Now on the index.php page I create php include files for the header, footer, and link pages. (these would read header.inc.php, footer.inc.php, links.inc.php) Now here is where I am trying to figure if there is an easier way to do the next step.

My normal steps would next to be to create a home.inc.php, aboutus.inc.php, contactus.inc.php files which will have all the "content" I want shown for each page. I would then create a duplicate of the index.php and create aboutus.php where I would use the php include function to add the aboutus.inc.php into the "main content" area I would want this information displayed at. Then I would create anther duplicate of the index.php and name it contactus.php and "include" the contactus.inc.php file.

Is there any way to use the index.php file and have all the inc.php files on that page? For instance,

<div id="main">
   <?php
     include ("home.inc.php");
     include ("aboutus.inc.php");
     include ("contactus.inc.php")
   ?>
</div>

Obviously this does not work they way I have it laid out above, it shows all the pages at the same time instead of only showing the one page that is clicked on from the menu. Any suggestions? Is there a different way or am I doing it correctly with creating multiple pages?

Thank you for any help and I hope I was clear, if not I can try to explain a different way.

4
  • if you know some javascripting, or bootstrap, you can use tabs to do this. Commented Jan 14, 2015 at 19:16
  • You would have to use JavaScript and/or CSS to hide/show what you want hidden/shown. Commented Jan 14, 2015 at 19:16
  • 1
    Include the appropriate file conditionally, based on a variable that determines which page you want to display (e.g. index.php?page=about). Commented Jan 14, 2015 at 19:17
  • Consider a modern framework like Laravel. It'll make your life easier in so many ways. Commented Jan 14, 2015 at 19:47

2 Answers 2

2

My suggestion is to include files conditionally, based on a variable that defines the current page.

For example, given the following navigation:

<a href="index.php">Home</a>
<a href="index.php?page=aboutus">About Us</a>
<a href="index.php?page=contactus">Contact Us</a>

Configure your index.php file to include external files, something like this:

// determine the requested page, default to the home page
$page = isset($_GET['page']) ? $_GET['page'] : 'home';

// check if the requested include file exists
$include_file = is_file($page.'.inc.php') ? $page.'.inc.php' : false;

// if the requested include file exists, include it
if ($include_file) {
    include $include_file;
}

Feel free to adjust the logic. For example, if a $page value is not recognized as a valid page on your site, you may want to show a 404 page, default to the "home" page, etc.

Edit

If your include files are in a different directory, you'll need to provide the correct path:

// define the path to includes
$path = 'inc/';

// check if the requested include file exists
$include_file = is_file($path.$page.'.inc.php') ? $path.$page.'.inc.php' : false;
Sign up to request clarification or add additional context in comments.

5 Comments

I am trying to use this but I can't seem to get it to work correctly. I wrapped the $page section all in <?php ?>, I was supposed to do that? I made the links look like what you have, but my page is coming up blank, the style is there but no information from the inc.php files. I'm not sure what I am doing incorrectly. Please help if you can.
OH, not sure if this matters but the index.php file is in the main folder and then I created an inc folder with all the inc.php files in that. Although the links.inc.php page (that lists the links menu) is in the main folder with the index.php file.
Yes, if your includes are in another directory, you'll need to provide the correct path. I have edited my answer accordingly.
Also, my page names did not match yours. That's probably self-explanatory, but I have updated them for clarity.
Thank you so much! It works perfectly. I have been trying to figure this out for so long and now it works! Thank you again. I will mark this as answered.
0

You could send a variable to PHP index.php?action=home; then, inside index make some verifications

if($action=="home") {include index.inc.php; }
else if ($action=="contact") {include contact.inc.php }
and so on.

Comments

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.