Jump to content

include() statement inside a for loop - aint working...


elias

Recommended Posts

 

Hi guys and gals!

 

I have various pages with slightly different code inside a for() loop:

 

		for ($i = 0; $i < $sn; $i = $i + 1){//first loop...			
			$detailsp = array();           
            			$detailsp = explode(" ", $lines[$xn]);
             //lots of code here.....
                        }

 

		for ($i = 0; $i < $sn; $i = $i + 1){	//first loop...
			$detailsp = array(); ;          
            			$detailsp = explode(" ", $lines[$xn]);
			$dummy2 = array_shift($detailsp);
             //lots of code here.....
                        }

 

I would like to just make one page with an include() statement to "include" the part of the code that changes.

 

		include 'compGlob_chnk1.txt' ;
             //lots of code here.....
                        }

 

where: compGlob_chnk1.txt, contains:

 

<?php


		for ($i = 0; $i < $sn; $i = $i + 1){	//first loop...		
			$detailsp = array();           
            			$detailsp = explode(" ", $lines[$xn]);		

?>

 

but I get an error, it seems the code "included" is not parsed as part of the for loop.

 

any help greatfully welcome :-)

 

cheers!!

 

elias

Thanks guys

 

khr2003: if that is the case, that you cannot include "parts of a for loop", then the application of include() here is worthless, as the for loop is 446 lines, and the point of the exercise was that as 400 of the 446 lines where common, I would have only like a "header" included depending on which application I needed.

 

so, is there another neat way of achieving the same effect?:

 

I have three or more pages of code, where, say 40 lines are page specific (and contain the beginnings of teh for loop) and 400 are common (html tables showing the results, etc). How can I use the same page for the three pages, where an if() statement sorts out which code-header I need.

 

I am not sure if that was clear - I hope so.

 

TIA

 

elias

It's quite difficult to see exactly what your objective is. To my knowledge, the only real requirement is that the opening curly bracket and the closing curly bracket of a language construct are in the same file. So you could possibly do something like this...

 

File1.php

<?
for($i = 1; $i < 10; $i++){
include('File2.php');
}
?>

 

But I'm assuming your saying that in your case this isn't suitable?

 

File2.php

echo $i;
// or whatever code you want here

 

Archived

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

×
×
  • 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.