PHP file layout/design
I would like to create a site in php that works the same way that https://www.bitcoins.lc/ does, in terms of it having the same layout on each page but the content would change as you change links/pages, how do I set this up in p开发者_JAVA技巧hp with layout.php and index.php and header.php ect?
I was told to read about MVC frameworks but I don't really understand it all.
Any help or advice would be appreciated.
Jason
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.
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.
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/
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
精彩评论