Forum Moderators: coopster
I have an index page that will load content based on the variable in the URL. This works perfectly fine. No issues there.
The problem is, I'm echo-ing $thisTitle variable in the <title> tags.
The $thisTitle variable is set in the home.php include file.
The index.php page calls the include and I thought the variable would get passed, but it doesn't.
I would like each of the includes to have unique variables that can be used or displayed on the main page. I figured keeping those variables within the include would be an easy solution, since the variables are unique to the include, instead of making a seperate file with variables associated to their includes. That would be another file to edit and would reduce its modularity.
So far from what I understand, one of the problems is that the home.php include is being called after the echo in the title.
Also, I know that the variables scope is only in the include. I know that global shouldn't be used and I don't want to get into a bad habit.
So is there a simple solution to this? Is there a better templating method then this?
For now I want to get this working with flat files. after this egg has been cracked I'll move into using it with a db.
I know I could be using some cms system, but the fact is I want to learn something and hopefully help some other beginners.
Thanks for your help ahead of time!
Here is the code simplified:
*-----[ index.php page ]-----*
[b]
<?php
// Enable output buffering
ob_start();
$root = $_SERVER[DOCUMENT_ROOT];
?><html><head>
<title><?php echo "$thisTitle";?></title>
</head>
<body>
<?php include($root.'/includes/nav.php');?>
<?php
// Define our array of allowed $_GET values
$pass = array('home','test');
// If the page is allowed, include it:
if (in_array($_GET['id'], $pass)) {
include ($root.'/pages/' . $_GET['id'] . '.php');
}
// If there is no $_GET['id'] defined, then serve the homepage:
elseif (!isset($_GET['id'])) {
include ($root.'/pages/home.php');
}
// If the page is not allowed, send them to an error page:
else {
// This send the 404 header
header("HTTP/1.0 404 Not Found");
// This includes the error page
include ($root.'/pages/error.php');
}
?>
</body>
</html>
[/b]
*-----[ home.php page ]-----*
[b]
<?php $thisTitle="Title Test";?><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
[/b]
Thanks!
Carlos
[edited by: coopster at 7:19 pm (utc) on Nov. 16, 2005]
[edit reason] removed url per TOS [webmasterworld.com] [/edit]
you have pointed out the error yourself
So far from what I understand, one of the problems is that the home.php include is being called after the echo in the title.
That's not just one of the problems, thats whole of the problem ;)
and as far as i know about "includes" , i think there is nothing as like "include level scope".
whenever a file is included as "code include" into another file, then virtually and essantially that all becomes just one file before it is sent to the parser or compiler (based upon the language). and when input it sent to them, at that point files dont remain different, they are just sent as source code chunk.
So if your file1 has some variables and file2 includes it and then calls its variables, there is no reason they wont work.
for example
//file1
<?
$var1="value of var1";
$var2="value of var2"
?>
//file2
<?
include "file1.php";
echo $var1;
echo "<br>";
echo $var2;
?>
it should return values of those variables. and AFAIK any variable in file1 doesnt even need to be declared as global variable to be accessable in file2.
Hope that Helps.
Kami
<edit>
Hey Sorry, forgot to say, Welcome to Webmasterworld
</edit>
That said, Answer to your question is "It Depends". it depends if the file you are including, has output statements (echo, printf) in it, if no, then its pretty simple you can take that include line up to the top but definitely your home.php file is definitely gonna have some output too, now if you take that include line up to top, then your final output will get disturbed (upside down) and if you let it remain there then you wont be able to echo that variable in the title. Although i would avoid this kinda situation but if there was no way out, i would do this
from your home.php take out your variable
$thisTitle
and define this variable and its value in a new file named like
variables.php
now in your file index.php
on the top you can include
include "variables.php";
and that's it . let rest of all your code remain the same as it is. Note that you can take any number of variables that you need, to variables.php file.
;)
So I guess the next solution would to be to change the way I'm templating.
I saw this in the forum unfortunately I don't remember the link to it, but it goes something along like:
<?php
$content = "<p>This is my content</p>";
$title = "$thisTitle";
include('mytemplate.php');
?>
Mentaly I can see this working, but now I get confused on how to make it work with the variables from the url.
Thanks Again!
Carlos
but now I get confused on how to make it work with the variables from the url
Pardon, i couldnt get it.
P.S: in a "code include" , GET and POST variables also remain accessable throghout the code as the others about which we just discussed.
// Define our array of allowed $_GET values
$pass = array('home');// If the page is allowed, include it:
if (in_array($_GET['id'], $pass)) {
include ($root.'/pages/' . $_GET['id'] . '.php');
}// If there is no $_GET['id'] defined, then serve the homepage:
elseif (!isset($_GET['id'])) {
include ($root.'/pages/home.php');
}// If the page is not allowed, send them to an error page:
else {
// This send the 404 header
header("HTTP/1.0 404 Not Found");
// This includes the error page
include ($root.'/pages/error.php');
}
I thought that some how, I could take the code above and make it equal to the variable $content, and then use that variable in the mytemplate.php, like so:
<?php
$content = "<p>This is my content</p>";
$title = "$thisTitle";
include('mytemplate.php');
?>
But you mentioned its not ideal to put all that html code into the variable $content. Why is it not ideal and how should I create this.
Its ok for me to start from scratch at this point, since I haven't commeted to any templating method yet.
And gain thanks for your help Kami.
----index.php----
<?
$root = $_SERVER[DOCUMENT_ROOT];
$pass = array('home');
$page="";if (in_array($_GET['id'], $pass))
{
$page=$_GET['id'].".php";
}elseif (!isset($_GET['id']))
{
$page="home.php";
}else
{
$page="error.php";
}include ($root."/pages/$page");
include ("mytemplate.php");?>
---mytemplate.php---
<html><head><title><? echo $pageTitle;?></title></head>
<body>
<? echo $pageContent;?>
</body>
</html>
//you can definitely have any html and any number of variables.
----home.php-----
<?
$pageTitle="Home Page";
$pageContent="Your content for home.php page";
?>
----anotherpage.php-----
<?
$pageTitle="Another Page";
$pageContent="Your content for anotherpage.php page";
?>
----error.php-----
<?
header("HTTP/1.0 404 Not Found");
$pageTitle="404";
$pageContent="404";
?>
;)