I am a beginner, just trying to learn php. Below is my index.php file:
<?php
include("header.php");
require_once('connect.php');
include("home.php");
include("footer.php");
?>
I made a simple test site that has the home page included inside the index.php file. Do I have to use include(header.php); on every page if I want to include the same code?
Thanks.
You dont have include header.php on every page.
Try this:
<?php include("header.php");?>
<div class="container">
<?php include ("home.php");?>
</div>
<?php include ("footer.php");?>
Keep the header and footer, just change the contents( home.php, otherpage.php, ... ).
I recommend you to use jQuery to dynamically load the content of container
Related
I'm a beginner at PHP.
I have multiple webpages residing in different locations. So when I wish to link to header.php and footer.php from the webpages in different folders, is it possible to do so? As shown in the picture, I have to create three different folders, containing same files, header.php and footer.php, to be able to link from three different sources.
With Best regards!
Yes it is possible to use a single footer.php and single header.php files and load them anytime you need.
What I would suggest you can do is that you create an include folder, then inside the include folder create another folder called common where by you will place website that elements that are always the same throughout the website ie, footer and header.
then I would also place a functions file inside the includes where I will place my website functions. Included in this function file is a function that I will use anytime I want to use the header.php and footer.php files.
Functions.php
<?php
function loadView($viename,$meta=[]){
//load footer/header page
include_once "common/$viename.php";
}
//any other functions
The loadView() function is used anytime you want to load these two dynamic files. This functions takes two parameters 1 optional. The first parameter is the name of the view you want to load which is header or footer then the second optional is the meta information important for the header file, as the page title and meta description needs to be dynamic and change according to the page.
header.php
<!DOCTYPE html>
<html>
<head>
<title><?=$meta['pagetitle']?><!-- Dynamic page title --></title>
<meta name="description" content="<?=$meta['pagedescription']?>"><!-- Dynamic description -->
<!-- load your styles -->
</head>
<body>
<header>
<nav>
<!-- Your page navigation -->
<ul>
<li>Home</li>
<li>About</li>
<li>Another Page
</ul>
</nav>
</header>
footer.php
<footer>
footer content
<p>© website name <?=date('Y')?>
</footer>
</body>
</html>
Main website pages
Your main website pages are pages such as index, about,services etc.
In these pages you would load the functions file, then be able to load the header and footer.
index.php
<?php
include 'includes/functions.php';
//meta info
$meta = array(
'pagetitle' => 'Welcome to my site | site | bla bla',
'pagedescription' => 'This is your website description'
);
loadview('header',$meta); //load heade
?>
<section>
<div id="content">
<p>Page Content</p>
</div>
</section>
<?php
loadview("footer"); //load footer
?>
About Page
<?php
include 'includes/functions.php';
$meta = array(
'pagetitle' => 'About Us',
'pagedescription' => 'This is about page'
);
loadview('header',$meta);
?>
<section>
<div id="content">
<p>Page Content</p>
</div>
</section>
<!-- load footer -->
<?php
loadview("footer");
?>
Hope this gives you the idea on how you could achieve your goal, there are many ways you can achieve this.
Let me know when you need any help
Assign values for $h_path and $f_path dynamically.
<?php
$h_path = '';
$f_path = '';
include($h_path.'header.php');
include($f_path.'footer.php');
?>
My apologies for not providing enough information about the issues. My issue is that, when the index.php refers to the header and footer by "includes/header.php" and "includes/footer.php" respectively, and other webpages are located inside another folder which needs to access the includes folder via "../includes/header.php". There is no problem while referring to the files but the issue occurs when headers.php targets the webpages inside when it is written to only work with index.php. For example, would only work on index.php but not on the php files inside the folder which needs , But I'll try with $h_path = ''; and $f_path = '' soon.
everytime I create a website I usually end up by creating a simple index.php file that will load the requested pages.
Example:
include ('header.php');
if(isset($_GET['page']))
{
$page = $_GET['page'];
$display = $page.'.php';
}
else
{
include ('homepage.php');
}
include ('footer.php'); ?>
THE PROBLEM:
If I want to create a connection.php file that will access my database usally it won't work in other pages beacuse I have to rewrite "include('connection.php')", in every single file that isn't the index.php.
THE REQUEST:
How can I embed header, footer, connection, etc... In a proper and safe way ? So I don't have to include it in every other file ?
How do you usually include the header and the footer in every page, in order to create a dynamic website ?
There's multiple ways to fix this.
Use a templating engine, like Blade or Twig.
Create an autoloader
Blade will allow you to make a layout, which can look like this:
<html>
<head>
<title>App Name - #yield('title')</title>
#yield('css')
</head>
<body>
<!-- Include all your files -->
#php
include('myfile.php');
#endphp
<div class="container">
#yield('content')
</div>
#yield('js')
</body>
</html>
Then for every other page you can create a blade file that extends the layout.
#extends('layout.file')
#section('title', 'Page Title')
#section('content')
<p>This content will be placed in the container in the layout.</p>
#endsection
You can write an auto-loader by yourself or use a pre-written one. I'm not going to attempt writing one because I usually go with the pre-written ones.
This should give you an idea though.
I have some php include files which are pulling through content to my main pages such as header.php
Within these php files I have other php includes which call different elements of the design.
It will displaying perfectly fine until I converted the flat HTML files into a Wordpress theme directory.
The main header.php file displays but any php includes within it do not. Please could someone help.
Here is an example of a php file header-withbooker.php
<?php include 'includes/content/headers/with-booker.php';?>
Within this file it then looks like:
<?php include 'includes/content/nav/top-nav.php';?>
<div class="container">
<div class="full-header">
<?php include 'includes/content/rating-box.php';?>
<h1>Your journey begins here</h1>
<h2>4-90 seater vehicles & drivers available worldwide</h2>
</div>
</div>
</header>
<!-- Close Header Box -->
<?php include 'includes/content/booking/main-booker.php';?>
Any idea what I am doing wrong?
Thank you in advance
You can include files in WordPress Like:
get_template_part( 'includes/content/headers/', 'withbooker' );
get_template_part( 'includes/content/nav/', 'top-nav' );
get_template_part( 'includes/content/booking/', 'main-booker' );
Hope this works for You.
I have a navigation bar and wrapper saved in header.php and have included it on my content.php page. but when i load the content.php page, the content from content.php is outside the wrapper from the header.php page.
<html>
<body>
<div class="navigation">
<?php include 'header.php'; ?>
<h1>Welcome to my home page.</h1>
<p>Some text.</p>
</div>
</body>
</html>
all help is greatly appreciated!
Because you define your wrapper for your navigation menu (which is in your header.php file). If you want your wrapper to wrap your whole document define this in your content.php file.
My form create by php script and between teo file:
index.php
header.php
In index.php, i try in
...
<div class="art-header-png"></div>
<div class="art-header-jpeg"></div>
<?php include("header.php"); ?>
</div>
<div class="art-nav">
<div class="l"></div>
<div class="r"></div>
and my header.php
<div class="art-logo">
<h1 id="name-text" class="art-logo-name">Your Title Goes Here</h1>
<div id="slogan-text" class="art-logo-text">Your Slogan Goes Here</div>
</div>
But it didn't show content inside header.php.
How can i make it work?
First thing, either place header.php in the same folder as index.php or specify the correct path relative to index.php when you are using include (same apply to require).
PHP Include
also check to see if your php is configured correctly as well. Save the following line of code in a php file. Run it and see if you get php window:
<?php
phpinfo();
?>