Reputation: 1224
Something basic that i don't understand:
I have header.php
with navigation bar for my site. Inside it, there's a <head>...</head>
section.
Now, in each other page of my site, I'm using require_once 'header.php'
so that each page will show the navigation bar. But, I need also specific <head>...</head>
sections to the different page.
For example, in page customers.php
, I'm using <script>...</script>
to include the jQuery library. I don't need to include it in other pages.
Now, searching the web I see that multiple head tags is wrong syntax.
So, how can anyone:
Upvotes: 1
Views: 136
Reputation: 9468
One possible solution.
header.php
.header.php
.If it is true, You print script or something. Something like this:
<!-- Fragment of header.php -->
<?php if ($i_want_jquery): ?>
<script ...>
...
</script>
<?php endif; ?>
On the other hand, a template may be a better solution.
Upvotes: 0
Reputation: 157991
You have to change your page structure and employ templates.
Instead of loading header at the top of the code, you have to do it at the bottom!
And page code should output not a word, but collect all data in variables.
And only after that output can be started by calling template.
A example layout is going to be like this:
First. page itself.
it outputs nothing but only gather required data and calls a template:
<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
$pagetitle = "Links to friend sites";
//etc
//and then call a template:
$tpl = "links.php";
include "template.php";
?>
Next, template.php
which is your main site template, consists of your header and footer:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>
And, finally, links.php
is the actual page template:
<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><a href="<?=$row['link']?>" target="_blank"><?=$row['name']?></a></li>
<? endforeach ?>
<ul>
easy, clean and maintainable.
there are many advantages in such approach:
Upvotes: 1
Reputation: 1861
in header.php you can type like this
<head>
<?php echo $script; ?>
</head>
then in your customers.php you can first assign the variable
$script = '<script>...</script>'
then
require_once 'header.php'
Upvotes: 0
Reputation: 6953
Here are some simple ways you can look at.
You can have jQuery on the pages that don't need it; once it's downloaded it will be cached so it still wont use more bandwidth.
You can move out the closing </head>
tag from header.php and close the
<head>
tag in the page that's including
header.php.
You can include javascript anywhere on a page, not only in the header.
You can also do something like this.
Before you do require_once 'header.php';
you put a variable called $jquery = true;
In your header.php file you check if $jquery
is set to true, if it is, you include jQuery.
Upvotes: 0