Jump to content


Photo

foreach loop inside <<< style string


  • Please log in to reply
5 replies to this topic

#1 cmaier

cmaier
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 13 August 2006 - 10:14 PM

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

<?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;
?>

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

#2 toplancers

toplancers
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 13 August 2006 - 10:25 PM

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



#3 cmaier

cmaier
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 13 August 2006 - 10:31 PM

I do... heres the code near the foreach...


								<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>


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

foreach (Array as ) {
}

#4 hostfreak

hostfreak
  • Members
  • PipPipPip
  • Advanced Member
  • 581 posts

Posted 13 August 2006 - 10:42 PM

Post the array as well please.

#5 cmaier

cmaier
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 13 August 2006 - 11:20 PM

I don't see how the contence of the array is related to my question.. but here it is...

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

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.

#6 cmaier

cmaier
  • Members
  • Pip
  • Newbie
  • 8 posts

Posted 13 August 2006 - 11:30 PM

Ok... to simplify what i'm trying to get accomplished i just wrote a small example (using if else this time)

Original Source:
<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>


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

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users