Jump to content

Archived

This topic is now archived and is closed to further replies.

cmaier

foreach loop inside <<< style string

Recommended Posts

Ok... so i have a very large block of code (the majority of my page) enclosed in a string that i print later on...

[code]
<?php
$temp_code_block = <<<TEMP_CODE_KEYWORD

.... some html & php code

.... foreach loop goes here

.... more html & php

TEMP_CODE_KEYWORD;
echo $temp_code_block;
?>
[/code]

Everything works fine except i can't figure out how to use loops within that string (a foreach loop specifically)...

Since i'm already in a <?php tag and it wouldn't make sense to open another, how would i make "foreach" be recognized as a php reserved word and execute it as a loop?

Thanks - cmaier

Share this post


Link to post
Share on other sites
try to use { and } even its a single line loop...

Thank you,

http://TopLancers.com
Create your profile Today!
http://TopLancers.com/forum join now

Share this post


Link to post
Share on other sites
I do... heres the code near the foreach...

[code]

<TR>
<TD>
<UL class='menu_inner_list'>
foreach ($global_site_left_column_menu_2_content_array as $global_site_left_column_menu_2_item)
{
<LI>
<A class="$global_site_left_column_menu_2_item[2]" href="$global_site_left_column_menu_2_item[1]">$global_site_left_column_menu_2_item[0]</A>
</LI>
}
</UL>
</TD>
</TR>

[/code]

... but it just outputs it as if it were text.. when you view the page you see...

foreach (Array as ) {
}

Share this post


Link to post
Share on other sites
I don't see how the contence of the array is related to my question.. but here it is...

[code]
$global_site_left_column_menu_2_content_array =
array(
array('Link One', 'http://mydomain.com/subfolder/', 'default_menu'),
);
[/code]

My problem is that foreach(){} is not being interpreted as php code... my only question is how do i make the loop be parsed as php.

Share this post


Link to post
Share on other sites
Ok... to simplify what i'm trying to get accomplished i just wrote a small example (using if else this time)

Original Source:
[code]
<HTML>
<?php
$var1 = "THIS IS TEXT";
?>
<BODY>
<?php
  $code_block = <<<KEYWORD
    <H1>TEST H1</H1>
    if (!$var1)
{
<H2>TEST H2</H2>
}
    else
{
<H2>$var1</H2>
}
KEYWORD;
echo $code_block;
?>
</BODY>
</HTML>
[/code]


View-Source of Output
[code]
<HTML>
<BODY>
    <H1>TEST H1</H1>
    if (!THIS IS TEXT)
{
<H2>TEST H2</H2>
}
    else
{
<H2>THIS IS TEXT</H2>
}</BODY>
</HTML>
[/code]

If you just show me how to make the if/else function as php code i'll be able to figure it out for my foreach example... i just can't find a way to make this work.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.