I currently have a very simple page, with some more complicated backend. I've always done this the same way, but I feel it's not right. But I haven't come up with much useful to me.
I've got my index.php, header.php and function.php. index.php includes header.php, which calls a function from function.php. The main thing I'm not sure about is how to make the website dynamic enough that it can have easily editable pages, but also be easily editable is a big part of it needs editing.
index.php
<?php
session_start();
include_once 'header.php';
?>
// Page content
</body>
</html>
header.php
<!DOCTYPE HTML>
<html lan="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<?php
require_once 'functions.php';
getBar(); // Top of page bar with menu/login etc
?>
I don't think showing functions.php would add that much. I know I shouldn't use 2 <head>
s in my file, so what do I do if I want to have a description on each page? I've previously had the files setup like:
index.php
<?php
session_start();
include_once 'header.php';
?>
// extra page specific stuff here, such as description or JS
</head>
<body>
<?php
require_once 'functions.php';
getBar(); // Top of page bar with menu/login etc
?>
// Page content here
</body>
</html>
header.php
<!DOCTYPE HTML>
<html lan="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css" type="text/css" />
However, I think the files are "fragmented". I don't know if there is a generally accepted way of doing this, but if there is, I'd love to hear it!
The <script> element should also go into the head, and should include a src attribute containing the path to the JavaScript you want to load, and defer , which basically instructs the browser to load the JavaScript after the page has finished parsing the HTML.
Using these simple steps, we can easily add the PHP code. Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to use the PHP. Step 2: Now, we have to place the cursor in any tag of the <body> tag where we want to add the code of PHP.
If you want to run your HTML files as PHP, you can tell the server to run your . html files as PHP files, but it's a much better idea to put your mixed PHP and HTML code into a file with the . php extension.
PHP processor scans the page, line by line. It build a processed HTML page. If it finds HTML, it passes that on as part of the processed HTML page it is building. If it finds PHP scripts, the PHP processor may or may not output HTML.
Methinks that you are looking for templates.
Though you almost nailed it, you are mixing matters a little. Just separate your "design parts" from "code parts". Don't make header to do the job of the actual code and everything become as clear as a fresh water
Let me suggest you the following setup:
A site contains of
the main idea is to make PHP script do no output until all data got ready - thus you will get an instant solution of your page title problem. Moreover (think about it):
Once php script done with data preparations, it calls for the the main site timplate, which, in turn, will include the actual page template.
Thus you will get exactly what you're asking for - the website dynamic enough that it can have easily editable pages, but also be easily editable is a big part of it needs editing - and many more other benefits.
Template I am talking about is a typical PHP script, however, consists mostly of pure HTML, with some PHP code only to display dynamically generated data.
Here you go with some basic yet working example
the page:
<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
// setting title for using in the main template
$pagetitle = "Links to friend sites";
//etc
//set page template filename
$tpl = "links.tpl.php";
//and then finally call a template:
include "main.tpl.php";
?>
where main.tpl.php
is your main site template, including common parts, like header, footer, menu etc:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>
and links.tpl.php
is the actual page template:
<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><a href="<?=$row['link']?>" target="_blank"><?=$row['name']?></a></li>
<? endforeach ?>
<ul>
I think most experienced coders would tell you to mix your code and your HTML as little as possible. This means doing all of your calculations, then simply passing any variables needed by the view to the view.
They would also tell you to look into the MVC design pattern. In PHP, this is where Controller classes take the initial request, perform the correct operations on the correct business Model classes & then pass needed data to a View class, which would render your HTML.
MVC can go from very simple 3 class variations, to extremely complex "Frameworks" like Zend Framework. If you build your own, look into PHP class autoloading, as it will make your life easier.
The general accepted approach in small sites is to have something like this:
<?php $title = 'Title'; ?>
<?php require_once('header.php'); ?>
<title><?php echo $title; ?> - MyWebsite</title>
I think that should work fine for you, while still preserving the K.I.S.S principle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With