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>
Related
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
I am creating my first website from scratch and had seen something where you can reduce code by using PHP includes for sections of the site that are to be repeated. So far, I have a head.php (which I added due to my stylesheet.css being linked there and needing access to it on every page), header.php, footer.php, index.php, and other pages with the php extension (about, contact, that bunch).
Everything is appearing where I'd like it to except for one issue: when setting the body background color, everything (all includes: header.php, footer.php) seems to be in the body. I tested this by setting a border around the body, and it confirmed what I thought.
Does anyone have an idea what is going wrong? I am using flexbox in my header, footer, and other bits within the index file, but I don't think that should be affecting anything.
<!DOCTYPE html>
<?php include 'head.php'; ?>
<?php include 'header.php'; ?>
<body id="main-block">
<!-- Button links to Portfolio and Other stuff -->
<div class="flex-container">
<a class="main-button" href="#">Web Work</a>
<a class="main-button" href="#">Other Work</a>
</div>
</body>
<?php include 'footer.php'; ?>
</html>
your body tag is suppose to wrap around your header and footer elements. see below markup
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<?php include (header);?>
<?php include (footer);?>
</body>
</html>
if u wanna change the background color of your elements just add css
element { background-color:pink }
I admit that I do not really understand your issue. But I'll give you a little snippet to start coding php:
<html>
<head>
<title>Welcome</title>
<style>
<?php include 'stile.php'; ?>
</style>
</head>
<body>
<?php include 'header.php'; ?>
<?php include 'footer.php'; ?>
</body>
</html>
So I've recently discovered how to use php includes to include a footer and header in each of my files to avoid copy pasting all the header/footer code to each file. But let's say I have a footer.php, header.php, home.php, and about.php
Do I have my title, opening html/body tag, etc. in the header.php or home.php and about.php.
//header.php
<html>
<head>
links to header.css
links to home.css
links to about.css
</head>
<body>
//home.php
<?php include("header.php"); ?> //PROBLEM: the header.php also includes other .css such as "about.css", etc. that could result in problems later.
</body>
</html>
What Should I do to fix this? One way I thought of is to remove the beginning part(html,head,title) of the header.php file and move it home.css and about.css so they each have their own css links.
You're on the right track. Break out the stylesheets as well as the javascripts into other php files and include them as well. So all pages have the following structure.
home.php
<?php $this_page = "home.php";
include "template.php";
For other pages, just replace the $this_page variable. The structure common to all pages is actually the template.
template.php
<!DOCTYPE html>
<html>
<head>
<title>My Website</title>
<!-- CSS-->
<?php include "stylesheets.php" ?>
</head>
<body>
<!-- common header -->
<?php include "header.php" ?>
<section>
<!-- PAGE CONTENT HERE determined by $this_page value -->
<!-- 'content_home.php', 'content_about.php'... have the content-->
<?php include "content_$this_page" ?>
</section>
<!-- common footer -->
<?php include "footer.php" ?>
<!-- link javascript files -->
<?php include "scripts.php" ?>
</body>
</html>
The only thing that changes from one page to the next is the value of $this_page. It's what determines which content gets loaded in the template above, and it also determines which CSS and JS files to include.
stylesheets.php
<?php
$cssDir = "path/to/styles/"; //folder where all CSS files live
//Link each page to its CSS file
$styles = [
'home.php' => 'home.css',
'about.php' => 'about.css',
'contact.php' => 'contact.css',
];
?>
<!-- CSS common to all pages -->
<link rel="stylesheet" type="text/css" href="<?="$cssDir/common.css"?>>
<!-- CSS, specific to the current page -->
<link rel="stylesheet" type="text/css" href="<?="$cssDir/$styles[$this_page]"?>>
The same approach can be used with the javascript you link to in scripts.php. Now that your HTML is into discrete modules, it is easy to edit a part of your site without worrying about another part breaking. In particular I recommend never to open a tag in one php file and close it in another because that would be a nightmare to debug, maintain and modify as your site gets bigger.
About paths:
Remember that when the browser sees the page, in place of include "stylesheets.php" and include "scripts.php", it will see the echoed contents of that file exactly as they are. So in those files you want your path to be either:
absolute paths from your domain root (simplest)
relative paths from the location of the top-level php file (eg home.php)
just the file name, if it is located in PHP's include PATH (places where PHP looks for content by default before throwing an error)
For header and nav you have to create a seperate file like nav.php which will contain only the nav and your site header not <head></head> and include it after your header.php. LIKE
//Home.php
<?php
include("header.php"); this will contain your head part mostly your .css and .js files
include("nav.php"); This will only contain header and nav
// home.php code goes here
?>
Also use below code will automatically get path to your root.
<?php
$PATH = "http://localhost/Folder/"; // change this when needed
$PAGE = basename($_SERVER['PHP_SELF']);
?>
Then Add your files like this
<link rel="stylesheet" href="<?php echo $PATH; ?>assets/plugins/font-awesome/css/font-awesome.css">
the bottom line is your code being accessible & easy to maintain, I would have a head.php, header.php & footer.php file. In the head.php you may want to include your config.php if you are connecting to a database & also have all the <html><head><title><link><script> tags you will include in every page then on your index.php or home.php
include('head.php');
include('header.php');
etc etc
I intent to create a template PHP file, this template will only serve the design, not the content. The purpose is to decrease the development time in a sense that when creating a new PHP file or new module, I can only need to concentrate on the main function of that PHP file not the design. Once I created the new file using the template, it should be able to display the consistent design and serve its specific function.
The issue is that I am not sure on how to make the design of the template works and applied to all of the new files created regardless of the location (as long as it is within the root directory).
As an example:
root directory (www.example.com): /
homepage (www.example.com/index.php): /index.php
css file: /style/style.css
template file: /template.php
newly created file (www.example.com/subone/find/css/file.php): /subone/find/css/file.php
another newly created file (www.example.com/subtwo/locate/css.php): /subtwo/locate/css.php
Content of the homepage (which is created base on the template.php, but the CSS file location is hard coded):
<html>
<head>
<title>Testing</title>
<link rel="stylesheet" type="text/css" href="style/style.css" />
</head>
<body>
<div id="header">logo and login form goes here
<div class="nav"> navigation goes here;</div>
</div>
<div id="main">main content goes here;</div>
<div id="footer">footer goes here; </div>
</body>
</html>
but, when I created a new file, /subone/find/css/file.php
the location of the css must be changed and specified manually, like this:
<html>
<head>
<title>Testing</title>
<link rel="stylesheet" type="text/css" href="../../../style/style.css" />
</head>
<body>
<div id="header">logo and login form goes here
<div class="nav"> navigation goes here</div>
</div>
<div id="main">main content goes here;</div>
<div id="footer">footer goes here;</div>
</body>
</html>
So, what I want to achieve is that, when creating a new file (/subone/find/css/file.php), I don't need to do anything, I can straight away concentrate on the main section:
<html>
<head>
<title>Testing</title>
...style.css is handled automatically
</head>
<body>
<div id="header">logo and login form goes here
<div class="nav"> navigation goes here</div>
</div>
<div id="main">main content goes here;
<?php
//I can continue to edit the file from this line onward
echo "I am concentrating on the main function of file.php right now!!";
?>
</div>
<div id="footer">footer goes here;</div>
</body>
</html>
example page can be seen at (only the desired design): neoborn.kodingen.com
I accept any answers as long as it can achieve my intention (template).
Thank you :)
Why don't you use absolute paths when referring to CSS files and other resources in your template file?
For example:
<link rel="stylesheet" type="text/css" href="/style/style.css" />
There are 2 options,
Use absolute paths for your css files <link rel=stylesheet href="/style/style.css">
Use HTML's <base> element to cause all relative paths on the page relate to it.
I would use a easy to install template engine. That will help speed up development and still give you the freedom to do whatever PHP you like.
Try http://www.raintpl.com/ that should be quick and easy for you to install and get back to coding the pages. If you include it in your PHP inc folder, it will be available for every PHP file you create. So you won't need to add an include line at the top of each PHP file.
Index.php
<?php define('BASE_URL', 'http://localhost'); ?>
Template.php
<link rel="stylsheet" type="text/css" href="<?php echo BASE_URL; ?>/style/style.css ?>" />
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.