The body of the base file has a include ' slider.php '.
In de slider.php there are css and javascript files that need to be load.
Because of the include it will but all of that in the body where the include is posted.
My question is: Is it possible if you have a include the file. You can say in the include put css and javascript in the <header> tag and not where the include is made.
[index.php]
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="slider">
<?php require_once( INCLUDES . 'slider.php');?>
</div>
</body>
</html>
[slider.php]
<?php
<!-- slider -->
<link rel="stylesheet" type="text/css" href="/normalize.css" />
<link rel="stylesheet" type="text/css" href="/demo.css" />
<link rel="stylesheet" type="text/css" href="/component.css" />
<script src="/slider.min.js"></script>
<script src="scattered_slider/classie.js"></script>
<div class="row header">
etc...
No, it's not possible to do that using only a single include/require.
The fastest solution would be to use two different files "slider_header.php" containing the javascript/css tags and "slider_body.php" containing the actual slider html.
Then you could include them like this:
<!DOCTYPE html>
<html>
<head>
<?php require( INCLUDES . 'slider_header.php');?>
</head>
<body>
<div id="slider">
<?php require( INCLUDES . 'slider_body.php');?>
</div>
</body>
</html>
Maybe you should consider using a proper template engine like Twig, but this requires a little bit more effort to set it up in the first place.
Related
I am trying to include header.php file inside my index.php file, both of them are in the same folder. However, it is not working.
Here's how I coded index.php file:
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="css/bootstrap.min.css" />
<link rel="stylesheet" href="css/main.css" />
<title>Bootstrap</title>
</head>
<body>
<? php include 'header.php'; ?>
<p>the header file should be above this line</p>
<div class="container">
<div class="row">
<div class="col-md-4">3</div>
<div class="col-md-4">2</div>
<div class="col-md-4">1</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="js/jquery.min.js"></script>
<script src="js/popper.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
And here's how I coded my header.php file:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<p>HEADER INCLUDED</p>
<p>HIP HIP HORRAH!</p>
</body>
</html>
I expected that when I open index.php in Chrome, I should see something like
HEADER INCLUDED
HIP HIP HORRAH!
the header file should be above this line
--- the column ---
But the first two lines are not showing up. I am confused what I am missing here.
Change
<? php include 'header.php'; ?>
To:
<?php include 'header.php'; ?>
As <?php means starting of PHP code. But, <? php does not mean anything and is a parse error.
As php is not a language construct like: for, foreach, while, etc...
I am having troubles with including a file that has an included file with some js script library's in it.
index.php
<?php
include 'header.php';
?>
<section>
<div>
<p>misc. html</p>
</div>
</section>
<?php
include ($_SERVER['DOCUMENT_ROOT'].'/footer.php');
echo 'test ='.$testVar;
?>
footer.php
<?php
include ($_SERVER['DOCUMENT_ROOT'].'/includes/footerscripts.php');
?>
</body>
</html>
/includes/footerscripts.php
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<?php
$testVar = 'test';
?>
In that scenario, the index file does have this in the source code when ran:
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
test =
The scripts are making their way there but are not being executed and the variables aren't coming through either.
When I change the footer.php to include those scripts and variable directly, the variable makes it to index.php, so do the scripts but they don't execute (or not on time?).
Any ideas guys?
I have a weird php5.ini file as I didn't know what all to put in it, if that makes a difference. The include file path seems okay as it still works for the most part.
Thanks,
Matt
Edit: Added the include ('header.php') which works fine and pulls in my header.
That file looks like:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="description" content="--">
<meta name="keywords" content="--">
<meta name="author" content="--">
<title>--</title>
<link rel="stylesheet" href="/css/mainstylesheet.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="icon" href="/images/favicon.ico">
</head>
<body>
<header class="nav-top" id="top" role="navigation">
<div class="container">
<div class="home-link">
<img src="/images/logo/logo-p.png" alt="plc" class="logo" />
</div>
<nav class="nav-main">
<ul>
<li>
Home
</li>
<li>
Portfolio
</li>
<li>
Contact
</li>
</ul>
</nav>
</div>
</header>
Edit: Guess I just needed my jquery loaded from the header of the document, because it works that way. I still can't carry variables from grandchild to parent but oh well
According to your example the index.php file is missing something:
<?php
include ($_SERVER['ROOT'].'/footer.php');
echo 'test ='.$testVar;
?>
I assume that you didn't post the whole file, since here the output hmtl here is not valid - missing html opening tag, etc. Could you please provide the whole and/or simplified index file?
Replace all instances of $_SERVER['ROOT'] with $_SERVER['DOCUMENT_ROOT'].
I'm trying to connect my PHP header from a file that is in a folder.
I'm attempting to do this by using:
<?php include("../header.php"); ?>
It works to bring the content in that file on the page, but its not applying the style.css file to the doc. Why?
Here is the contents of header.php:
<!DOCTYPE html>
<html>
<head>
<title>%TITLE%</title>
<link href='http://fonts.googleapis.com/css?family=Lilita+One' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Roboto:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" type="text/css" href="includes/normalize.css">
</head>
<body>
<header>
<div class="page-wrap">
<h2 id="logo">Some Company</h2>
<nav>
Home
Contact
Rentals
For Sale
</nav>
</div>
</header>
<section id="mainContent">
This is probably because its trying to reference the css at 'style.css' but try putting '../style.css'. It should allow the styles but it won't fix all your pages. In that case its better to use something like this:
<?php
// Set a constant with your url
define('BASE_URL', 'http://localhost:64411/TestingPHPStuff/');
echo 'Now use it in your html like this:';
?>
<link rel="stylesheet" type="text/css" href="<?=BASE_URL;?>style.css"/>
That way file location does not matter.
Basically though, you are trying to use a stylesheet that doesn't exist at that current level. Probably.
Edited
I changed it. If you include the 'http://' and the port ':64411' (for example you would use your port) then it should work. I just tested it and it only worked for me with both 'http://' and ':64411'.
I'm having a problem where my page automaticly puts two doublequotes outside php tags ("")
This happens as soon as the page contains <?php and ?> and I can't find out why.. Nor should it be my php settings because files in other directories work... except for this files somehow, here are the codes:
index.php
<html>
<?php include 'include/head.php' ?>
some data to test
</body>
</html>
head.php
<head>
<link rel="stylesheet" href="style/common.css.php">
<link rel="stylesheet" href="style/header.css.php">
</head>
<body>
<div id="test">test data</div>
and both of the CSS files are empty, I work with .css.php because I have been told that this could greatly improve loading speeds.. This all together gives me the following code in chrome, and Internet explorer:
<html>
<head></head>
<body>
"
"
<link rel="stylesheet" href="style/common.css.php">
<link rel="stylesheet" href="style/header.css.php">
<div id="test">test data</div>
"
somedatatotest"
</body>
</html>
EDIT: Real browser source:
<html>
<head>
<link rel="stylesheet" href="style/common.css.php">
<link rel="stylesheet" href="style/header.css.php">
</head>
<body>
<div id="test">test</div>
some data to test
Change your index.php file to this:
<html>
<head>
<?php include 'include/head.php' ?>
</head>
<body>
<div id="test">test data</div>
</body>
</html>
And your head.php file to this:
<link rel="stylesheet" href="style/common.css.php">
<link rel="stylesheet" href="style/header.css.php">
No questions, just do this. Promise it will solve your problem.
EDIT: Also, why is it common.css.php and not just common.css?
EDIT 2: Okay, do this then. You've got to atleast try some of the suggestions, otherwise no one will want to help you.
//index.php
<html>
<?php include 'include/head.php' ?>
<div id="test">test data</div>
</body>
</html>
//head.php
<head>
<link rel="stylesheet" href="style/common.css.php">
<link rel="stylesheet" href="style/header.css.php">
</head>
<body>
<div id="test">some head test data</div>
You're not passing a well formed HTML document to your browser, and the rendering engine is trying its best to show what it has got.
Since your HTML isn't well formed, you can't expect a predictable output. Fix your source, and the problem will be solved
Okay I found the problem, It seems that my Notepad++ settings were wrong, it would create files with the UTF-8 coding and ANSI enabled, but since I set my settings to UTF-8 (WITHOUT BOM) and ANSI coding ignored everything seems to be workign fine again.
I have a php file that doesn't (for now) use any php code, that holds code for the header and main menu that will be used across all pages. The CSS file has no effect, even though I've created a style class for h1. The text "TEST" shows up, but the style is not applied. How do I properly include the CSS file?
mainMenu.php
<!--This code is included within the <head> of each page, and the stylesheet for the header and main menu are included here-->
<link href="styles/headerMenu.css" ref="stylesheet" type="text/css">
<!--Header and menu code-->
<div>
<h1>TEST</h1>
</div>
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>untitled</title>
<?php include ('./includes/mainMenu.php') ?>
</head>
<body>
</body>
</html>
The CSS file is not found. Double check the link and correct it:
<link href="menu.css" rel="stylesheet" type="text/css">
^- REL not REF
Also to prevent additional problems, remove the start and end tags of <head> and <body> from the code. The way you output the HTML elements, you would create wrong HTML if you keep those tags. Remove them and your page will be valid HTML again.
index.php:
<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<title>untitled</title>
<?php include ('menu.php') ?>
</html>
Valid HTML has the benefit that you can run it through a validator to spot errors early.
I think it may be because you have got your menu appearing inside your <head> tag.
The CSS needs to go inbetween the <head> and </head> but the rest needs to be inside the <body> tag
<link href="styles/headerMenu.css" rel="stylesheet" type="text/css">
This must be at <HEAD></HEAD>
<div>
<h1>TEST</h1>
</div>
This must be at <BODY></BODY>
You have to separate this file into 2 files and include them in Head and in Body..
Don't include your HTML code in HEAD part. Only include CSS and JavaScript files in HEAD section. and you need to prefix the css or images path in some php file.
E.g.
create new php file with name "conn_path.php"
<?php
define('SITE_PATH',$_SERVER['DOCUMENT_ROOT'].'siteName/');
define('SITE_HTTP_PATH','http://localhost/'siteName/');
define('CSS_PATH','http://localhost/siteName/styles/');
define('IMAGE_PATH','http://localhost/siteName/images/');
?>
And then you path will be like below:-
mainMenu.php
<?php include "conn_path.php" ?>
<link href="<?php echo CSS_PATH ;?>headerMenu.css" rel="stylesheet" type="text/css">
It will help you in whole project…
Create a template file, with your essential (and re-used) html. Also with <html>, <head> and <body> tags and anything you must have in all pages – As your stylesheets and menu.
Then add a content section with a single variable.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>untitled</title>
<link href="styles/headerMenu.css" rel="stylesheet" type="text/css">
</head>
<body>
<!--Header and menu code-->
<div>
<h1>TEST</h1>
</div>
<?php echo $page_content; ?>
</body>
</html>
This way, any page content shoud be assigned to $page_content instead of echoed.