What I'm trying to do:
I have two files: One is the header.php which is included (by php include) into my second file content.php
I am going to be use my header.php with many different 'content' files, just so you know
Now, I have a div that is going to display a title text provided by content.php and any other pages that includes the header.php: <div class="title"> </div> This div is only in the header.php.
My problem is that I do not know how to include a text into the header.php div area <div class="title"> </div> from my content.php which again includes the header.php.
Header.php (contains the <div class="title"></div>)
Content.php (needs to have the title text and be included into the header.php <div class="title"> <div> area)
My question is: How in heck can I do this with php?
And I am not very knowledgeable about php or any type of coding for that matter. I have just been winging it, now I'm rather stuck. So If anyone could possibly explain how to do this in a simplified way, I would really appreciate it.
A very basic simple system would work like this:
header.php:
<div class="title"><?php if (isset($title)) { echo $title; } ?></div>
content.php:
<?php
$title = 'This is my content page';
?>
blah blah blah html here.
<?php include('header.php'); ?>
blah blah more html her e
header.php would check for the presence of a "title" variable, and output it if need be. If there's no title varaible, you get an empty div.
The content page is responsible for setting the title variable. And when the header page is included, it'll be in the same "scope" as the content.php code, pick up the variable, and run with it.
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.
As of right now I have this code in one of my templates on my Wordpress theme.
// blah blah
<div class="content">
<?php include_once( 'http://www.website.com/new/file.php?variable=1' ); ?>
</div>
// blah blah
Now, it will load everything in this template except this php include! It will load the sidebar which is included above the /* blah blah */ and it will load the footer, navigation, etc.
When I Inspect Element on Google Chrome it shows this:
<div class="content">
(it's empty)
</div>
Please help me if you know what I'm doing wrong! Thank you!
include and require don't take URLs. They take server filepaths. Try this:
<?php include_once(ABSPATH.'/new/file.php'); ?>
EDIT: Forgot to mention that query strings cannot be included in filepaths. If a query string is necessary for proper loading, consider loading the page with all necessary query variables in your current URL, loading the part using an iframe, or using file_get_contents()
Iframe:
<iframe src="http://www.website.com/new/file.php?variable=1"></iframe>
file_get_contents()
<?php echo file_get_contents('http://www.website.com/new/file.php?variable=1'); ?>
I am very new to webscripting and technologies. So please pardon me if this is a very basic query. Further I have been researching this all day and I haven't been able to figure out why this is not working.
I am essentially trying to improve a website for my wife. The design dictates that we have a common header.php file that can be included into the various static pages that the website contains.
Now as a new requirement, they want to target a few static pages targeting different keywords, and for this the company wants to setup a different "Heading Message". So I want to dynamically pass a different "$page_heading" before I include my header.php.
My header.php has a bunch of HTML code with interspersed in between , so the following doesn't seem to be working:
In <mypage.php>:
...
<body>
<div class="container">
<?php
//global page_header;
$page_header = 'A Wildlife Resort of South India';
include('header.php');
?>
...
In <header.php>:
<header class="row-fluid">
<div class="header-main">
<img src="http://www.innthewild.com/img/itw-logo.png" alt="Inn The Wild" style="margin-top:8px;"/>
<div class="pull-right" style="height: 20px;">
<nav>
...
<div class="heading pull-right">
<?php
if(!isset($page_heading)) {
$page_heading = 'A Wilderness Retreat — Masinagudi Jungle Resort, India';
}
echo $page_heading;
?>
</div>
...
The above seems like it should work straight up, but the variable seems to be losing its scope across a new instance. How do I make this work?
Here are the example pages: http://www.innthewild.com and http://innthewild.com/resorts-places-around-bangalore.php
You're calling it $page_header in mypage.php and $page_heading in header.php.
Change one or the the other so that the variable name matches across them
u don't need to pass it
$some_var = "text";
include "some.php";
the script you just included can access the $some_var
Im working on a Wordpress portfolio site and there is a line of text
<?php get_template_part( 'post-meta-page' ); ?>
that pulls information from that file and displays it on the page. Right now it displays only the title of the blog/page. I want it to display a unique line of text for each page while keeping the original title. I looked into it a little bit and this could be referred to as parsing?
Example: from external file.
<html>
<p class="homepage">Text for homepage</p>
<p class="interior">Text for interior page</p>
<p class="contact">Text for content page</p>
</html>
In other words, I want to call a specific class on one page and another class in another page. Any snippet of Javascript or PHP that can do this?
You need to edit the post-meta-page page and check where you are and add the appropirate text.
For example:
if(is_home()){
<p class="homepage">Text for homepage</p>
}
For most of my projects I make an administration interface, which has the same design for every project. The design of the header, the footer, the topbar, the leftmenu, the css, etc. are always the same. It is a pity to create the views every time; so I was thinking: maybe there would be a nice way to put the admin interface in my MVC library, as it is reused by every project?
But for the moment, in every single view I got code like the following:
<?php $this->_include('/includes/doctype.php'); ?>
<head>
<?php $this->_include('/includes/head.php'); ?>
<title>Some title</title>
</head>
<body>
<?php $this->_include('/includes/topbar.php'); ?>
<div id="page">
<?php $this->_include('/includes/header.php'); ?>
<?php $this->_include('/includes/leftmenu.php'); ?>
<div id="content" role="main">
<h1>Some title</h1>
<p>Blah blah blah.</p>
</div><!-- /#content -->
<?php $this->_include('/includes/footer.php'); ?>
</div><!-- /#page -->
</body>
</html>
Would it be a good idea to extract the custom content from the structure of the interface, and put that structure in my library somehow to make it reusable?
After that how will it be possible to customize the title and the actual menus?
I do this all the time. I have a custom header and footer file that are called at the start and end of every page.
<?PHP
Require("includes/header.php");
...
Require("includes/footer.php");
?>
The header provides a database handle, a datetime string and handles logon, priveleges, logging of pageviews etc.
The footer provides a standard HTML page but includes some systematised variables. It also generates the menu dynamically from the driving database then closes the database connection.
This way when I write code, I don't get mixed up in the HTML and any bugs are easy to find.
I like variables akin to:
$display_scripts - adds extra data in the head section.
$display_onload_scripts - adds onload scripts to body section.
$display_style_sheets - option to include link to additional stylesheets
$display_above_menu - will appear above the menubar. NOT recommended.
$display_below_menu - will appear immediately below the menubar.
$display_one_column - page contents when only one column is to be used
$display_left_column - page contents when two columns used. Left pane.
$display_right_column - page contents when two columns used. Right pane.
$display_footer - appears in footer division.
My main code then just has to generate the appropriate variable. Fundamentally, what you need to do is examine the source of a good age you have produced then replace the stuff you want to change with variables.
Here is a schematised version of the file I use (pseudocode) to give you an idea of how I do it.
// Code here generates the menu from database
// Code here genereates popup alert messages from other users
//permanent links to external style sheets go here.
//You can also select skins here.
<?PHP
echo $display_style_sheets;
echo "<title>".$display_page_title."</title>";
?>
<script type="text/javascript" src="JAVASCRIPT GOES HERE.js"></script>
</head>
<body <?PHP echo $display_onload_scripts;?> >
<div id="page_area" >
<div id="banner">
</div>
<?php
echo $display_above_menu;
if(!$hide_menu){echo $display_menu;} //Insert the menu variable here.
echo $display_below_menu;
?>
<div id="content_area">
<div id="inner_content">
<?PHP
if($display_number_of_columns==1)
{
echo "<div id='onecolumn'>".$display_one_column."</div>"; //I only use this one
}
if($display_number_of_columns==2)
{
echo "<div id='leftcolumn'>".$display_left_column."</div>"; //these are left for legacy support from before I got better at CSS.
echo "<div id='rightcolumn'>".$display_right_column."</div>";
}
echo "<div id='footer'>".$display_footer."</div>"; //just in case - I hardly use it.
echo $display_pop_box; //for user alert messages to other users
?>
</div>
</div>
</div>
<div id="logbox"> Automatic Logout statement</div> //this is called by JS to activate timeouts.
</body>
</html>
<?PHP
$mysqlidb->close();
?>
Sorry it's such a lot of code. The layout allows easy adaptation and makes it simple to find the offending variable if things are not going as expected. There are more elegant solutions but this works well for me and is very fast.