I am working on a new website and I am trying to teach myself some PHP to make this whole process easier. I am trying to create one master PHP template that will use variables to call the different HTML pages that will house the content.
Here is a very basic example of what I need it to do:
<body>
<!-- Navigation with PHP Variable rather than links --> <a href=" PHP Variable ?phpMyAdmin=4594f30712f4fabaff6997416810f3f2">Home</a>
<!-- Body where the variables will load --> <?php ( Call the Variable ); ?>
</body>
Then the HTML file would be:
<div class="ClassName">
Content for the Home Page
</div>
I just need to know what to put there. I understand the basics behind it, but I don't know what to call it or how to grab the HTML files that will house the content area. I also need to have the home page load by default.
I like to make a file for each page which includes the common page elements such as the header, footer and navigation. The goal is to make it so that, except for setting a few unique page variables that are available in the included elements, the only contents of each file is that page's unique content. This way you can put the files in a logical folder structure so you get friendly URLs and it is easy to manage. Below is a sample page, hopefully it is fairly self-explanatory. The only thing to know about the include code is that the paths are always relative from the file that the code is in. So in this example, the index.php file is the home page that is in the same folder as the "assets" folder where I keep everything that isn't a web page. If you were to then include something from the header for example, the path would be relative from the header even though you are still compiling the home page.
<?php
// set the unique page variables $bodyClass='homepage; $pageTitle='Welcome to Widgetland';
// add on the header html @ require_once ('assets/php/includes/generic/header.php'); @ require_once ('assets/php/includes/generic/navigation.php');
?>
<div id="main"> <!-- unique page content goes here --> </div>
<?php
//add on the rest of the html @ require_once ('assets/php/includes/generic/footer.php'); ?>
I understand how to include files into the content, what I am trying to accomplish is to have one master layout that I can change and then have each individual page that I can switch out the content so editing them will be much faster.
I began my research on this subject with the article you linked to and quickly found a shortcoming that I couldn't abide. It means that (without messing with Apache "mod_rewrite" settings) your URLs will all have ugly query strings on the end in order to set which page's content is displayed. That's why I figured out my way. Maybe I didn't explain it well enough, because the benefits you are after are what it provides, and also enables URLs like www.somedomain.com/ for the home page and (for example) www.somedomain.com/clients/ for the clients page etc. because the clients page would be called index.php and within a folder called "clients" which would be in the root folder of your site. the clients page would look identical to my example above but would have different page variables (to populate the 'title' tag and add a class to the body element to provide CSS hooks, for example), include paths (as its in a different folder than the home page in my example) and the bit that says "<!-- unique page content goes here -->" will be only as much HTML necessary to show that particular page's unique content. It keeps it simple yet offers all the benefits you desire. The only question you may have is "what if I want to change the structure of the HTML that is part of the page's unique content?" And my answer would be: okay, then you would have to change each page's file individually, which is why its important to extract as much as possible (the stuff that is common to all pages) into includes. Or you could just have literally everything in includes and set more page variables at the top of each page in your site to contain the unique content, but I find that a bit more inflexible without getting overly complicated with how that content is then inserted into the HTML. The important bit of my suggestion is that each page does have it's own file which is in a folder structure that matches your site structure, and each file is named index.php. Then updating content for each page is easy. I hope I am helping you - I do think I understand what you are trying to achieve and I have used this method on quite a few sites now and it has helped with the ongoing maintenance greatly and I am still pleased with it. Good luck.
It's as simple as this. Start the main file you want to use the variables in with <?php include 'security/variable.php';?>
values will be seen by any subsequent included files.
Assuming you are doing this to avoid using a database to store information, the information can be stored in multidimensional arrays in variable.php file.
Try playing around using this method but be mindful it will only work for clean urls eg: /index.php etc
variable.php:
/* Get the current page */
$url=$_SERVER['PHP_SELF']; $url=split('[/ .]' , $page1); /*turns the url into an array with / and . as the separators*/ $num=count($url)-2; /* gets the page name */ $page=$url[$num];
/* Set the array fields eg: */ $header [index]['title']='the title'; $header[index]['keywords']='the keywords';
In the main file when you want a value put the following: