|
-
Aug 3rd, 2004, 10:35 AM
#1
Thread Starter
Junior Member
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
-
Aug 3rd, 2004, 10:41 AM
#2
Frenzied Member
#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.
-
Aug 3rd, 2004, 10:58 AM
#3
Thread Starter
Junior Member
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
-
Aug 3rd, 2004, 11:03 AM
#4
Thread Starter
Junior Member
Oh, one other thing.
If I copy my array from page.php into template.php - it works!
I am Remus, come from the dead
-
Aug 3rd, 2004, 11:42 AM
#5
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).
-
Aug 3rd, 2004, 11:51 AM
#6
Thread Starter
Junior Member
...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
-
Aug 3rd, 2004, 12:06 PM
#7
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; ?>");
?>
-
Aug 3rd, 2004, 12:58 PM
#8
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|