Results 1 to 8 of 8

Thread: Getting an array from an include file [Resolved]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21

    Getting an array from an include file [Resolved]

    Having trouble with some code... see below:

    File: template.php
    PHP Code:
    <?php /* kicks ASP! */

    $related = include('page.php');
    foreach (
    $related as $rel_item){
      include(
    $rel_item.'.php');
    }

    ?>
    File: page.php
    PHP Code:
    <?php /* kicks ASP! */

    $related = array('mediareview','vectornews','latestnews');
    return 
    $related;

    ?>
    When I access template.php I recieve the following error:
    Warning: Invalid argument supplied for foreach() in /home/xvnt/public_html/php_template/template.php on line 4
    Anybody know whats wrong?
    Thanks for any help,
    Phil

    P.S. Running PHP 4.3.8 on Apache 1.3.31, if it makes a difference.
    Last edited by ppeter; Aug 4th, 2004 at 05:47 AM.
    I am Remus, come from the dead

  2. #2
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    #1) I don't understand why you are making page.php at all.
    #2) Including a file is not like creating a function. You don't have to return anything.
    #3) You cannot set a variable equal to an include.

    So... remove the "return" statement and just include the file. There's no reason to set anything equal to anything. Variables created in page.php will be available in template.php.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21
    Okey doke,

    The code I have given is much more simplified than the actual code I am working with. Some points to consider:

    In answer to #1:
    - page.php is not always called page.php, it is dynamic - the very reason as to WHY I am using an include here.
    - the include for page.php is actually a remote file (the include is really 'http://...page.php')

    In answer to #2:
    - Looking at www.php.net/include/ (Example 11-7) it shows that my include file must use return(), otherwise $related in template.php would simply give me 1 instead of the array.

    In answer to #3:
    - As Example 11-7 at www.php.net/include/ shows, the include must be the value of a variable, otherwise return() would return the array into no-where.

    I've been working on this for a while now... still no luck!

    Any ideas?
    I am Remus, come from the dead

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21
    Oh, one other thing.

    If I copy my array from page.php into template.php - it works!
    I am Remus, come from the dead

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    You don't have to use the return statement in an include file. When you include a file the code in the file is inserted into the script at the point in which it was included. Therefore all variables defined in the include file have the same scope as the scope in which you included it.

    The reason your script does not work as expected is because you are including a remote file. The PHP manual says the following with regards to using the return statments when including remote files:
    You can take the value of the include call as you would a normal function. This is not, however, possible when including remote files unless the output of the remote file has valid PHP start and end tags (as with any local file).
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21
    ...unless the output of the remote file has valid PHP start and end tags
    ...which my page.php file does, so I would presume that it would work.

    The only reason I am calling it as a remote file is because it only gives the array if certain variables are passed to it... and you can't pass variables to a local include file. Example:

    PHP Code:
    <?

    include 'page.php?foo=bar';
    // Trys to find a file called 'page.php?foo=bar'

    include 'http://www.server.com/page.php?foo=bar';
    // This would work

    ?>
    Thoughts?

    Your help is much appreciated... Thank you very much
    I am Remus, come from the dead

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    I see what you are doing now. You have gone about solving your problem in the wrong way. The include function is designed to include and execute a section of php code.

    If you include the file from a remote source such as another website the php will be executed by the remote server and all you will get back is the HTML from echo statments. The variables you are passing to the script are seen by the remote server only. I will use an example to demonstrate:

    This is the wrong way.
    [remotepage.php]
    PHP Code:
    <?php 

        $var 
    $_GET['variable'];

        return 
    $var
    ?>
    [localpage.php]
    PHP Code:
    <?php
        $var 
    = include ('http://www.sever.com/remotepage.php?variable=test');

        echo (
    $var);
    ?>
    This is the right way.
    [remotepage.php]
    PHP Code:
    <?php 

        $var 
    $_GET['variable'];

        echo (
    "<?php return $var; ?>");
    ?>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2004
    Posts
    21
    Ahhh yes

    Thanks very much
    I am Remus, come from the dead

Posting Permissions

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



Click Here to Expand Forum to Full Width