Jump to content

foreach loop inside <<< style string


cmaier

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
Link to comment
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 ) {
}
Link to comment
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.
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.