SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Putting Echo Statements in Includes

    How do you put an echo statement - like $myname = 'Alaska'; - in an include page, then include it in the middle of a head section on another page?

    Below is a head section for one of my web pages, "Alaska."

    PHP Code:
    <head>
    <?php
    $myname 
    'Alaska';
    $mycode 'ak';
    $type 'state'// Chooses Article Include (further defined by $mycode)
    $myregion 'North';
    $reg 'north';

    $mycountry 'United States';
    $mycontinent 'North America';
    $mysection 'World';
    $nat 'us';
    $cont 'na';
    $sec 'world';
    $mylink '<span class="navhere">' $myname '</span>';
    $countrylink '<a href="../index.php">' $mynation '</a> &gt; ';
    $continentlink '<a href="../../index.php">' $mycontinent '</a> &gt; ';
    $sectionlink '<a href="../../../index.php">' $mysection '</a> &gt; ';

    $path '../../../../';
    $mynickname 'Land of the Midnight Sun';
    $mywebring '/world/na/us/ak/';
    $inctitle 'title';
    $inctop 'top';
    $incbanner 'banner';
    $incnavmenu 'navmenu';
    $incsupport 'support';
    $incwebring 'webring';
    $incpolad 'polad';
    $incfooter 'footer';
    include (
    $path."a1/inc/head.php")
    ?>
    </head>
    The last line - include ($path."a1/inc/head.php") - links to an include that features all the metatags, links to style sheets and JavaScript functions, etc.

    There are many functions in the center of the head section I posted that will be shared by all 50 states, as follows...

    PHP Code:
    $mycountry 'United States';
    $mycontinent 'North America';
    $mysection 'World';
    $nat 'us';
    $cont 'na';
    $sec 'world';
    $mylink '<span class="navhere">' $myname '</span>';
    $countrylink '<a href="../index.php">' $mynation '</a> &gt; ';
    $continentlink '<a href="../../index.php">' $mycontinent '</a> &gt; ';
    $sectionlink '<a href="../../../index.php">' $mysection '</a> &gt; ';
    $inctitle 'title';
    $inctop 'top';
    $incbanner 'banner';
    $incnavmenu 'navmenu';
    $incsupport 'support';
    $incwebring 'webring';
    $incpolad 'polad';
    $incfooter 'footer'
    So I'd like to put all these shared values on another include. Thus, my revised head section for Alaska might look something like this:

    PHP Code:
    <head>
    <?php
    $myname 
    'Alaska';
    $mycode 'ak';
    $type 'state'// Chooses Article Include (further defined by $mycode)
    $myregion 'North';
    $reg 'north';

    include (
    $path."a1/inc/NewHeadSection.php");

    $path '../../../../';
    include (
    $path."a1/inc/head.php")
    ?>
    </head>
    I could even eliminate the last line - include ($path."a1/inc/head.php") - by including that within the NewHeadSection.php include. Unfortunately, I can't seem to make NewHeadSection.php work.

    I tried this approach...

    PHP Code:
    <?php
    $todayDate 
    date("m-d-Y");
    ?>
    $mycountry = 'United States';
    $mycontinent = 'North America';
    $mysection = 'World';
    and this approach...

    PHP Code:
    <?php
    $todayDate 
    date("m-d-Y");
    ' . $mycountry . ' United States;
    ' . $mycontinent . ' North America;
    ?>
    but neither one works. What am I doing wrong?

  2. #2
    SitePoint Zealot colinr's Avatar
    Join Date
    Aug 2003
    Location
    san francisco, ca
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    first of all, in order to seperate code from content, I find its better to put all PHP not REQUIRED to be within the <html> tags, before the <html> tags (and therefore not within the head tags)

    As for includes... image includes as copy and paste done by PHP, whatever is include()'ed, is (as far as the PHP parser), there... nothing special happens.



    In this example,
    PHP Code:
    <?php 
    $todayDate 
    date("m-d-Y"); 
    ?> 
    $mycountry = 'United States'; 
    $mycontinent = 'North America'; 
    $mysection = 'World';
    the last three lines are not interpreted by php because they are not surrounded by php tags ( <?php ?> )

    PHP Code:
    <?php 
    $todayDate 
    date("m-d-Y"); 
    $mycountry 'United States'
    $mycontinent 'North America'
    $mysection 'World';
    ?>
    would be parsed...
    hint: pay attention to the nice color-coding features of php, they let you have a bit of insight into how the parser will see things.....



    Overall, my suggestion would be to have 2-3 includes:
    1st include: State Specific vars.. ie. ak_info.inc.php
    2nd include: Country Specific... ie. us_info.inc.php
    3rd include: Whatever don't fit into the first two (if needed)

    for the php calling them, you can do this:
    PHP Code:
    <?php 
    if (!IsSet($country)){  // if the $country code isn't set, set it to the default, 'us'
      
    $country 'us';   
    }
    if (!IsSet(
    $state)){ // if the $state code isn't set, set it to the default, 'ak'
      
    $state 'ak'
    }
    include(
    $path."whatever/foobar/" $state "_info.inc.php");
    include(
    $path."whatever/foobar/" $country "_info.inc.php");
    ?>
    Hope that helps
    Web Slave

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by colinr
    first of all, in order to seperate code from content, I find its better to put all PHP not REQUIRED to be within the <html> tags, before the <html> tags (and therefore not within the head tags)
    Like this?:

    PHP Code:
    <?php
    $path 
    '../../../../';
    $myname 'Alaska';
    $mycode 'ak';
    $mynickname 'Land of the Midnight Sun';
    $type 'state'// Chooses Article Include (further defined by $mycode)
    $myregion 'North';
    $reg 'north';
    include (
    $path."a1/inc/headstates.php");
    include (
    $path."a1/inc/headworld.php");
    include (
    $path."a1/inc/head.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" xml:lang="en" lang="en">
    <head>
    </head>
    I'm amazed it works; I thought all that stuff had to be inside the head tags, especially the links to the head includes, like head.php.

    "As for includes... image includes as copy and paste done by PHP, whatever is include()'ed, is (as far as the PHP parser), there... nothing special happens."

    Great. Everything's working just fine, and I also have my PHP at the top of the page. Thanks for helping me kill two birds with one stone.

  4. #4
    SitePoint Zealot colinr's Avatar
    Join Date
    Aug 2003
    Location
    san francisco, ca
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, if you want to have your meta-data included in the page you have to do that in the head (as far as I know.)

    I'm guessing you are getting confused about head elements and php..

    html has a head element, php does not.

    for php, since it is a parsed, interpreted language, you only need to make sure that the $vars have a value before you use them (and even a $var = '' counts)

    as for html, it requires everything to be in its proper place, so in the head you would need to do something like
    PHP Code:
    <html>
      <head>
         <title>$state_name . "-" Information </title>
         <?php include ("foobar/metadata.inc.php");
             
    /* OR */
                  
    include ($path "foobar/" $state "_meta.inc.php");
         
    ?>
      </head>
    <body> /// ETC ETC ETC
    Web Slave

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're right; I just checked the source code, and the head section is empty. So I put the main head include, with the metatags and links to scripts back in the head section, and everything looks fine.


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
  •