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

Thread: Include file as a variable

  1. #1
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default Include file as a variable

    I have a template file that uses two different versions of the right colum so I use this code for the right column:

    Code:
    <div id="right-hm">
          <?=$rightcol;?>
    </div>
    I have created 2 include files called right-hm.html and right-res.html which are on the include path in php.ini. What I would like to do is set the value of $rightcol in the file that runs the template like this: $rightcol = 'right-hm.html'; in which case the above would be:

    Code:
    <?include($rightcol);?>
    But it does not work! The only way I can get the right column to appear is to load the value of $rightcol from a mySQL database. How do you write a command to get to to load from the file instead of the database? I even tried setting $rightcol = 'include("right-hm.html")'; It didn't work.

    What am I doing wrong? Thanks, erin

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Instead of using short starts, use the standard.
    Jeremy | jfein.net

  3. #3
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    What are short starts and what are standards? I have no idea what you are saying. Sorry.

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Short:
    PHP Code:
    <?

    ?>
    Standard:
    PHP Code:
    <?php

    ?>
    Jeremy | jfein.net

  5. #5
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    Dear Nile: Thanks for helping me with this. It is not the php short tags because I have tons of them throughout the site. But I know you know how to solve this problem because you know php so well and helped me previously with a similar issue. If you would be so kind, I need you to show me how to say this:

    <?php include('right-hm.html'); ?>

    but where the filename is a variable. I've tried the following:

    <?php include('$rightcol'); ?> and <?php include($rightcol); ?>

    Setting $rightcol = 'include("right-hm.html");';

    then in template <?php echo $rightcol; ?> ... this one prints the literal 'include("right-hm.html") on the screen.

    So I tried leaving out the outer quotes and it did print the column, but at the very top of the screen above the page. Not sure what else to try. Any ides? Thanks, erin.

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    So this doesn't work?:
    PHP Code:
    <?php
    $rightcol 
    "include('right-hm.html')";
    ?>
    Then
    PHP Code:
    <?php echo $rightcol?>
    If the above works it was because of your ; after the include.
    Jeremy | jfein.net

  7. #7
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    instead of using include, simply use file_get_contents like so:

    Code:
    <?php 
     $content = file_get_contents('filename_here.txt');
    ?>
    and then:

    Code:
    <?php echo $content; ?>
    Edit: Added Later: You cannot use include in a variable as that is not what the include function was made for and will cause strange things to happen to your php code. Instead, you need to read the file using file accessing functions (such as: file, file_get_contents, fgets, and others).

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  8. The Following User Says Thank You to thetestingsite For This Useful Post:

    kuau (03-21-2008)

  9. #8
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    That doesn't work either. It printed this on the screen where the column would be...

    include('right-hm.html')

  10. #9
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Oh, sorry. Try this:
    Code:
    <?php $rightcol; ?>
    Jeremy | jfein.net

  11. The Following User Says Thank You to Nile For This Useful Post:

    kuau (03-21-2008)

  12. #10
    Join Date
    Sep 2007
    Location
    Maui
    Posts
    642
    Thanks
    284
    Thanked 15 Times in 15 Posts

    Default

    I tried the file_get_contents and removing the echo and both had the same result. The column did not appear and in the code there was nothing at all.

    Maybe this is just not possible. I really appreciate your willingness to help me.

    Mahalo, erin

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
  •