• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Common text for Multiple Pages

Engaged ,
Jun 23, 2017 Jun 23, 2017

Copy link to clipboard

Copied

Suppose I have a set of links or just plain text, which is identical across multiple html pages. Is there a way to 'encapsulate' that into one file, which is separately maintained, and which can then be called on every page that needs to have it? Hope that is clear.

Views

471

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 23, 2017 Jun 23, 2017

The simplest way to do that would be by using server side includes...

1. Save your pages that will have includes with .php extensions (servers don't normally parse php in .html files, but you can do that, pretty easily, if your hosting company allows it)
2. Create an empty file that will have only the code of the item(s) you want to include. For example, if it's a group of links, the file will only have the <a href=... code and text for the links, no <html>, <head>, <body> or other code that norma

...

Votes

Translate

Translate
Community Expert ,
Jun 23, 2017 Jun 23, 2017

Copy link to clipboard

Copied

The simplest way to do that would be by using server side includes...

1. Save your pages that will have includes with .php extensions (servers don't normally parse php in .html files, but you can do that, pretty easily, if your hosting company allows it)
2. Create an empty file that will have only the code of the item(s) you want to include. For example, if it's a group of links, the file will only have the <a href=... code and text for the links, no <html>, <head>, <body> or other code that normally appears in a full page. Includes are just fragments being added into the page, not complete pages.
3. Save that file into an includes folder in your site for organizational purposes...

includes
     header.php
     navigation.php

     text.php

4. Add a php include statement into the code of the page that you want your server to add it to, where you want it added. The server will replace the include statement with all code from the include file, so inserting it in the correct location is important. With the above folder structure and example pages, the include statement would something look like...

<?php include 'includes/navigation.php' ; ?>

Wherever you put that small snip of code, all code from the navigation.php file will be written in its place.

5. Upload your pages and include folder to the server. As long as you have PHP installed (most servers do), the include's file will be written into every page that it appears on, automatically when your viewers pull them up. If you ever want to change something site-wide, all you need to do is update and upload the include file. The server will push your updates automatically to every page that has a given include statement.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 23, 2017 Jun 23, 2017

Copy link to clipboard

Copied

This is Looking very promising. Next question: can I pass parameters to the php? That would make ever smooch more useful for my present purpose (showing jpg images in sequence).

With thanks in advance,

jwc

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 23, 2017 Jun 23, 2017

Copy link to clipboard

Copied

You can do just about anything with php, from simple includes like the above to an entirely database driven website with ecommerce.

If you start a new thread about the type of script you want to make, I'm sure one of the resident php gurus can point you in the right direction.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 23, 2017 Jun 23, 2017

Copy link to clipboard

Copied

LATEST

You can pass variables to and from the include files in several ways. At this point you should probably start studying basic PHP, but here are some ways to pass variables.

  • If you simply define a variable in the parent page, it will be available in the include. If you define a variable in the include, it will be available in the parent page, but not before the include is called. (example:  $myvariable = "apples";
  • Pass a variable using a URL parameter like this: mypage.php?myvariable=apple. You then retrieve the variable like this: $myvariable = $_GET['myvariable'];

  • Pass a variable in the POST or SESSION array

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 23, 2017 Jun 23, 2017

Copy link to clipboard

Copied

To parse PHP includes on shared hosting that supports PHP code, your parent pages need a .php extension.   However the include files themselves can be named anything you wish: foo.html or foo.php, or foo.whatever

Nancy

Nancy O'Shea— Product User, Community Expert & Moderator
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 23, 2017 Jun 23, 2017

Copy link to clipboard

Copied

Thanks, Nancy; this looks very promising. Will respond further after implementing and testing (may be a while; several files to work on).

jwc

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines