Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post

    Need help with PHP includes

    Okay, I did a little searching and found a PHP snippet for including pages. I put it into my index.php page, and now when I view the page I get an error:

    Notice: Undefined variable: page in C:\Apache\Apache2\htdocs\Homm\index.php on line 26

    That's the following line:
    if(!empty($HTTP_GET_VARS[$page]) and file_exists("./$HTTP_GET_VARS[$page]")) {

    Here's the source for the page, php.index.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

    <link rel="stylesheet" type="text/css" href="styles.css" />

    <meta name="Heroes of Might and Magic 1" content="HOMM, Heroes of Might and Magic, A Strategic Quest, artifacts, heroes, troops, castles, towns, spells, skills, scenarios, campaigns, pictures, strategies, cheats, links, downloads, where to buy, 3DO, New World Computing, NWC" />
    <title>Heroes of Might and Magic Homepage</title>
    </head>

    <body onload="top.document.title = document.title;">
    <table border="1">
    <tr>
    <td rowspan="2" valign="top">
    <? include("leftNav.php") ?>
    </td>
    <td valign="top">
    <? include("topNav.php") ?>
    </td>
    </tr>
    <tr>
    <td>
    <?php
    if(!empty($HTTP_GET_VARS[$page]) and file_exists("./$HTTP_GET_VARS[$page]")) {
    include("./$HTTP_GET_VARS[$page]");
    } else {
    include("./index.txt");
    }
    ?>
    </td>
    </tr>
    </table>
    </body>
    </html>

    I see the "page is undefined" problem, but how/where would I define it? Thanks for any help- I'm puzzled by this.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    577
    Thanks
    0
    Thanked 0 Times in 0 Posts
    find-replace.....

    find $page ---- replace 'page'

    the syntax for $_GET array calls just quotes the variable name without a leading $

    echo $HTTP_GET_VARS['page'];

    would echo the value of ?page=???? in the url

    if you have a recent build with reg_globals OFF in the .ini, use
    $_GET['page'] instead.

    -------------------------

    quick note on strings
    any string encased in double quotes will have the variables within it parsed and translated to their values
    any string encased in single quotes will just be echoed (if echoing you are)

    eg
    $var = 'Grant';
    echo "hello $var";
    ======> hello Grant
    echo 'hello $var';
    ======> hello $var

    you can step out of string with the period concatenator .

    echo 'hello ' .$var;
    =====> hello Grant

    personally I prefer encasing pretty much all strings in single quotes and just concatenating into and out as neccesary
    echo 'hello ' .$var. ', how are you?';

    --------------------------
    PHP Code:
    if(!empty($HTTP_GET_VARS['page']) && file_exists('./' .$HTTP_GET_VARS['page'])) {
    include(
    './' .$HTTP_GET_VARS['page']); 
    also note - encasing php code within [ php] [/ php] tags here will syntax highlight it, which helps for debugging.
    Ökii - formerly pootergeist
    teckis - take your time and it'll save you time.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post
    Thanks for the help. It works now without errors (just that one page). Now, when I want to include other content pages, do they need to be .php as well, or just plain HTML?

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    you can call your pages .aunt_mary if you wish at this point PHP does not care so any or no extension is cool , though any php code that you include will still need to be encased in <?php ?> etc
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post
    Okay, here's the PHP code I'm now using in index.php
    PHP Code:
        <table border="1">
          <tr>
            <td rowspan="2" valign="top">
              <? include("leftNav.txt"?>
            </td>
            <td valign="top">
              <? include("topNav.txt"?>
            </td>
          </tr>
          <tr>
            <td>
            <?php
              
    if(!empty($HTTP_GET_VARS['page']) && file_exists('./' .$HTTP_GET_VARS['page'])) {
                include(
    './' .$HTTP_GET_VARS['page']);
              } else {
                include(
    "./home.html");
              }
            
    ?>
            </td>
          </tr>
        </table>

    Here's the resulting HTML source code
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

    <link rel="stylesheet" type="text/css" href="styles.css" />

    <meta name="Heroes of Might and Magic 1" content="HOMM, Heroes of Might and Magic, A Strategic Quest, artifacts, heroes, troops, castles, towns, spells, skills, scenarios, campaigns, pictures, strategies, cheats, links, downloads, where to buy, 3DO, New World Computing, NWC" />
    <title>Heroes of Might and Magic Homepage</title>
    </head>

    <body onload="top.document.title = document.title;">
    <table border="1">
    <tr>
    <td rowspan="2" valign="top">
    <table border="0" cellspacing="3" cellpadding="0">
    <tr>
    <td><a href="index.php?page=screenshots"><img src="screenshots_button.jpg" width="75" height="34" border="0" alt="Screenshots" class="popshadow" /></a></td>
    </tr>
    <tr>
    <td><a href="index.php?page=heroes"><img src="heroes_button.jpg" width="75" height="34" border="0" alt="Heroes" class="popshadow" /></a></td>
    </tr>
    <tr>
    <td><a href="index.php?page=artifacts"><img src="artifacts_button.jpg" width="75" height="34" border="0" alt="Artifacts" class="popshadow" /></a></td>
    </tr>
    <tr>
    <td><a href="index.php?page=creatures"><img src="creatures_button.jpg" width="75" height="34" border="0" alt="Creatures" class="popshadow" /></a></td>
    </tr>
    <tr>
    <td><a href="index.php?page=castles"><img src="castles_button.jpg" width="75" height="34" border="0" alt="Castles" class="popshadow" /></a></td>
    </tr>
    <tr>
    <td><a href="index.php?page=spells"><img src="spells_button.jpg" width="75" height="34" border="0" alt="Spells" class="popshadow" /></a></td>
    </tr>
    <tr>
    <td><a href="index.php?page=map_locations"><img src="maplocations_button.jpg" width="75" height="34" border="0" alt="Map Locations" class="popshadow" /></a></td>
    </tr>
    <tr>
    <td><a href="index.php?page=general_info"><img src="general_button.jpg" width="75" height="34" border="0" alt="General Information" class="popshadow" /></a></td>
    </tr>
    <tr>
    <td><a href="index.php?page=general_strategy"><img src="general_strategy_button.jpg" width="75" height="34" border="0" alt="General Strategy" class="popshadow" /></a></td>
    </tr>
    <tr>
    <td><a href="index.php?page=campaign"><img src="campaign_button.jpg" width="75" height="34" border="0" alt="Campaign" class="popshadow" /></a></td>
    </tr>
    </table> </td>
    <td valign="top">
    <table border="0" cellspacing="2" cellpadding="0">
    <tr>
    <td><a href="index.php"><img src="home_button.jpg" width="75" height="34" border="0" alt="Home" class="popshadow" /></a></td>
    <td><a href="index.php?page=links"><img src="links_button.jpg" width="75" height="34" border="0" alt="Links" class="popshadow" /></a></td>
    <td><a href="index.php?page=where_to_buy"><img src="wheretobuy_button.jpg" width="75" height="34" border="0" alt="Where to Buy" class="popshadow" /></a></td>
    <td><a href="index.php?page=downloads"><img src="downloads_button.jpg" width="75" height="34" border="0" alt="Downloads" class="popshadow" /></a></td>
    <td><a href="index.php?page=copyright_info"><img src="copyright_button.jpg" width="75" height="34" border="0" alt="Copyright Info" class="popshadow" /></a></td>
    <td><a href="index.php?page=contact"><img src="contact_button.jpg" width="75" height="34" border="0" alt="Contact Me!" class="popshadow" /></a></td>
    <td><a href="http://www.freewebz.com/hommworld/index.html"><img src="homepage_button.jpg" width="75" height="34" border="0" alt="Go to my Homepage!" class="popshadow" /></a></td>
    </tr>
    </table> </td>
    </tr>
    <tr>
    <td>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

    <link rel="stylesheet" type="text/css" href="styles.css" />

    <meta name="Heroes of Might and Magic 1" content="HOMM, Heroes of Might and Magic, A Strategic Quest, artifacts, heroes, troops, castles, towns, spells, skills, scenarios, campaigns, pictures, strategies, cheats, links, downloads, where to buy, 3DO, New World Computing, NWC" />
    <title>Heroes of Might and Magic Homepage</title>
    </head>

    <body onload="top.document.title = document.title;">

    <p class="center"><img src="HOMM_title.gif" width="354" height="149" border="0" alt="Heroes of Might and Magic" /></p><br />
    <h3>Welcome</h3>

    <p>Welcome, weary traveler! You are here because you are interested in learning more about Heroes of Might and Magic, right? Read onward!</p>

    <h3>What is Heroes of Might and Magic?</h3>

    <p>Heroes of Might and Magic: A Strategic Quest (HOMM for short) was produced by <a href="http://www.3do.com">3DO</a> in 1995. It is now the first chapter in the highly acclaimed Heroes of Might and Magic series. However, the subject of this site is solely on the first game. HOMM is a strategy-war type of game. You have to manage resources, while battling your opponents at the same time. You must capture and build up castles to gain more powerful troops, or surely your opponents will annihilate you... This game has almost unlimited replay potential, since you can use different strategies every time you play. It is turn-based, unlike real-time games like Warcraft and Starcraft. I prefer HOMM, because unlike the aforementioned games, you don't have to be everywhere at once! They do have their high points, such as fast-paced action, which some people do like... But that's off-topic. On this website, I will tell you as much as I can; HOMM is a complex game. I will provide information on the heroes, artifacts, creatures, and more (in short, pretty much everything)! So pull up a chair, brew a pot of coffee, and prepare to descend into the world of might and magic.</p>

    <h3>Want more?</h3>

    <p>Use the buttons in the navigation pane on the left and above to go through my site. I hope you will find the content informative and useful.</p>

    <h3>Let me know what you think!</h3>

    <p>If there is anything you want to say or ask me, you can email me or sign my guestbook. Options for both are on my Contact page.</p>

    </body>
    </html> </td>
    </tr>
    </table>
    </body>
    </html>

    Notice the extra html, body, head, etc tags? This presents a bit of a problem. So now I'm wondering, does this mean I need to remove the doctype, html, head, etc tags from each individual HTML page, or is there some other way I can do it?

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post
    Here's an idea. Why don't I drop the content page include in index.php, and just include the two navigation files into each one of my HTML pages? Would that mean I would need to nsave all those files as .php? Would that work?

  • #7
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    the 'standard' way would be ... (along the lines of)

    PHP Code:
    <?
    //get the doctype <html style sheets etc in header.php
    include 'header.php';
    ?>
    <table>
      <tr>
         <td><?include $nav.'.php';?></td>
         <td>Content</td></tr>
    </table>
    <?
    //closing html and footer stuff
    include 'footer.php';
    ?>
    so each page of content calls the appropriate headers and footers and navigation etc , but thats only 1 way , and there is no 'right' way.

    these days I tend to have each page with a config then include everything..

    PHP Code:
    $config['page_name']='index';
    $config['page_title']='welcome to blah';
    /*etc , more +variables which may 
    be used in the included pages*/

    include './include/'.$config['page_name'].'_functions.inc';
    include 
    'header.inc';
    include 
    $config['page_name'].'.inc';
    include 
    'footer.inc'

    now thats heavy on the includes ... especially when the includes also include files but I also keep static versions of each page where appropriate, but it does (IMO) make for a tidy way to keep track of things.

    ...plus a quick addition to includes - I did some testing to proove a point to someone the other week and ended up eating my words (again ) , including a lot of files does significantly slow down a page as opposed to a single larger file .. I knew it would be slower but I did not realise quite how much slower ! nearly x2 !
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post
    Okay, I think I know what I'll do. On each content page in my site, after the doctype and starter tags (html, head, title, you know which), I'll start a layout table and include the navigation files into the top and left cells. Then just have the actual page content in the last cell in the bottom right.

    PHP Code:
    <doctype...>
    <html>
      <head>
        <meta...>
        <link stylesheet...>
        <title></title>
      </head>
      <body>
        <table>
          <tr>
            <td rowspan="2"><? include('leftNav.php'); ?></td>
            <td><? include('topNav.php'); ?></td>
          </tr>
          <tr>
            <td>
              Page content
            </td>
          </tr>
        </table>
      </body>
    </html>
    How does this seem? I assume each individual page, since it's using some PHP code, would need to be a .php file?

  • #9
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post
    I've been working with the idea I suggested in my last post (just including the navigation files into each page), and I've got a problem.

    Below is the code I'm using in each individual page to include the left navigation and top navigation. Then there's the content for that page.
    PHP Code:
        Page starts
        <table border="1">
          <tr>
            <td rowspan="2" valign="top">
              <? include("leftNav.txt"?>
            </td>
            <td valign="top">
              <? include("topNav.txt"?>
            </td>
          </tr>
          <tr>
            <td>
              Page content...
              Page ends
    Here is topNav.php, to indicate how I'm doing links.
    Code:
    <table border="0" cellspacing="2" cellpadding="0">
                <tr>
                  <td><a href="index.php"><img src="home_button.jpg" width="75" height="34" border="0" alt="Home" class="popshadow" /></a></td>
                  <td><a href="links.php"><img src="links_button.jpg"  width="75" height="34" border="0" alt="Links" class="popshadow" /></a></td>
                  <td><a href="where_to_buy.php"><img src="wheretobuy_button.jpg"  width="75" height="34" border="0" alt="Where to Buy" class="popshadow" /></a></td>
                  <td><a href="downloads.php"><img src="downloads_button.jpg"  width="75" height="34" border="0" alt="Downloads" class="popshadow" /></a></td>
                  <td><a href="copyright_info.php"><img src="copyright_button.jpg" width="75" height="34" border="0" alt="Copyright Info" class="popshadow" /></a></td>
                  <td><a href="contact.php"><img src="contact_button.jpg" width="75" height="34" border="0" alt="Contact Me!" class="popshadow" /></a></td>
                  <td><a href="http://www.freewebz.com/hommworld/index.html"><img src="homepage_button.jpg" width="75" height="34" border="0" alt="Go to my Homepage!" class="popshadow" /></a></td>
                </tr>
              </table>
    When I load up index.php, it's fine. The navigation stuff appears where it should be, and the page content is what it should be.

    But when I click one of the links in the top navigation area, the browser just reloads the index. The actual content or page title never changes!

    The one thing that does change is the address in the address bar. Initially, when index.php is loaded, the address is http://localhost/Homm/index.php.

    But when I click on one of the top links, like I said, the page content or title doesn't change. The address does change, however. It becomes http://localhost/Homm/index.php?page=links or http://localhost/Homm/index.php?page=where_to_buy, or whatever, depending on which link you clicked.

    My question is: why is this happening? Is there something wrong with the include scripts I wrote? Is it just the way that PHP works? Can anyone suggest a solution?

  • #10
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    PHP does not put the '?page=whatever' etc in there , are you are sure you are not just include()ing the wrong or older version of your nav files ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #11
    Regular Coder
    Join Date
    Jun 2002
    Location
    Victoria, BC, Canada
    Posts
    962
    Thanks
    0
    Thanked 1 Time in 1 Post
    You know what? I was including leftNav.txt and topNav.txt when I was making the changes to leftNav.php and topNav.php . I changed the references to the php versions, and deleted the text ones, and the links are working fine now . Now I've got to iron out some bugs in the page layout tables.

    Thanks for the help!


  •  

    Posting Permissions

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