I have a php page that generates all the html and echo's it. Now I want to write a php script that I can use include to handle the footer code. that way if I need to update the footer on all the pages I can just edit the code in the included page.
But when I use include("footer.php"); and the footer page contains the footer code that works if it exists on the page it breaks and prints the code. Im very confused as too why?
if(isset($_SESSION['user_cart']) && count($_SESSION['user_cart']) > 0)
Starts writing the code on the page if I include from 0)
Please help?
EDIT: The code in the footer is wrapped in <?php ?>
You need to wrap that code in footer.php with php tags.
<?php
if(isset($_SESSION['user_cart']) && count($_SESSION['user_cart']) > 0) ...
?>
Dont your short hand notation of php tags <? ?>, use <?php ?>instead...
It sound like you didn't start php file with
<?php
Please make sure that your footer file has <?php in the beginning.
I'm not sure if you stated your question very clearly.
If you are echoing code out it will appear as soon as you include the file. Your file should look like this:
<?php require_once 'header.php';
// content goes here
require_once 'footer.php'; ?>
Related
I want to add a common code for footer on all my web pages. I created a new php file named commonFooter.php and have the below code in it:
<?php
echo '--footer-code-in-html--';
?>
On my home page I added the below code:
<?php require_once('/static/v3/commonFooter.php'); ?> // /static/v3 is the path of the commonFooter.php
But somehow the footer is not showing up on the webpage. Can anyone help me out what am I missing.
The require_once statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again.
See the include_once documentation http://php.net/manual/en/function.include-once.php
Try
<?php echo('/static/v3/commonFooter.php'); ?>
If that works and the statement is printed out in the page , Try
<?php require_once('static/v3/commonFooter.php'); ?>
If that fails, then take a share the folder structure of your project.
I've started creating a custom html page for the first time and i've hit a bump. The code in the header will be the same in all my pages , so i want to put it in an external file (header.php) and just put 1 line of code in all my pages to link to it but it doesn't seem to be working .
Note - I want to include just basic html code ( not a function ) - i'm thinking that's why its not working but not sure.
HTML file :
<body>
<?php include('header.php') ?> // didn't work
<?php get_header(); ?> // didn't work
<div class="content"> </div>
</body>
PHP File :
<h1>TITLE</h1>
When i put the code directly on the page - it shows "TITLE"
When i try include or get_header , i get a blank page . I checked the codex and online posts but i've not been able to fix it .
Any help would be appreciated - Thanks
Try in your header.php:
<?php
echo "<h1>TITLE</h1>";
?>
Then where you want to insert the header:
<?php include '/header.php' ?>
The / character makes the path to header.php relative to your root.
<?php include("dyn_layout/cbside_php.php"); ?>
This works for me... just don't forget to rename your files to .php extension... including the file where the insertion is made.
apology for this newbie question.
I have created an html page (dash.html) that uses the same header as the other pages.
it calls this PHP function <?php include 'header.php'; ?>
the dash.html contains a special <div> made specially for that page; and it must be placed inside the header.php
im trying to figure out how to enable/disable a certain div on a certain html page.
will it require a PHP conditional statement?
Yes. The easiest way is to include a conditional line in the header, and pass the checked variable from each page that calls header. So, in your dash.php (it can't be a .html if it calls php, can it?):
<?php
$includediv = true; // set to false, or leave out, if you don't want the div
include('header.php');
?>
and in the header.php:
<?php
...some other code...
if($includediv){
...code to include div...
}
?>
This will continue to work as before for all other pages that call header.php.
You are trying to show the div only if header.php is present, right?
So, just set a variable inside header.php and use a conditional inside the HTML page.
Try this code
<?php
$path=explode('/',$_SERVER['REQUEST_URI']);
$page=end($path);
if($page=='dash.php')
{
?>
<div>your div for dash.php page</div>
<?php } ?>
My problem is that I try to include a class using include_once(); but instead of including the desired file, it prints the code in the file straight into the source html so the code appears on my webpage.
This is the code:
<?
include_once('php/class.loadClients.php');
$client = new loadClient();
$client->connect();
?>
I'm not sure why its doing this as I've used include_once(); elsewhere in this page and it works fine:
include_once('php/class.register.php');
Thanks in advance!
Open php/class.loadClients.php, and add <? to the beginning of it.
To add to Paulpro's answer: if his solution does not work your server might not have short tags enabled ( <? vs <?php ). Use the full <?php tag.
I'ld like to insert the footer of my website dynamically so I can add it to multiply pages without copy and paste it every time.
I know an iframe can do it but I heard that's not a good solution and not working for seo.
Can it be done with PHP or JavaScript?
footer.php code as follows
<?php
//Write your custom footer here
?>
home.php code as follows
<?php
//All contents of Home page
include('footer.php');
?>
<?php include('path/to/footer.php'); ?>
PHP is the right tool for this job:
<?php include('footer.php'); ?>
from php use include function
e.g: <?php include('inc/footer.php');?>