Best way of loading content that should appear on every page - php

Until now, I've been using the <iframe> tag to load things like headers/footers/navbars into my webpage. These cause so much hassle though and as I'm about to start building a new site I thought I'd get it sorted now.
I was thinking of having all the html code in a php file and just loading it in dynamically.. Ideally I'd like the code to become a part of the page. So it appears inline. But I also want to be able to edit one single file if I need to change one bit rather than editing the same file 100 times.
<iframe>'s did this well until recently and I don't want to use workarounds to solve my problems. Could someone please post some code I could adapt or post a link to something that tells me how to do this? Cheers

You can use PHP's include() function to include elements like headers and footers in your pages.
So:
include('header.php');
. . . will look for a file called header.php in the same directory and include it in your page. Then you just need to write this at the top of your pages.
That said, this isn't really a very good way to go about designing your site. How about looking for a content management system, that allows you to keep the design and content of your site separate?

Are PHP includes what you're looking for ? http://php.net/manual/en/function.include.php

Related

Looking for a way to change all headers and footers on my html/css website at once

I built my website in 2007 using html and css which I learnt from a book. I'm a jeweller and it's used as a portfolio of my work and therefore has lots of photos on it and separate pages with each photo. I have continued to add photos and pages and not made much changes to the overall structure of the website, just copied and pasted the code into each new page and changed the bits I needed to.
But now I want to change the headers and footers of all these pages, and there's hundreds of pages!
After some reading it seems I can use PHP (just finding out about this) to insert headers and footers. Which seems to mean I'd need to edit every page of code anyway, and change all links in the code to .php, which would be the same (or more!) amount of work as just changing the code on every page to be what I want, although will make it easier next time I want to change.... so wondered if there was another way of doing this?
First time asking anything on a web developer forum! As I'm sure you can tell I'm no expert so keep things simple please! My website is www.islayspalding.co.uk. Many thanks :)

Virtual path but different content

I'm trying to make a really simple website with multiple pages. I've been coding in HTML for a while now, but just started to experiment with PHP.
My page setup goes like this:
Rootfolder/settings/language -- In there, I just put a index.php
Rootfolder/settings/Privacy -- In there, I also just put a index.php
In this particular case, I could indeed use rename the files to language.php/Privacy.php to reduce space. But that is still not what I'm expecting from a lightweight website.
In order to make it easier for myself, I use include_once to include the header, meta data (I know it's not smart, because of SEO reasons), footers and other stuff that is very general and has to be the same across the whole website. But here is the thing; I really think that this method is way to complicated. Every time I need a new file, I just have to duplicate the page that includes the include_once files, and then change the content of it. Same goes for the titles.
So, what I'm looking for is a page setup like this (1) :
Rootfolder/Pages/index.php -- Inside that index.php, the content has to change dynamically. The visitors still have to go to: https://domain.tld/settings/language but the server has to decode it to the setup from (1) and change the content to the original content of Rootfolder/settings/language/index.php.
In the past, I've downloaded some PHP scripts, which all included title setups like: <title> $title - $SiteName </title> (or something like that) - So it must be possible to change content dynamically.
The problem is... I really don't know where to start. Does anyone has good/lightweight solution for this?

How Do I Use PHP Includes to Display HTML pages?

I have my web page setup to use a new management system, but would also like to use some basic html pages for other content (such as images or whatever), and I seem to remember from years ago that there was a way to use PHP to load HTML pages that are not formatted into a content area, which I assume are DIVs.
I also seem to remember that I didn't have to create an if statement, but I'm not sure and quite honestly I'm awfully confused about how to do this. A friend used to do this coding for me way back when, so the way it was setup was that all I had to do was tell my navigation where to find the file and it would load it inside of my index.php file.
Hopefully my question is clear, but if not, my website is right here, and I just want my HTML files to load into the are with the content image, but only the grey portion (which I do have a DIV set up for). Any help would be much appreciated! Thanks!
The simplest way is by using require and include.
for more details, you can visit the following link:
http://www.w3schools.com/php/php_includes.asp
For example:
<?php include 'footer.html';?>

Update multiple pages

I am doing some freelance work for a client and I need to re-code an old menu. The entire site is static which will make this process extremely slow and redundant, does anyone have a good technique for updating multiple pages automatically?
The old developer used "Allwebmenus" which is a automatic menu creation tool. It is implemented by using JavaScript which writes HTML to the DOM. I'm going to replace this with a clean html menu and some simple jQuery.
Right now I think the best way is to create a separate .html file with the menu code, and use PHP includes on all the pages but this still requires me to update every page on the site. Can anyone give me better idea? Or do you think this is the best option?
Thanks for the help!
create menu.php and include("menu.php") into each file where the old menu's are written.
It will make your life easier going forward too.
As far as fixing all the static pages, you will have to go in and do that yourself.
include("menu.html");
You can use includes, but it might also make since to put them onto a CMS like Drupal. Handles a lot of that for you.
Using the PHP includes is a good method. If the "Allwebmenus" has Javascript code on each page, you'll have to edit each file anyways, so adding the includes is no big deal.

where to place the php code?

i would like to know your point of view on where to position the PHP code on .php page and why?
a) top of the document
b) just above the html elements where i am going to use it.
thank you.
c) In a different file and use a template engine such as smarty
http://www.smarty.net/
Your life will be beautiful and awesome after smarty.
EDIT : I won't downvote other solutions , but it's a very ugly anti-pattern to mix html code with php, you have good, stable and easy solutions to avoid that, use it now or your website will be a big mess of spaghetti code.
Depends on the purpose.
Database query related posts that determine the contents within the part, I call it before there is any input. Also any type of PHP commands that contain raw header information should be presented before any output is made.
Any content related stuff can be positioned anywhere on the page. PHP code is really everywhere - where ever, and however you want to create the HTML from your PHP dynamically.
My pages usually take this structure:
<?
include 'start.php';
$pagetitle = 'the services we offer (branding, web, print etc.)';
$metatitle = 'Our Creative Services (branding/logo, web, print)';
$scriptinclude = 'whatwedo.js';
include 'header.php'; // contains the <body><head></head><body> and a few more elements to start the header/menubar etc.
?>
<div class="full_grid" id="index_slide">
// content here, mixed with PHP if you like...
</div>
<?
include 'footer.php'; // contains the footer HTML, as well as </body></html> etc. to wrap things up.
?>
I put as much code as I can at the top. And only use php withi HTML where I need loops or output data.
This gives me a better overview of the code and it's easier to work with.
Keep your code and HTML as separate as possible. Have them in entirely separate files where you can.
Your HTML should be as much pure HTML as possible, and your PHP code should contain as little HTML as possible.
Obviously, you're producing a web page, so there will have to be some mixing, but keep it as limited as possible: The only code you should mix in with your HTML should be the one-liners to place specific bits of PHP-generated code into your HTML template.
It completely depends what you're doing with it. Personal preference for me is to create any functions I need at the top and then scatter inline php throughout the document calling the functions at the top of the page.
If something needs calculating and it can be done at the top, it's much easier to read and debug if you keep it all in one place. And keeping this the same throughout all your files will help too. What you could do is just include a config file at the top of the page with any site-wide functions you need too, so you don't have to copy and paste through all your files.
If you are only using one PHP file then definitely put all PHP code at the top, then the HTML below with variables where necessary.
For example, $title = 'Page title'; at the top of the page, then <h1><?=$title?></h1> in the HTML portion of the page.
However a better solution is to have two (or more) files. The main one contains all the PHP logic to grab/process data, while the second one is a "view" file containing mostly HTML. The simply include your view file from the main PHP file.

Categories