|
Posted by Johnny on 08/27/06 20:16
"Mike Collins" <webspammer_@_yaho-o_.com> wrote in message
news:a513f29kmupbv3q6h0cjobvu7fpcjfhsbv@4ax.com...
> On Sat, 26 Aug 2006 23:50:05 -0700, "Johnny"
> <removethis.huuanito@hotmail.com> wrote:
>
> >> While attempting to assign the array elements of an include file to a
> >> variable the php tags embedded in the include file fail to
> >> interpolate. What am I missing?
> >>
> >> $t_file = file($PATH_DOCS . '/inc/navElem.inc.php');
> >> $iw_mast .= $t_file[0];
>
> >so you'll need to put echo at the beginning of each line and quote each
> >line which means you'll also need to escape quotes within the line.
>
> echo, quote, escape, rtm... right. I went round and round with this
> for awhile. Almost gave up. Here's what I'm using:
>
> ob_start();
> include($PATH_DOCS . '/inc/navElem.inc.php');
> $iw_mast .= ob_get_contents();
> ob_end_clean();
That works for one line but I don't see how it fills your need to populate
an array, but maybe you don't need to.
BTW I wasn't saying RTFM in the previous post, I was trying to point you to
other approaches that were best described by links to TM.
TM for php is very good, IMO, way better than other languages, with lots of
support from other users as well as the core team.
So using include() I've found, though it's not explicitly stated, you need
php tags at the start and end of any php you have in a file and if there's
none then you need to escape php by opening and closing php at the start.
This is because php expects php code in an include file. For me this works
as I can construct a page using several includes for like header body and
footer (when not using classes for the same).
What you have now done seems equivalent to doing this (adding php tags to
start of the include file):
------------ftst.php --------------
<?php // file ftst.php ?>
<ul><li><a href="<?php echo $absPath; ?>/<?php echo $p0_file;?>.php"><?php
echo $p0_label; ?></a></li></ul><?php ?>
---------------------------------
------------f.php --------------
<?php // file f.php
$absPath = 'localhost';
$p0_file = 'filename';
$p0_label = 'link text';
include ("ftst.php");
?>
---------------------------------
So when you run f.php it puts the link correctly and if you expand to
several lines (with extra vars $p1_file, etc.) that works too.
The plain include seems a whole lot simpler to me than the whole ob_ thing
but YMMV, depends what you are trying to do.
I use include files all the time with php tags in them and never have
problems, but then I usually escape out of php as shown.
The heredoc syntax can also help with reducing all those "<?php echo ... ?>
constructs and no need to escape quotes, also single quotes around a string
can work sometimes.
for example you could use this as the include file:
----------------ftst.php---------
<?php // ftst.php
echo <<<LINKS
<ul>
<li><a href="$absPath/$p0_file.php">$p0_label</a></li>
<li><a href="$absPath/$p1_file.php">$p1_label</a></li>
<li><a href="$absPath/$p2_file.php">$p2_label</a></li>
<li><a href="$absPath/$p3_file.php">$p3_label</a></li>
</ul>
LINKS;
?>
----------------------------------
This makes things much simpler... f.php just needs to define all those
extra vars:
------------f.php --------------
<?php // file f.php
$absPath = 'localhost';
$p0_file = 'filename0';
$p0_label = 'link0 text';
$p1_file = 'filename1';
$p1_label = 'link1 text';
$p2_file = 'filename2';
$p2_label = 'link2 text';
$p3_file = 'filename3';
$p3_label = 'lin3 text';
include ("ftst.php");
?>
---------------------------------
/* johnny */
Navigation:
[Reply to this message]
|