How to pass arguments to an included file? - php

I'm trying to make the whole <head> section its own include file. One drawback is the title and description and keyword will be the same; I can't figure out how to pass arguments to the include file.
So here is the code:
index.php
<?php include("header.php?header=aaaaaaaaaaaaaaaaaaaaa"); ?>
<body>
.....
..
.
header.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="shortcut icon" href="favicon.ico">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content=" <?php $_GET["header"]?> " >
<meta name="Description" content=" <?php $_GET["header"]?> " >
<title> <?php $_GET["header"]?> </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
Obviously this doesn't work; how can I pass arguments to an included file?

Include has the scope of the line it's called from.
If you don't want to create new global variables, you can wrap include() with a function:
function includeHeader($title) {
include("inc/header.php");
}
$title will be defined in the included code whenever you call includeHeader with a value, for example includeHeader('My Fancy Title').
If you want to pass more than one variable you can always pass an array instead of a string.
Let's create a generic function:
function includeFile($file, $variables) {
include($file);
}
Voila!
Using extract makes it even neater:
function includeFileWithVariables($fileName, $variables) {
extract($variables);
include($fileName);
}
Now you can do:
includeFileWithVariables("header.php", array(
'keywords'=> "Potato, Tomato, Toothpaste",
'title'=> "Hello World"
));
Knowing that it will cause variables $keywords and $title to be defined in the scope of the included code.

index.php:
<?php
$my_header = 'aaaaaaaaaaaaaaaaaaaa';
include 'header.php';
?>
and header.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="shortcut icon" href="favicon.ico" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content=" <?php echo $my_header ?> " />
<meta name="Description" content=" <?php echo $my_header ?> " />
<title> <?php echo $my_header ?> </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
It's not an ideal solution, but I understand it's your first steps in php.
PS. Your Doctype doesn't match the code. I've adjusted your header html to be XHTML.

You can't pass arguments to include, but it has access to all variables you've already set. From the include documentation:
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward.
Thus:
index.php
<?php
$header = 'aaaaaaaaaaaaaaaaaaaaa';
include("header.php");
?>
header.php
<title> <?php echo $header; ?> </title>

Well marc, when you are using include, you can simply just set up a variable to use:
<?php
$var = "Testing";
include("header.php");
?>
In your header file:
<?php
echo $var;
?>
Allow your previously defined variables are usable in any include you have.

you are over thinking it
<?php
$header = "aaaaaaaaaaaaaaaaa";
include("header.php");
?>
::EDIT::
Decided I would add value
The included file will gain the scope of where you included it. So if you include a file INSIDE a function:
<?php
$get_me = "yes";
function haha()
{
include("file.php");
}
haha();
// And file.php looks like
echo $get_me; // notice + blank
?>
More over, you include the same file more than once to great effect.
<?php
$output = "this";
include("cool_box.php");
$output = "will";
include("cool_box.php");
$output = "work";
include("cool_box.php");
?>
And even use this to load templates that become part of a method in a class. So you can do something like:
<?php
class template
{
private $name;
function __construct($name)
{
$this->name = preg_replace("/[^a-zA-Z0-9]/", "", $name);
}
function output(array $vars)
{
include($this->name.".php"); // Where $vars is an expected array of possible data
}
}
$head = new template("header");
$body = new template("body");
$head->output();
$head->output(array("content" => "this is a cool page"));
?>

defining a variable as a pseudo-argument/workaround before an include() - as recommended by many - is a bad idea. it introduces a variable in the global scope. define a function in the included file instead to catch the arguments u want to pass.

This is good approach. I however would do it a bit inside out. Define a layout, a wrapper for your webpage and include your content file into it:
layout.phtml
<html>
<head>
... your headers go here
</head>
<body>
<? include $content ?>
</body>
</html>
Your content template file can look like this e.g.
content.phtml
<h1>hello world</h1>
<p>My name is <?= $name ?></p>
Then, you would have your main script (index) that will handle logic, connects to database etc.
index.php
$content = 'content.phtml';
$name = 'Marc'; //Can be pulled from database
include 'layout.phtml';
This way, you can nicely separate business logic and presentation. And it can help you cut repetitive code for parts of page like logo or navigation which are repeated on the whole site.

If you include a file it is just like inserting that code into the parent file. You could simply do this:
<?php
$parameter = "Hello World";
include("header.php");
?>
and then in the header.php
<?php
$parameter = isset($parameter) ? $parameter : "Default Text";
// Use accordingly
?>
I used the isset() method to verify that it has a value already and is instantiated.

I noticed nobody suggested using a template engine. I came looking here because for the project I'm working with, a template engine isn't possible and that might be your situation too, however I thought it might be worth mentioning these: Twig (my preferred engine) and Smarty both allow passing specific variables to includes.
I highly recommend the use of a template engine whenever possible, as it simplifies your front end code, adds a layer of abstraction between your front end and back end, and both Twig and Smarty automatically clean the variables you pass to them which helps mitigate XSS attacks.
Twig Example
header.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="shortcut icon" href="favicon.ico">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content="{{ header }}" >
<meta name="Description" content="{{ header }}" >
<title> {{ header }} </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
index.html
{% include 'header.html' with { 'header' : '<script>alert("this shouldnt work")</script>'} only %}
Body Text
{% include 'footer.html' %}
Smarty Example
header.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="shortcut icon" href="favicon.ico">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content="{$header}" >
<meta name="Description" content="{$header}" >
<title> {$header} </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
index.tpl
{include 'header.tpl' header='<script>alert("this shouldnt work")</script>'}
Body Text
{include 'footer.tpl'}

Related

php ( function.php) file function call in another file

I have a page function.php and call this function in another page view.php but function not call properly
Here is the function.php code
<?php
function head1()
{
global $title;
<html xmlns="http://www.w3.org/1999/xhtml">
<header>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>WhiteFlower| florida web design</title>
</header>
}
head1();
?>
Second php file
<?php
include "function.php";
head1();
?>
This is code of another file view.php
I am new in php so I cannot understand how it works properly
This page is showing error because you are have error in your function you should echo your html or return them you are writing like html in php block
your code
<?php
function head1()
{
global $title;
<html xmlns="http://www.w3.org/1999/xhtml">
<header>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>WhiteFlower| florida web design</title>
</header>
}
?>
Replace with this
<?php
function head1()
{
global $title;
echo '<html xmlns="http://www.w3.org/1999/xhtml">
<header>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>WhiteFlower| florida web design</title>
</header>';
}
?>
please let me know if your error is resolved or not and also attach a snapshot of error we will figure it out

How can I give a title for each of my pages?

I would like to give a title to each of my pages, but all my pages are linked to my index.php:
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Test</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php include("top_bar.php");?>
<?php include("header.php");?>
<?php include("container.php");?>
<?php include("footer.php");?>
</body>
</html>
Here is how my site is: http://prntscr.com/47nn7h
All my pages have the title I put for index.php, but how to add a title to a specific page (example, when I go to the page members.php)?
members.php:
<?php include "index.php";?>
Thanks.
Replace the existing <title> tag with this.
<title><?php echo $pagetitle; ?> </title>
in your <head> block.
Make sure that $pagetitle actually contains the desired title before you emit the tag. It's not clear from your question where these titles are coming from - you'll probably need some PHP right at the top of the page to set all this up.
.
You could use this method and add the
$pageTitle = 'Title of Page';
to your content page (i.e. member page)

Using PHP include to include a file with it's own included file

I am trying to load a projects.php file into a higher-level index.php file and want to maintain all the .js .css and any further nested (in .js files) .php files, that are declared in the projects.php file.
My index.php file goes like:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>BLST</title>
</head>
<body>
<?php $projectsPage = "/tables/php/projects.php"?>
<li>Projects</li>
<?php if(is_null($_GET["page"])) {
$page = "";
}else{
$page = $_GET["page"];
}
include($page);
?>
</body>
</html>
My projects.php file is
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Projects</title>
<style type="text/css">
#import "css/test.css";
#import "http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.23/themes/smoothness/jquery-ui.css";
</style>
<script type="text/javascript" language="javascript" charset="utf-8" src="js/jquery.min.js"></script>
</head>
The issue is that none of the files in projects.php are found. This is because the browser is looking to my main directory now (where index.php is).
The test.css is in "tables/php/css/test.css" and what about the location of projects.php and index.php.
I think the answer to these will solve the problem.

set page title and meta tags from another page in dreamweaver

I'm developing a php web application using Dreamweaver CS5. When I create a new page, Dreamweaver automatically adds the following code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
What I'm trying to do is to take this code and put it in a file, say 'head.php', and then I want to import this head.php file to all the pages I create using
<?php include("head.php"); ?>
If I do this, it means that I'm gonna have the same page title for every page because the code <title>Untitled Document</title> is included in head.php.
So is there a way for me to send the page title via a variable from the new pages I create and then set it on the head.php document. So if I had a customer.php page, it would look something like this.
$pageTitle = "Customer List";
<?php include("head.php"); ?>
Then pass $pageTitle to the the head.php
Inside your header.php
function header($title)
{
echo '<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>.$title.</title>
</head>';
}
then you include your head.php
< ?php include("head.php"); ?>
and finally, instead the < head >< /head > tags in your main file your call the function to create the title.
<?php header('This is my Awesome Page'); ?>
Or still
<?php
$pageTitle = "My Title";
header($pageTitle);
?>
This should work for you.

Yii register CSS file, to be the last one (after assets)

Is there a way in Yii to register js or css files to load them after those loaded by the assets manager.
I use a css file to override styles from some Yii extensions, but Yii includes my file before the assets generated by the extension.
I know I can change the extension to remove the css files from the assets manager and add them manually with registerCssFile but that's not the way I want to do this.
Here's what I have :
<head>
...
<link rel="stylesheet" type="text/css" href="/css/MY_CSS.css" />
...
<link rel="stylesheet" type="text/css" href="/assets/8e838803/css/EXTENSION_CSS.css" />
...
</head>
Here's what I want:
<head>
...
<link rel="stylesheet" type="text/css" href="/assets/8e838803/css/EXTENSION_CSS.css" />
...
<link rel="stylesheet" type="text/css" href="/css/MY_CSS.css" /> <!-- the last one -->
</head>
Any help would be appreciated!
Registered assets by CClientScript are always added before the title
In your case you should add your CSS after the title, to guarantee the order you want
In Yii2, a good way to accomplish this is by using the 'depends' option on registerCssFile as shown in the documentation
$this->registerCssFile("http://example.com/css/themes/black-and-white.css", [
'depends' => [BootstrapAsset::className()],
'media' => 'print',
], 'css-print-theme');
//example 1
class someWidget extends YourExtendsionClassName
{
public function init()
{
parent::init();
Yii::app()->getClientScript()->registerCssFile('/path/to/your/css/file.css');
}
// or
public function run()
{
Yii::app()->getClientScript()->registerCssFile('/path/to/your/css/file.css');
parent::run();
}
}
//example 2
//Layouts file main.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
<?php Yii::app()->getClientScript()->registerCssFile('/path/to/your/css/file.css'); ?>
</head>
In your main.php layouts, find tag, put this line code:
Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/path/to/css/file');
http://www.codexamples.com/98/how-to-include-javascript-css-in-yii/

Categories