How to include from multiple directories - php

My header.php file contains the .css for my project. I've made a new page that is a few directories in. (root/mods/people/employees/addemployee.php)
If i put the file in the root, the css works fine. If I put it where I want it, the css doesn't appear.
Is there a way around this? Im trying to keep things organized.
Add Employee Code:
<?php include("../../../includes/layouts/header.php"); ?>
<div id="main">
<div id="subnavigation">
<?php
include('../../../mods/main_menu/index.html');
?>
</div>
<div id="page">
<p>Add Employee!</p>
</div>
</div>
</div>
<?php include("../../../includes/layouts/footer.php"); ?>
Header.php Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>Company H&S Site </title>
<link href="stylesheets/public.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header">
<h1>H&S Site </h1>
</div>

The problem is these headache inducing ../../../ relative path calls. Instead I recommend you set a default base path and never worry about stuff like this again:
<?php
$BASE_PATH = '/the/path/to/the/codebase/';
include_once($BASE_PATH . "includes/layouts/header.php");
?>
<div id="main">
<div id="subnavigation">
<?php
include_once($BASE_PATH . "mods/main_menu/index.html");
?>
</div>
<div id="page">
<p>Add Employee!</p>
</div>
</div>
</div>
<?php include_once($BASE_PATH . "includes/layouts/footer.php"); ?>
If you don’t know the base path to your files, then place this line at the top of your PHP code:
echo "Your path is: " . realpath(dirname(__FILE__)) . "<br />";
And load that page. Somewhere near the top should be a line that reads:
Your path is: /the/path/to/the/codebase/
Of course /the/path/to/the/codebase/ will be your actual file path, but that will be your base path. Then just set $BASE_PATH to that value.
By setting a hard-coded base path with $BASE_PATH you always know where your code is located & can place your pages anywhere in a directory structure with ease.
I would also recommend using include_once instead of include to avoid scenarios where your script might inadvertently attempt to load the same file more than once.
include_once($BASE_PATH . "includes/layouts/header.php");

Related

PHP static site generator

I am currently building up a static website with PHP.
What I want to have is the following:
There is one main.php file that includes all the common parts of the page (header, footer, navigation and so on) and a couple of pages like index.php, team.php, contact.php and so on.
I do want to be able to edit the main.php in that way, that is effects all the pages in my project. I do however want to be able to output some specific content for each single page by writing code directly in the specific file (not main.php but e.g. index.php). So I want to assign each page of the project to use main.php as the core template.
My main.php file which looks so far like this is here:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Static Site Genarator</title>
</head>
<body>
<div id="navigation">
<?php echo navigation()?>
</div>
<div id="pageWrap">
<header>
Header
</header>
<main id="content">
<?php echo $templateContent; ?>
</main>
<footer>
Footer
</footer>
</div>
</body>
</html>
and a couple of pages like index.php, contact.php and so on.
All of them look like this:
<?php include_once $_SERVER['DOCUMENT_ROOT'].'/essentials/settings.php'; ?>
<h1>
This is the h1 for the index page
</h1>
<?php require($_SERVER['DOCUMENT_ROOT'].'/essentials/exit.php'); ?>
I deliver some settings in my settings.php file and in my exit.php file I have the following code:
<?php
$templateContent = ob_get_contents();
ob_end_clean();
echo $templateContent;
?>
I need to somehow bind all the pages to be the part of main.php at the point where I output the $templateContent variable at
What is the right way for me to achieve this?
I personally would consider using a micro PHP framework like lumen, slim, fat-free-framework for making even the smallest PHP web application.
That said, below is the solution of the approach you took to solve the problem. I will keep the file structure and file naming similar to yours, even though there is a place for improvement here.
Lets consider the following application structure:
essentials/main.php
essentials/navigation.php
essentials/exit.php
essentials/settings.php
index.php
about.php
contact.php
As you can see, I have moved all common files into the essentials folder and left all pages in the root folder
essentials/settings.php
<?php
// start output buffering
ob_start();
essentials/navigation.php
<ul>
<li>index</li>
<li>contact</li>
<li>about us</li>
</ul>
main.php
<?php $templateContent = ob_get_clean(); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Static Site Generator</title>
</head>
<body>
<div id="navigation">
<?php include("navigation.php"); ?>
</div>
<div id="pageWrap">
<header>
Header
</header>
<main id="content">
<?= $templateContent; ?>
</main>
<footer>
Footer
</footer>
</div>
</body>
</html>
<?php require_once("exit.php"); ?>
essentials/exit.php
<?php
ob_end_flush();
The actual pages structure index.php, about.php and contact.php look similar:
index.php
<?php require_once("./essentials/settings.php"); ?>
<h1>
This is the h1 for the index page
</h1>
<?php require_once("./essentials/main.php"); ?>
I hope this helps to move your idea forward, but highly encourage you to investigate time and learn a modern approach for PHP application development. Laravel is a great stating point.
Maybe you want something like this?
Structure
index.php
about.php
core\function.php
core\setting.php
templates\header.php
templates\footer.php
templates\menu.php
core\function.php - all function in here
core\setting.php - all setting in here
templates* - html page
index.php
<?php
include_once("core\setting.php");
include_once("core\function.php");
/*
* code php for this file in here
*/
$message = 'index data';
include_once("core\header.php");
include_once("core\menu.php");
echo <<<HTML
<div>show content {$message}</div>
HTML;
include_once("core\footer.php");
?>
It looks like you are looking for template engine.
There are many out there like Twig, Blade, Smarty
Or you can write one alone like it is explained in this excellent post by David Adams

Links as Variables in other File

my problem is that i have a navbar and need to put i on each page.
So Home, Profile, Search, Add have the same navbar fixed in bottom.
Now i want to save the links to the page in a diffrent file.
So something like:
<div class="botnav">
<a class="active" href="links.php?$homelink">Home</a>
Profile
Search
Add
</div>
is this even possbile with GET or POST?
Thank you
I think you need to create a page with your code:
header.php
<div class="botnav">
<a class="active" href="links.php?$homelink">Home</a>
Profile
Search
Add
</div>
In each page you can call that with
<?php include ('header.php'); ?>
The way I include a nav menu to be used on different pages is with PHP's include statement include().
<?php include("path_to_header"); ?>
What I recommend is to create a few extra files: path.php and header.php to hold your nav menu.
The path.php file should be located in the root of your project so that __FILE__ provides the actual root project path.
<?php
define("ROOT_PATH", realpath(dirname(__FILE__)));
?>
Next, handle the header being in a separate file header.php for reference.
<div class="botnav">
<a class="active" href="links.php?$homelink">Home</a>
Profile
Search
Add
</div>
Then in each of your pages include() the path.php file and also the header.php with another include statement where ever you choose.
<?php include("path.php"); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>El test</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- Include header file where you need -->
<?php include(ROOT_PATH . "/header.php"; ?>
</body>
</html>

style.css cannot be found while my references to my php files are still functioning

I expect that it is a really stupid question and it must be some kind of newbie mistake but I cannot figure out why my index.php cannot find my style.css. The page was working the whole time until I changed the map structure too something more OO. Now I have the problem that my images and css files cannot be found but my php files are still functioning. I have changed the style references so it fits the new structure. Only it does not work. I have uploaded some images below to illustrate my problems.
This image shows my file structure: http://imgur.com/DbDHOwe,vRLfwO3
This image shows a part of my code: http://imgur.com/DbDHOwe,vRLfwO3#1
I would really appreciate it if somebody is able to take a look at it. I am just not able to figure out what is wrong.
Here is the code in text:
<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>Home</title>
<link href="/css/style.css" rel="stylesheet" type="text/css">
<link href="/css/slider.css" rel="stylesheet" type="text/css">
<?php
require '../inc/SliderFunction.inc.php'
?>
</head>
<body onload="Slider();">
<?php
require '../inc/Topbar.inc.php';
?>
<div class="body">
<?php
require '../inc/Navigation.inc.php';
?>
<div class="content">
<div class="featured">
<img id="1" src="../img/slider/gummybears.jpg" border="0" alt="Gummybears"/>
<img id="2" src="../img/slider/sweethearts.png" border="0" alt="Sweethearts"/>
<img id="3" src="../img/slider/lollipops.jpg" border="0" alt="Lollipops">
</div>
<div class="producten">
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
<div class="product"></div>
</div>
</div>
</div>
<?php
require '../inc/Footer.inc.php';
?>
</body>
</html>
it's not stupid question.
you should referenc your css files like
"/css/style.css"
If I am seeing your file structure correctly, you have your php files inside a folder and trying to pull css files outside of the folder? Looks really unusual file structuring. It should be more like this.
Root Folder
css folder
img folder
includes folder
index.php (then call your css without / example: css/style.css same with your includes and images)
The homepage / index.php should be the first page on the main root and every folder goes inside from there.

php virtual (''); works but php include''; does not

I'm trying to understand the difference between the following. All of the reading I've done says that 'include' should work. I'm wondering 'why' I have to use the 'virtual()' and/or WHAT I'm doing wrong with the 'include'.
<?php virtual('/path'); ?>
and
<?php include'/path'; ?>
I use both in the code below. The 'virtual()' works and is used for header.php. The 'include' (does not work) and is used for footer.php. The code for the entire page is below.
Link to live page
<head>
<title>Untitled Document</title>
<link href="/student_sites/2013/web_40/pages/css_includes/css_includes.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<div id="header"><?php virtual('/student_sites/2013/web_40/pages/css_includes/assets/includes/header.php'); ?>
</div>
<div id="content_wrapper">Content for New CONTENT WRAPPER Div Tag Goes Here
<div id="side_bar">Content for New SIDE BAR Div Tag Goes Here</div>
<div id="content">Content for New CONTENT Div Tag Goes Here</div>
</div>
<div id="footer"><?php include '/student_sites/2013/web_40/pages/css_includes/assets/includes/footer.php'; ?></div>
</div>
</body>
</html>
<?php include'/path'; ?>
Doesn't work because of the type of file it's in. This statement must use the 'virtual()' which only works in php files. The 'include' only works in html or shtml files.
<!--#include virtual="/path" -->
for html files and
<?php virtual('/path'); ?>
for php files.
The html code still uses virtual but include as well, so I'm still not sure why it can't use include by itself.
-your 2ed period stutent

how to convert an HTML template into Cake PHP template

I'm a beginner in cake php. i have needed to convert my HTML template into Cake PHP template. any idea?
It is very easy.
Replace your template main file extension to .ctp
index.html to index.ctp
Look at the layout of you file (html code) and determine what section of that template you want to appear on all pages;
Usually most templates are setup as follows:
<html>
<head>
<title>My Site</title>
// You will include your javascript and css files here
<?php
echo $this->Html->css(array('cake.generic','default'));
echo $this->Html->script(array('myscript','jquery'));
?>
</head>
<body>
<div id="container">
<div id="header"></div>
<div id="body">
<div id="main_content" style="width:600px;float:left">
<?php
//Code for this should be in your home.ctp
// in your pages folder. Usually I cut this content from
// my template and place the whole thing in that file
// everything else happens magically
echo $content_for_layout;
?>
</div>
<div id="side_content" style="width:300px;float:left">
<!-- You can have content here manually or create elements and include them here like the following -->
<?php $this->element("sidebar_content"); ?>
</div>
</div>
<div id="footer">...</div>
</div>
</body>
You should then upload all images to the /img folder in your /app/webroot folder
Change your images path to reference /img folder.
You must do the same with all your CSS and JS files. Place them in their corresponding folders in the /app/webroot location.
Good luck!
Save your template in to APP/views/layouts/template.ctp.
Make sure it has at least two variables:
<title><?php echo $title_for_layout; ?></title>
and
<body>
<?php echo $content_for_layout; ?>
</body>
Fire up your view, or controller and add
$this->layout = 'template'; // view/method
or $layout = 'template'; // controller;
Look at the Cake default.ctp for ideas.

Categories