Page 1 of 1

Include a Variable

Posted: Wed Dec 20, 2006 7:52 am
by iknownothing
Hey all,
I was wondering if it were at all possible to include a variable from another page. Done some search engining, but nothing came up.

eg.

Code: Select all

include($var, "afile.php"); //include $var from afile.php
the reason why is that want to include only one paragraph out of the page.

Posted: Wed Dec 20, 2006 8:06 am
by jayshields
Why include it? Just paste it into the file you want it or set it as a constant in the other file and refernence it that way.

Posted: Wed Dec 20, 2006 8:09 am
by iknownothing
jayshields wrote:Why include it? Just paste it into the file you want it or set it as a constant in the other file and refernence it that way.
because its editable on the other page.

also with this, when the variable includes, it will be a reasonably long paragraph of text, and I was wondering how to add a "..." just before the end of its available area. Prime example being a search engine, when the desciption doesnt fit in.

Re: Include a Variable

Posted: Wed Dec 20, 2006 8:41 am
by onion2k
iknownothing wrote:I was wondering if it were at all possible to include a variable from another page. Done some search engining, but nothing came up.

eg.

Code: Select all

include($var, "afile.php"); //include $var from afile.php
the reason why is that want to include only one paragraph out of the page.
You can't do that. Think of the include() function as a "copy and paste" of whatever is in the file being included into the script thats running at the position where the include() is called.

Re: Include a Variable

Posted: Wed Dec 20, 2006 8:46 am
by iknownothing
onion2k wrote:
iknownothing wrote:I was wondering if it were at all possible to include a variable from another page. Done some search engining, but nothing came up.

eg.

Code: Select all

include($var, "afile.php"); //include $var from afile.php
the reason why is that want to include only one paragraph out of the page.
You can't do that. Think of the include() function as a "copy and paste" of whatever is in the file being included into the script thats running at the position where the include() is called.
ok, so can i include the entire file, but have it invisible, so i can "paste" only the variable...?

Re: Include a Variable

Posted: Wed Dec 20, 2006 8:50 am
by onion2k
iknownothing wrote:ok, so can i include the entire file, but have it invisible, so i can "paste" only the variable...?
You could do that with the output buffer I guess. It'd be a pretty awful hack though. Why not refactor the code so that it works properly? EG, put the variable you want into another file (better still, a database table) and include it in both the page you're working on at the moment, and the page it currently resides in.

Posted: Wed Dec 20, 2006 8:52 am
by iknownothing
yeah i could do that easily enough, just figured it would be great to have something so simple actually work...

Posted: Wed Dec 20, 2006 9:21 am
by iknownothing
iknownothing wrote: also with this, when the variable includes, it will be a reasonably long paragraph of text, and I was wondering how to add a "..." just before the end of its available area. Prime example being a search engine, when the desciption doesnt fit in.
so does anyone know anything about this?? perhaps a character counter or something?

Substr

Posted: Wed Dec 20, 2006 10:18 am
by timclaason
To do that, use substr()

Code: Select all

print( substr($var, 0, 50) . "..." );

Posted: Wed Dec 20, 2006 10:25 am
by John Cartwright
Looks in our useful posts thread, there is a thread describing how to do this without chopping words in half.