PHP File Layout/design
Solution 1:
Here's the approach I generally take to this:
FILES:
- layout.php
- index.php
- views
- _index.php
layout.php:
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<?php include($childView); ?>
</body>
</html>
_index.php:
<section>
Some page content
</section>
index.php:
<?php
$title = 'Home';
$childView = 'views/_index.php';
include('layout.php');
?>
Basically, the page itself tells the layout what view to inject into the content area. It's similar to how you would use ASP.NET ContentPlaceholder elements.
Solution 2:
You could include a header and a footer in each page:
<?php
include('header.php');
?>
Welcome to my page
<?php
include('footer.php');
?>
Put the header and navigation in header.php, and the footer in footer.php.
Solution 3:
Hope you are worried about MVC as you are reading complex documentations about that. I would suggest you to go through a documentation which helps you to easily understand and create an MVC Framework. It will not be good if you move forward with your project without understanding its basics. Please have a look at the following article and let me know if it helps. Feel free to contact if you need any support.
http://www.henriquebarroso.com/how-to-create-a-simple-mvc-framework-in-php/
Solution 4:
The simplest approach is the one described by Sjoerd. If your page only contains some few elements, there is noting wrong with a switch or if statement.
index.php:
<html>
<body>
<!-- Wrapper div -->
<div id="wrapper>
<!-- Header div -->
<div id="header">
<?php
include('header.php'); // File containing header code
?>
</div>
<!-- Content div -->
<div id="content">
<!-- Left Colon div -->
<div id="leftCol">
<?php
include('leftMenu.php'); // File containing the menu
?>
</div>
<!-- Center colon -->
<div id="centerCol">
<?php
$page = $_GET['page']; // To get the page
if($page == null) {
$page = 'index'; // Set page to index, if not set
}
switch ($page) {
case 'index':
include('frontPage.php');
break;
case 'about':
include('about.php');
break;
case 'contact':
include('contact.php');
break;
}
?>
</div>
</div>
<!-- Footer div -->
<div id="footer">
<?php
include('footer.php'); // File containing the footer
?>
</div>
</div>
</body>
</html>
header.php:
<?php
echo "This is header";
?>
leftMenu.php:
<?php
echo "<a href='index.php/?page=index'>Front Page</a>"; // set page to index
echo "<a href='index.php/?page=about'>About</a>"; // page = about
echo "<a href='index.php/?page=contact'>Contact</a>"; // page = contact
?>
and so on
Post a Comment for "PHP File Layout/design"