View Full Version : Resolved echo problems w/ included pages
Hello all,
I've been trying to define variables (e.g., meta tags) on a page, then include another page (e.g., a <head> section) that echos them.
I've had success doing it all on one page, like so:
<?php $page['keywords'] = "these, are, my, keywords" ?>
<html>
<head>
meta name="keywords" content="<?php echo $page['keywords'] ?>"
</head>
etc...
As expected, I get this:
meta name="keywords" content="these, are, my, keywords"
However, when I try to separate the pages it doesn't work out so well.
webpage.php:
<?php $page['keywords'] = "these, are, my, keywords";
include ("path/to/htmlhead.php"); ?>
<body>
...page content...
</body>
</html>
htmlhead.php:
<html>
<head>
meta name="keywords" content="<?php echo $page['keywords'] ?>"
</head>
When I look at the source of webpage.php, I just get this in the <head> section:
meta name="keywords" content=""
The page/site I'm working on is here (http://www.custom-anything.com/reel/).
Can anyone help me out? Thanks in advance, guys.
?foru
05-17-2009, 05:11 AM
I just recently did this on a project I was working on. I used a database to pull the info, but I modified the code for static content.
This will be your htmlhead.php page (add additional entries of course to suit your needs)
<?php
$page = $_SERVER['SCRIPT_NAME']; $ishome=isset($_GET['page'])?($_GET['page']=="about"?true:false):(preg_match("/about\.php/", $page)==1?true:false); if ($ishome) echo "<title>Your About page title here</title>\n<meta name=\"description\" content=\"Your page description will go here...\">\n<meta name=\"keywords\" content=\"keyword1, keyword2, keyword3, keyword4\">\n";
?>
the \n just creates another line so your html source code is stacked on top of each other as if you were writing it all manually.
Some portions of my project had query strings like index.php?page=about
The above will check for that along with a static page about.php so you should be covered either way.
Thanks.
thing is, I want to be able to define the page title, description, keywords, etc. on the individual page, not in the htmlhead page. This way, I only need one htmlhead page that will be written into each individual webpage - but I still get page-specific meta information.
If your php does that, then thanks again, but it's a little beyond my learning curve atm.
Do you know of a way to correct what I'm doing in the first post? It seems to me that it should be able to work. Any ideas?
?foru
05-17-2009, 10:17 PM
traq,
thing is, I want to be able to define the page title, description, keywords, etc. on the individual page, not in the htmlhead page. This way, I only need one htmlhead page that will be written into each individual webpage - but I still get page-specific meta information.
If I'm reading this right, that would be a little more work for you to change items in page1 page2 page3 and so on rather than making all your page changes in one single file
htmlhead.php. (I didn't test the code you were trying to use since this might make things a little easier for you.)
I'm sorry for not explaining it a little better. This should do what you need. In all your pages you will only need to include htmlhead.php like you were doing
<head>
<?php include ("path/to/htmlhead.php"); ?>
<head>
then htmlhead.php will contain all the meta data for all your pages. htmlhead.php will check against the page name of the site because it's included in your mainpage (like above) and will deliver the correct meta data.
htmlhead.php (I changed the code from what was previously posted to make it a little easier to see the data you would need to change for each page.)
<?php
//index page - notice preg_match("/index\.php/ - change to your page name
$page = $_SERVER['SCRIPT_NAME']; $ishome=isset($_GET['page'])?($_GET['page']=="index"?true:false):(preg_match("/index\.php/", $page)==1?true:false); if ($ishome)
echo '<title>Your homepage title here</title>';
echo "\n";
echo '<meta name="description" content="Your homepage description will go here...">';
echo "\n";
echo '<meta name="keywords" content="keyword1, keyword2, keyword3, keyword4">';
//about page - notice preg_match("/about\.php/ - change to your page name
$page = $_SERVER['SCRIPT_NAME']; $ishome=isset($_GET['page'])?($_GET['page']=="about"?true:false):(preg_match("/about\.php/", $page)==1?true:false); if ($ishome)
echo '<title>Your about page title here</title>';
echo "\n";
echo '<meta name="description" content="Your about page description will go here...">';
echo "\n";
echo '<meta name="keywords" content="keyword1, keyword2, keyword3, keyword4">';
//abc page - notice preg_match("/abc\.php/ - change to your page name
$page = $_SERVER['SCRIPT_NAME']; $ishome=isset($_GET['page'])?($_GET['page']=="abc"?true:false):(preg_match("/abc\.php/", $page)==1?true:false); if ($ishome)
echo '<title>Your abc page title here</title>';
echo "\n";
echo '<meta name="description" content="Your abc page description will go here...">';
echo "\n";
echo '<meta name="keywords" content="keyword1, keyword2, keyword3, keyword4">';
// keep adding the same blocks of code as above to create meta tags for as many pages as you need
?>
for instance...the index page from the code above will output
<title>Your homepage title here</title>
<meta name="description" content="Your homepage description will go here...">
<meta name="keywords" content="keyword1, keyword2, keyword3, keyword4">
the echo "\n"; in the php code just breaks to a new line so it displays like the example above and isn't one long line of code in the browser.
htmlhead.php will detect when someone visits yoursite.com/abc.php and it will display the meta data you specify in those lines of code for the abc.php page. (preg_match("/abc\.php/", $page) assumes that your files are at the top level...if not change it to (preg_match("/yourfolder/abc\.php/", $page) or whatever to suit your needs.
Anytime you want to add a page to your site, just include the htmlhead.php file in that page and add a new block of code to htmlhead.php for your new page meta data.
Let me know if you run into any issues, or have questions about the php code, but that should make things easier to update site wide.
hmmm... cool. I think I'll try that. I'll let you know how it works.
however (at the risk of beating a dead horse), I am still curious about my original plan. Shouldn't a variable defined on a page be available to code in an included file on that page?
problem:
<?php
// /index
$page = $_SERVER['SCRIPT_NAME'];
$ishome=isset($_GET['page'])?($_GET['page']=="index"?true:false):(preg_match("/index\.php/", $page)==1?true:false);
if ($ishome)
echo "\n";
echo '<title>Reel Lumber Service | speculative website</title>';
echo "\n";
echo '<meta name="description" content="Wholesale Hardwood and Pine Lumber, Mouldings, Plywood, Custom Milling">';
echo "\n";
echo '<meta name="keywords" content="adrian testa-avila, custom-anything, custom, web, web design, web site, html, css, php, javascript">';
echo "\n";
echo '<meta name="robots" content="noindex, nofollow">';
// /demo/moulding
$page = $_SERVER['SCRIPT_NAME'];
$ishome=isset($_GET['page'])?($_GET['page']=="moulding"?true:false):(preg_match("/demo/moulding\.php/", $page)==1?true:false);
if ($ishome)
echo "\n";
echo '<title>Reel Lumber Service | moulding list demo</title>';
echo "\n";
echo '<meta name="description" content="sortable dynamically generated moulding catalog">';
echo "\n";
echo '<meta name="keywords" content="adrian testa-avila, custom-anything, custom, web, web design, web site, html, css, php, javascript">';
echo "\n";
echo '<meta name="robots" content="noindex, nofollow">';
?>
generates this:
<title>Reel Lumber Service | speculative website</title>
<meta name="description" content="Wholesale Hardwood and Pine Lumber, Mouldings, Plywood, Custom Milling">
<meta name="keywords" content="adrian testa-avila, custom-anything, custom, web, web design, web site, html, css, php, javascript">
<meta name="robots" content="noindex, nofollow"><title>Reel Lumber Service | moulding list demo</title>
<meta name="description" content="sortable dynamically generated moulding catalog">
<meta name="keywords" content="adrian testa-avila, custom-anything, custom, web, web design, web site, html, css, php, javascript">
<meta name="robots" content="noindex, nofollow">
on both the index page (http://www.custom-anything.com/reel/index.php) and the moulding page (http://www.custom-anything.com/reel/demo/moulding.php).
Should this be an if{} / elseif{} -type statement?
I tried this:
<?php
echo $_SERVER['PHP_SELF'];
if ($_SERVER['PHP_SELF'] == "/reel/index\.php"){
echo "\n";
echo '<title>Reel Lumber Service | speculative website</title>';
echo "\n";
echo '<meta name="description" content="Wholesale Hardwood and Pine Lumber, Mouldings, Plywood, Custom Milling">';
echo "\n";
echo '<meta name="keywords" content="adrian testa-avila, custom-anything, custom, web, web design, web site, html, css, php, javascript">';
echo "\n";
echo '<meta name="robots" content="noindex, nofollow">';
}
elseif ($_SERVER['PHP_SELF'] == "/reel/demo/moulding\.php"){
echo "\n";
echo '<title>Reel Lumber Service | moulding list demo</title>';
echo "\n";
echo '<meta name="description" content="sortable dynamically generated moulding catalog">';
echo "\n";
echo '<meta name="keywords" content="adrian testa-avila, custom-anything, custom, web, web design, web site, html, css, php, javascript">';
echo "\n";
echo '<meta name="robots" content="noindex, nofollow">';
}
else {
echo "\n";
echo '<title>Reel Lumber Service | some other page</title>';
echo "\n";
echo '<meta name="description" content="Wholesale Hardwood and Pine Lumber, Mouldings, Plywood, Custom Milling">';
echo "\n";
echo '<meta name="keywords" content="adrian testa-avila, custom-anything, custom, web, web design, web site, html, css, php, javascript">';
echo "\n";
echo '<meta name="robots" content="noindex, nofollow">';
}
?>
which is, at least, selective: it returns the "else{}" option. I added the "echo $_SERVER['PHP_SELF']" at the top to see why I wasn't getting the /reel/index.php option, and I got "/reel/library/head.php" - meaning the script is executing from its original location, and not from the file where it is being included (/reel/index.php). That would also explain why my original method wasn't working; the code that wrote the meta tags was done running by the time the $page[] array became available.
suggestions?
got it to work. head.php page:
$page = $_GET['page'];
if ($page == "index"){
//index meta tags
}
if ($page == "demoMoulding"){
//moulding page meta tags
}
// and so forth
webpages:
include "/path/to/head.php?page=index"
still very interested in getting my previous idea to work, as I had several applications in mind for it.
(besides, it still seems like it ought to work!)
How can I get the variables on a page to be made available to code on other, included pages?
thanks for your help!!
?foru
05-19-2009, 03:00 AM
When I used the code I was using query strings like you posted index.php?page=demoMoulding I attempted several different code changes without the query strings and I couldn't get anything to work like you described since it was using the htmlhead.php as the SCRIPT_NAME or PHP_SELF. I was going to suggest getting it from the URL or I have code using a database, but that may be a little too much for what you're needing. With there being multiple ways of doing things in PHP...yet another option which is like your general idea in the beginning...it also answers your question about how to include variables from one page into another.
I've tested the following and it works
webpage.php
<?php
//place htmlhead.php at the top to prepare the variables
//the code to follow will interpret those variables and display them
include("htmlhead.php"); ?>
<title><?php echo $page_title; ?></title>
<meta name="description" content="<?php echo $page_description; ?>">
<meta name="keywords"<?php echo $page_keywords; ?>">
htmlhead.php
<?php
$page_title="Your page title";
$page_description="Your page description would go here...";
$page_keywords="keyword1, keyword2, keyword3, keyword4";
?>
In webpage.php above if you include htmlhead.php at the end it won't work because your variables will be shown before the included page that contains their values.
I think the problem when you were testing things orignally in 2 pages was $page['keywords'] [ ] are mainly used for items in an array or other uses but not much with variables and PHP didn't know quite to handle that from another page.
You could expand on the example above and create additional variables with different names in htmlhead.php and in webpage2.php you can echo $page_title2; etc. or whatever names would help you keep track of them. That essentially would allow you to only need to update htmlhead.php if anything changed in any existing pages and as long as any new pages included htmlhead.php before new variabls that will work the same way.
Okay:
That works, and I also got this to work (see here (http://www.custom-anything.com/sand/testpage.php)):
head.php:
<title><?php echo $page_title; ?></title>
<meta name="description" content="<?php echo $page_description; ?>">
<meta name="keywords"<?php echo $page_keywords; ?>">
webpage.php:
<?php
$page_title="Your page title";
$page_description="Your page description would go here...";
$page_keywords="keyword1, keyword2, keyword3, keyword4";
include ("/path/to/head.php");
?>
But when I do the same thing in my actual pages, it doesn't work at all.
Actual code of head.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title><?php echo $pageTitle ?></title>
<meta name="description" content="<?php echo $pageDescription; ?>">
<meta name="keywords" content="<?php echo $pageKeywords; ?>">
<meta name="robots" content="<?php echo $pageRobots; ?>">
</head>
Actual code of webpage (actually "index.php"):
<?php
$pageTitle="Reel Lumber Service | Wholesale Hardwood & Pine Lumber, Mouldings, Plywood, Custom Milling";
$pageDescription = "speculative website build";
$pageKeywords = "adrian testa-avila, custom-anything, custom, web, web design, web site, html, css, php, javascript";
$pageRobots = "noindex, nofollow";
require("http://www.custom-anything.com/reel/library/head.php");
?>
//regular html page content...
I'm at a loss as to what the difference is.
p.s. Incidentally, the reason I originally wanted to code the meta data to the individual page was so when I created new pages for the site, I could define the meta data in the new page and not have to update a second file (the head.php page) for it.
?foru
05-20-2009, 02:00 AM
Normally when I "include" a file it contains variables that my main page is using so it seems a little odd that your include file is just echoing the value. Sometimes scripts will be built to include a head.php file in the main page but within that head.php there is another included file like a config.php file that references all the site details and such. ie. a content management system may use something similar or the head info is pulled from a database.
It seems logical but I can't offer an explanation on why only 1 set of the code in your last post works, and why the 2nd setup doesn't. Query strings(getting info from URL)(simple switch statement could even be applied) or a database might be the only option to update the meta data dynamically on all your static pages.
The head data will pretty much remain the same on each page so if you really want to keep the meta tags and echo them out in a seperate file it might be just as easy to do something like this...
<?php
$pageTitle="Reel Lumber Service | Wholesale Hardwood & Pine Lumber, Mouldings, Plywood, Custom Milling";
$pageDescription = "speculative website build";
$pageKeywords = "adrian testa-avila, custom-anything, custom, web, web design, web site, html, css, php, javascript";
$pageRobots = "noindex, nofollow";
//end of content changes for each page
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<?php include ("/path/to/head.php"); //head only echos the meta tags ?>
<!-- if you wanted to add other meta info on a specific page you could do it here -->
</head>
<body>
</body>
</html>
Create all your static pages in a similar format and change your meta info at the top, and the included file head.php is seperate and echos the common meta info. Anytime you create a new file you update the php info at the top in that one file which is really the same as coding it in html but using php variables and echoing them out.
I've got it figured out. it had to do with my webhost and their default settings for PHP. For example, I didn't actually have php5 until yesterday, when I finally finished beating the .htaccess file over tech support heads. THEN, I had to change all my absolute paths (which used to be the only way it would work) to
$_SERVER['DOCUMENT_ROOT']."/path/to/whatever.php" because they have all the php5 defaults screwed with also. Now that I've got this figured out, we'll see about my SQLite problems. I'm getting *different* error messages now, at least that's something!
Thanks a bunch for sticking with this, ?foru. You've been very helpful.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.