Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: echo problems w/ included pages

  1. #1
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default 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:
    Code:
    <?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:
    Code:
    <?php $page['keywords'] = "these, are, my, keywords";
    include ("path/to/htmlhead.php"); ?>
    <body>
    ...page content...
    </body>
    </html>
    htmlhead.php:
    Code:
    <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.
    Can anyone help me out? Thanks in advance, guys.
    Last edited by traq; 05-21-2009 at 12:57 AM.

  2. #2
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    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 Code:
    <?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.

  3. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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?

  4. #4
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    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
    PHP Code:
    <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 Code:
    <?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
    Code:
    <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.

  5. The Following User Says Thank You to ?foru For This Useful Post:

    traq (05-18-2009)

  6. #5
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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?

  7. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    problem:
    Code:
    <?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 and the moulding page.

    Should this be an if{} / elseif{} -type statement?

  8. #7
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I tried this:
    Code:
    <?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?

  9. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    got it to work. head.php page:
    PHP Code:
    $page $_GET['page'];
    if (
    $page == "index"){
    //index meta tags
    }
    if (
    $page == "demoMoulding"){
    //moulding page meta tags
    }
    // and so forth 
    webpages:
    PHP Code:
     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!!
    Last edited by traq; 05-19-2009 at 12:59 AM.

  10. #9
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    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 Code:
    <?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 Code:
    <?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.
    Last edited by ?foru; 05-19-2009 at 03:10 AM.

  11. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Okay:
    That works, and I also got this to work (see here):
    head.php:
    PHP Code:
    <title><?php echo $page_title?></title>
    <meta name="description" content="<?php echo $page_description?>">
    <meta name="keywords"<?php echo $page_keywords?>">
    webpage.php:
    PHP Code:
    <?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:
    PHP Code:
    <!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 Code:
    <?php 
    $pageTitle
    ="Reel Lumber Service | Wholesale Hardwood &amp; 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.
    Last edited by traq; 05-19-2009 at 03:46 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •