sending variable to include file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vetchling

    #1

    sending variable to include file

    Hi, I'm pretty new to php but I've been working on a site and have a
    problem I can't fix, though it's probably pretty simple.

    Basically I have an index.php file that includes a header.php file. I
    attempt to pass a variable to it as follows:
    <?include("head er.php?page=Hom e");?>

    Then the header.php file contains this code:
    <?php
    $page = $_GET['page'];
    ?>
    <html><head><ti tle><?$page?></title>

    I want $page to contain the string "Home" of course, but it ends up
    being nothing but an empty string.

    Thanks in advance for your help.

    Matt

  • Andrew Poelstra

    #2
    Re: sending variable to include file

    "vetchling" <vetchling@gmai l.comwrites:
    Hi, I'm pretty new to php but I've been working on a site and have a
    problem I can't fix, though it's probably pretty simple.
    >
    Basically I have an index.php file that includes a header.php file. I
    attempt to pass a variable to it as follows:
    <?include("head er.php?page=Hom e");?>
    >
    Then the header.php file contains this code:
    <?php
    $page = $_GET['page'];
    ?>
    <html><head><ti tle><?$page?></title>
    >
    I want $page to contain the string "Home" of course, but it ends up
    being nothing but an empty string.
    >
    Thanks in advance for your help.
    You can't add ?args to an include; PHP doesn't evalutate that when
    including files. (In fact, it usually never evaluates URL arguments:
    that's the HTTP daemon's job.) It merely copies the contents of
    header.php directly into your document (or parses equivilantly).

    To do this, you need to do:

    #In mypage.php you need to access $mypage_page directly.
    mypage_page = "Home";
    include ("mypage.php ");


    I've prefixed the variable $page to avoid naming conflicts.

    --
    Andrew Poelstra <http://www.wpsoftware. net/projects>
    To reach me by email, use `apoelstra' at the above domain.
    "Do BOTH ends of the cable need to be plugged in?" -Anon.

    Comment

    • Geoff Muldoon

      #3
      Re: sending variable to include file

      In article <1155873472.662 698.276320@i42g 2000cwa.googleg roups.com>,
      vetchling@gmail .com says...
      Hi, I'm pretty new to php but I've been working on a site and have a
      problem I can't fix, though it's probably pretty simple.
      >
      Basically I have an index.php file that includes a header.php file. I
      attempt to pass a variable to it as follows:
      <?include("head er.php?page=Hom e");?>
      Write your header (and footer if so warranted, example included) as a
      function which returns the header text with the variable embedded in it,
      and pass the variable to the function.

      # header_footer_f unctions.php
      <?php
      function print_header($t his_page) {
      $text = '<html><head><t itle>'.$this_pa ge.'</title></head>';
      return $text;
      }
      function print_footer($a uthor, $dept) {
      $text = '<div>Author: .'$author.'<br />Dept: '.$dept.'</div></html>';
      return $text;
      }
      ?>

      # home_page.php
      <?php
      include_once('h eader_footer_fu nctions.php')
      echo print_header('h ome');
      ?>
      ... rest of home page
      <?php
      echo print_footer('S lave Driver', 'Head Office');
      ?>

      # some_other_page .php
      <?php
      include_once('h eader_footer_fu nctions.php')
      echo print_header('s ome other page');
      ?>
      ... rest of some other page
      <?php
      echo print_footer('U nderpaid', 'Underlings');
      ?>

      Geoff M

      Comment

      • Chung Leong

        #4
        Re: sending variable to include file

        vetchling wrote:
        Hi, I'm pretty new to php but I've been working on a site and have a
        problem I can't fix, though it's probably pretty simple.
        >
        Basically I have an index.php file that includes a header.php file. I
        attempt to pass a variable to it as follows:
        <?include("head er.php?page=Hom e");?>
        >
        Then the header.php file contains this code:
        <?php
        $page = $_GET['page'];
        ?>
        <html><head><ti tle><?$page?></title>
        >
        I want $page to contain the string "Home" of course, but it ends up
        being nothing but an empty string.
        [rant against using includes as functions]

        Just wrap a function around the HTML then call it. Later on you'll find
        that it gives you a lot of flexibility. So instead of a header.php and
        footer.php, you have one file, interface.php:

        <?php function printHeader($pa ge) { ?>
        <html><head><ti tle><? echo htmlspecialchar s($page); ?></title>
        <? } ?>

        <?php function printFooter() { ?>
        </body></html>
        <? } ?>

        In the different pages, you'd include this file then call the functions
        at the right places:

        <?php

        require_once("i nterface.php");

        printHeader('We lcome');

        /* ... */

        printFooter();

        ?>

        Comment

        • m2m tech

          #5
          Re: sending variable to include file

          Dear Matt, your Script should work, you just forgot add 'echo' which
          prints your variable ...

          <?php
          $page = $_GET['page'];
          ?>
          <html><head><ti tle><?php echo $page; ?></title>


          vetchling wrote:
          Hi, I'm pretty new to php but I've been working on a site and have a
          problem I can't fix, though it's probably pretty simple.
          >
          Basically I have an index.php file that includes a header.php file. I
          attempt to pass a variable to it as follows:
          <?include("head er.php?page=Hom e");?>
          >
          Then the header.php file contains this code:
          <?php
          $page = $_GET['page'];
          ?>
          <html><head><ti tle><?$page?></title>
          >
          I want $page to contain the string "Home" of course, but it ends up
          being nothing but an empty string.
          >
          Thanks in advance for your help.
          >
          Matt
          >

          --
          --------------------------------------------
          m2m server software gmbh - http://www.m2m.at

          Comment

          • Chuck Anderson

            #6
            Re: sending variable to include file

            vetchling wrote:
            Hi, I'm pretty new to php but I've been working on a site and have a
            problem I can't fix, though it's probably pretty simple.
            >
            Basically I have an index.php file that includes a header.php file. I
            attempt to pass a variable to it as follows:
            <?include("head er.php?page=Hom e");?>
            >
            Then the header.php file contains this code:
            <?php
            $page = $_GET['page'];
            ?>
            <html><head><ti tle><?$page?></title>
            >
            I want $page to contain the string "Home" of course, but it ends up
            being nothing but an empty string.
            >
            Thanks in advance for your help.
            >
            Matt
            >
            >
            You don't need to pass a variable to an included file, it already has
            access to all variables in the includING script. It becomes part of the
            script that is including it.

            $page = 'Home';
            include 'header.php';

            Now header.php will see that $page contains 'Home'.

            --
            *************** **************
            Chuck Anderson • Boulder, CO

            *************** **************

            Comment

            Working...