...

View Full Version : Why won't include() work inside arrays?



cyphix
05-07-2004, 03:24 PM
Trying to use an include() inside an array but I'm getting errors. Why is this?

Thanks guys!

raf
05-07-2004, 03:47 PM
it's because there is something wrong.

Maybe post the code and the error you get because my psychic powers are letting me down at the moment.

bcarl314
05-07-2004, 04:48 PM
it's because there is something wrong.

Maybe post the code and the error you get because my psychic powers are letting me down at the moment.


Swami says: <calling powers="mystic">Parse Error</calling> You are ending before beginning an ending... :D

raf
05-07-2004, 04:53 PM
Swami says: <calling powers="mystic">Parse Error</calling> You are ending before beginning an ending... :D
Noone can proove anyhting ever truely ends ... so i fear there will be no fix possible (until the next Windows release) --> but can we trust on that :eek:

So please give us the error you're getting and maybe we can recreat some kind of hope or temporarely equilibrium

cyphix
05-07-2004, 05:11 PM
ok..

Here is my code..



<?php

$people = array(

include("people.inc");

);


?>


Here is the error:

Parse error: parse error, expecting `')'' in xxxxxxxxxxxxxxxxxxxxxxxx on line 5

Cheers!

bcarl314
05-07-2004, 05:58 PM
wohoo, I was (er I mean Swami) was right! Parse Error! :D

Anyway, if your trying to create an array of include files, I'd try something like this...



$people = array("people.inc");
include($people[0]);

mordred
05-07-2004, 05:59 PM
The parser expects variables or values to be put into an array. include is neither of both, so it throws an error. include is a language feature, and you just can't use it at that point in your script. The PHP language is defined this way and you can't get around that.

If you tell us what you're actually trying to achieve, we can (hopefully) give you more detailed help.

EDIT: Posts crossed. And that even when I'm a couple of time zones ahead of bcarl. Bah. ;)

cyphix
05-07-2004, 06:11 PM
Thanks bcarl but that idea isn't going to work in my situation. :(

Ok, what I'm trying to achieve here is to pull the array information from another file (several other arrays as this is a multi-dimensional array).

Now you're probably thinking why don't I just put it in the code?

Because I want to use a file to append extra arrays to the file & I need to break up the main array to add sub-arrays inside the main array otherwise they will get added after the main array would close.

Now, I know this could all be handled with a database better but I'm just doing this as a learning experience. :D

firepages
05-07-2004, 06:27 PM
do you mean you want to have array $x , then make that array bigger by including array $y within it ?

not that you can as Mordred explained but trying to work out exactly what you mean.

cyphix
05-07-2004, 06:36 PM
Ok.. here is the code that I have inside "people.inc", this may help you better understand what I am trying to do..



array("name" => "Bill",
"age" => 15,
"country" => "USA",
"email" => "rat@test.com"),

array("name" => "Harry",
"age" => 24,
"country" => "USA",
"email" => "rat2@test.com"),

array("name" => "Devon",
"age" => 32,
"country" => "Italy",
"email" => "devon@test.it"),

array("name" => "Brett",
"age" => 23,
"country" => "USA",
"email" => "ratrat@test.com"),

array("name" => "George",
"age" => 33,
"country" => "Germany",
"email" => "goo@test.de"),

array("name" => "Mary",
"age" => 35,
"country" => "USA",
"email" => "moo@test.com")


As you can see there is several arrays inside there which I am trying to pull into people.php inside the main array.

Doing it this way I can eaisly append a new array to the "people.inc" file & it will also be updated inside the main array inside people.php.

bcarl314
05-07-2004, 06:57 PM
In that case, why don't you just name the array in your include file???



//include.inc
$tmpArrayName = //All the array stuff


Then just include it...



include(include.inc);
$people = $tmpArrayName;

cyphix
05-07-2004, 07:05 PM
I can't put all the array code in the include file because then when I append to it the new sub-array will be outside the main array. :(

raf
05-07-2004, 07:20 PM
err... so you wan't to merge the tw arrays? or append the array to another one?
It doesn't matter if it's a uni-dimensional or multidimensiona array.
Say your array inside the file you include looks just liek you posted. But inside a real array instead of a list of elements.So
$includearray=(your elements come here);
Then you can use


$bla= array ('animalsounds'=> array('dog'=>'woefwoef', 'cat'=>'niauw', mordred=>'grrrr'));
require('include.php');
$total = array_merge($bla, $includearray);

Or if the $includearray needs to be an element of the $bla:

$bla[] = $includearray;


But including the elements isn't gonnan work, because they are just include like that. As seperate arrays. Just like you would copy paste them insie your code. So you need to group them inside an array that you can then refer to.

Maybe gve us an example of the endresult you have in mind + the relevant parts of the script you are using it in

cyphix
05-07-2004, 07:37 PM
Yes... I'm trying to pull all the code in post #10 to where the include reference is in the below code:



<?php

$people = array(

include("people.inc");

);


?>


You then can see the only way to reference this array is by using..

$people[0]["name"]

..variables.

raf
05-07-2004, 08:01 PM
OK. The elaborate approach then.

If you want the result (your script (--> script.php) when it's getting parsed to look like


<?php
echo 'I am using arrays here !';
$people = array(array("name" => "Bill",
"age" => 15,
"country" => "USA",
"email" => "rat@test.com"),

array("name" => "Harry",
"age" => 24,
"country" => "USA",
"email" => "rat2@test.com"),

array("name" => "Devon",
"age" => 32,
"country" => "Italy",
"email" => "devon@test.it"),

array("name" => "Brett",
"age" => 23,
"country" => "USA",
"email" => "ratrat@test.com"),

array("name" => "George",
"age" => 33,
"country" => "Germany",
"email" => "goo@test.de"),

array("name" => "Mary",
"age" => 35,
"country" => "USA",
"email" => "moo@test.com") );
echo '<br /> Very simple';
?>


then you need to have this inside the included file (lets call it included.php)


<?php
$people = array(array("name" => "Bill",
"age" => 15,
"country" => "USA",
"email" => "rat@test.com"),

array("name" => "Harry",
"age" => 24,
"country" => "USA",
"email" => "rat2@test.com"),

array("name" => "Devon",
"age" => 32,
"country" => "Italy",
"email" => "devon@test.it"),

array("name" => "Brett",
"age" => 23,
"country" => "USA",
"email" => "ratrat@test.com"),

array("name" => "George",
"age" => 33,
"country" => "Germany",
"email" => "goo@test.de"),

array("name" => "Mary",
"age" => 35,
"country" => "USA",
"email" => "moo@test.com") );
?>


and inside the actual script, where you wan't to use this array, you'd have



<?php
echo 'I am using arrays here !';
require('include.php');
echo '<br /> Very simple';
?>


You see?
you could then use it like if you copy-paste it, because that is all that include() and require() do. They simply paste the content inside the script and you can then refere to variables as if they were part of yout script.php
Like


<?php
require('include.php');
echo 'The arrray i include looks like :<br />';
print_r $people
echo '<br /> and it\'s second element is:' . $people[1];
echo '<br /><br />And here i includes this $people array inside another array:';
$total = array ($people, 'yet another array'=> array('one'=>'test1', 'second'=>'lala'));
print_r $total;
?>

cyphix
05-07-2004, 08:17 PM
Yes... but that's the point I am trying to get across.

If I did that how would I append a new array to it? The code in the included file would end up looking like this when I appended a new array to the file..




$people = array (

array("name" => "Bill",
"age" => 15,
"country" => "USA",
"email" => "rat@test.com"),

array("name" => "Harry",
"age" => 24,
"country" => "USA",
"email" => "rat2@test.com"),

array("name" => "Devon",
"age" => 32,
"country" => "Italy",
"email" => "devon@test.it"),

array("name" => "Brett",
"age" => 23,
"country" => "USA",
"email" => "ratrat@test.com"),

array("name" => "George",
"age" => 33,
"country" => "Germany",
"email" => "goo@test.de"),

array("name" => "Mary",
"age" => 35,
"country" => "USA",
"email" => "moo@test.com")

);

array("name" => "NewArray",
"age" => 1,
"country" => "CodingForums",
"email" => "newarray@test.com")



You see how it ends up "outside" the main array? I need to get it inside the array with the other sub-arrays.

raf
05-07-2004, 08:27 PM
Look a few posts earlier.


<?php
require('include.php');
//let's print the original array for illustrative purposes
print_r $people;
// now i'm gonna add an element
$people[] = array("name" => "NewArray",
"age" => 1,
"country" => "CodingForums",
"email" => "newarray@test.com");
// and now i'm gonna print the expanded one
print_r $people;
?>

cyphix
05-07-2004, 08:52 PM
So how does that manage to get it inside the main array & not outside it?

Also, in that example.. what code is inside "include.php"?

raf
05-07-2004, 08:56 PM
include.php looks like i posted a few posts back :


<?php
$people = array(array("name" => "Bill",
"age" => 15,
"country" => "USA",
"email" => "rat@test.com"),

array("name" => "Harry",
"age" => 24,
"country" => "USA",
"email" => "rat2@test.com"),

array("name" => "Devon",
"age" => 32,
"country" => "Italy",
"email" => "devon@test.it"),

array("name" => "Brett",
"age" => 23,
"country" => "USA",
"email" => "ratrat@test.com"),

array("name" => "George",
"age" => 33,
"country" => "Germany",
"email" => "goo@test.de"),

array("name" => "Mary",
"age" => 35,
"country" => "USA",
"email" => "moo@test.com") );
?>



So how does that manage to get it inside the main array & not outside it?
Well, it's magic. It's one of the ways to insert a new element into an array.
Try it. It might work. And if it doesn't, then post back and i'll try it and if necessery set it straight.

cyphix
05-08-2004, 12:16 AM
Well... the "print_r " didn't work out too well. Just ended up with a lot of text in the output page.

So if I can add an extra sub-array to the current $people array by using the code you showed I'll just use:



<?php

require('listing.php');

?>


To include the file contents in the page.

Now I am trying to use a seperate file to add an array to the "listing.php" file but am getting some errors..

Here is my code:



<html>
<head>
<title>Add Person</title>
<style>
<!--
td { font-family: Arial; font-size: 13px; font-weight: bold}
a:link { color :blue; numeric-decoration: none; text-decoration: underline;}
a:visited { color :blue; numeric-decoration: none; text-decoration: underline;}
-->
</style>
</head>

<body>


<?php

$file = "listing.php";

$add = fopen("$file", "a") || die ("Couldn't open file.");

fputs($add, "$people[6] = array( 'name' => '$addpname',");
fputs($add, "'age' => '$addpage',");
fputs($add, "'country' => '$addpcountry',");
fputs($add, "'email' => '$addpemail')");

fclose($add);

echo ($addpname);
echo " has been added. <a href=\"peoplelisting.php\">Back to database</a>.";

?>


Errors I am getting is:

Warning: fputs(): supplied argument is not a valid stream resource in xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx on line 22

Warning: fputs(): supplied argument is not a valid stream resource in xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx on line 23

Warning: fputs(): supplied argument is not a valid stream resource in xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx on line 24

Warning: fputs(): supplied argument is not a valid stream resource in xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx on line 25

Warning: fclose(): supplied argument is not a valid stream resource in xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx on line 27

Thanks for all your help!

raf
05-08-2004, 12:33 AM
Well... the "print_r " didn't work out too well. Just ended up with a lot of text in the output page.
That's exactly what it's meanth for. Look at the source of the page inside your browser, and you'll see that the texts is nicely formatted there, so that you can see the structure of the array. It is not intended for actual showing of values to the client, but for debuggingpurposes to see your arrays content in a formatted way.
all he errors mean that the fopen() didn't return a filehandler. Proably because the adress is wrong. Try using a relative path, the url or the complete physical path (c:\myroot\etc)
i'm not sure the || should be used there. i'd use 'or'

bcarl314
05-08-2004, 02:30 AM
Look at the source of the page inside your browser, and you'll see that the texts is nicely formatted there'

I always like to enclose print_r between <pre> and </pre>

Nicely formatted on the page!

cyphix
05-08-2004, 12:16 PM
Well I didn't need to modify the path to get rid of those errors.. all I did was chnage the || to or & it worked fine.

Only thing tho, the $people[] doesn't seem to work.. I get this error:

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING'

raf
05-09-2004, 06:32 PM
Only thing tho, the $people[] doesn't seem to work.. I get this error:

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING'
Could be that the code i psoted contained an error.

But it's not very likely. It's much more likely that you either altered the code or made a mistake when copying it.

So i tryed it out, and there is no error on that line. There are however parse-error on the print_r 's which of course should have been enlosed in brackets.

cyphix
05-09-2004, 07:54 PM
Well, here is the code..



<?php

$file = "listing.php";

$add = fopen("$file", "a") or die ("Couldn't open file.");

fputs($add, "$people[] = array (");
fputs($add, "array( 'name' => '$addpname',");
fputs($add, "'age' => '$addpage',");
fputs($add, "'country' => '$addpcountry',");
fputs($add, "'email' => '$addpemail')");
fputs($add, ");");

fclose($add);

echo ($addpname);
echo " has been added. <a href=\"peoplelisting.php\">Back to database</a>.";

?>


Tried it a few different ways, but this is how I currently have it.

raf
05-09-2004, 08:37 PM
What are you trying to do ?

If you try to append to the array in listing.php, then this obviously wount work since you will be adding php-code to the file. So this means that you'd need to eval() the code when you use the file.

A better way to add a new element to the array if first including listing.php, then appending the element (like in the code i posted) and then replacing the content of listing.php. Check the sticky at the top of this forum to see how t write to a file.
(so read in, expand, replace original content)

You should also learn that stringvalues enclosed in double quotes are parsed. So it you would like to write that code to the file, the you need to enclose it in single quotes. Else all variable (where you wan't to write the name from to the file, are parsed and looked for in the curent symbols table, where thay are of course missing + all syntax will of course be incorrect.
So everything you try to write to the file as plain text, should be enclosed in single quotes, and the single quotes you try to write should be escaped or replaced by double quotes.

the $file inside
fopen("$file", "a")
should not be enclosed with quotes.

cyphix
05-09-2004, 11:44 PM
Ok cheers!

I think I'll try & do this via a database instead hehe. ;)

raf
05-10-2004, 11:44 AM
That would indeed be a more promissing approach.

I already feared i was just wasting my time trying to figure out why you were using arrays in embedded files.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum