pee_aych_pee Posted May 22, 2007 Share Posted May 22, 2007 I'm probably attempting something illegal, but I haven't found any coverage on such a topic. My aim is to have a for() loop divided up into 2 separate includes with markup in between. ... the first include file: <?php // (include1.php) for($i=0;$i<5;$i++){ ?> ... and the second include file: <?php // (include2.php) } ?> ... and the main page looks like: <?php include(include1.php); ?> <div><?= $i; ?></div> <? include(include2.php); ?> The first include has the beginning of the for() loop, whereas the second include has the closing brace. I am guessing this sort of thing is not possible with PHP, but maybe I'm doing it wrong. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/52558-solved-is-a-2-part-loop-possible/ Share on other sites More sharing options...
per1os Posted May 22, 2007 Share Posted May 22, 2007 So you have tried this being unsuccesful. I do not think it will work, for one it is ill-logical, why would you need it in 2 seperate includes? Two, I think that the includes may, may throw a syntax error. and three, if you are using PHP 5 this will not work at all given that in the main page you are using 3 different ways to do the data. If it does work I would suggest revamping the code like so: <?php include('include1.php'); echo '<div>'.$i.'</div>'; include('include2.php'); ?> Make it more efficient and look friendlier. If you have not tried it yet, I would suggest you do that. If you have and it was un-succesful, well there is your answer. Quote Link to comment https://forums.phpfreaks.com/topic/52558-solved-is-a-2-part-loop-possible/#findComment-259355 Share on other sites More sharing options...
pee_aych_pee Posted May 23, 2007 Author Share Posted May 23, 2007 The purpose is this - I need to loop through a number of iterations and display tabular data. To make the code reusable, I want to encapsulate the start of the loop, which contains mod function, etc, in the first include(), and the end of the loop in the second include(). The inner section is merely markup with a few php variables echoed inside. These need to be customizable so that my associates can easily change the markup without tampering with the looping structure & functions. Perhaps I'm trying to dumb it down far too much. Regardless of the reason, I am certainly getting the expected error from the first include(). Since there is no closing brace in the loop which starts in the include, it errs out: Parse error: parse error, unexpected $end in c:\xampp\htdocs\testing\index.php on line 31 But, when I take the code out of the two separate includes, and place them in the parent document, it works nicely. So, there is no syntactical problems with the code. I guess I was going out on a limb on this one to accommodate my coworkers. I am exploring dynamic variables and perhaps even a way of using eval() to generate the desired output, but haven't found it yet. I'll post again here once the answer is found. Otherwise, I will resolve this thread. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/52558-solved-is-a-2-part-loop-possible/#findComment-259839 Share on other sites More sharing options...
kathas Posted May 23, 2007 Share Posted May 23, 2007 since this works: <?php for ($i=1; $i<5; $i++) { ?> html here <?=$i ?> <?php } ?> i see no reason why yours won't work... and it doesn't matter that on their own the includes cause a parse error... but really why do you need the includes??? Quote Link to comment https://forums.phpfreaks.com/topic/52558-solved-is-a-2-part-loop-possible/#findComment-259850 Share on other sites More sharing options...
pee_aych_pee Posted May 23, 2007 Author Share Posted May 23, 2007 The only "need" for includes is to allow my associates to simply type in 'include(filename);' rather than the entire contents of the file. And after they have included the first file, they can modify the markup with echoed variables which are subject to change. Then, they simply close the loop by typing in 'include(secondfilename);' It keeps them from mucking up the code, for it's far easier to edit the markup. Oh, and by the way, the example I posted is a dramatically simplified version of the first include. It's not only a for loop. I scaled it back just for readability in this forum. Quote Link to comment https://forums.phpfreaks.com/topic/52558-solved-is-a-2-part-loop-possible/#findComment-259908 Share on other sites More sharing options...
per1os Posted May 23, 2007 Share Posted May 23, 2007 Given what you said, a function would be better suited. Try this: //function.php <?php function loop($begin, $end="", $numtimes) { if ($numtimes < 1) { die('Numtimes must be greater than 0.'); } for ($i=1; $i<$numtimes;$i++) { echo $begin . $i . $end; } } ?> // main.php <?php include('function.php'); loop('BEGIN HTML HERE', 'END HTML HERE', 5); echo '<br />'; loop('<div id="', '">Some Information Here</div><br />', 5); echo '<br />'; loop('HTML HERE', '', 5); ?> Quote Link to comment https://forums.phpfreaks.com/topic/52558-solved-is-a-2-part-loop-possible/#findComment-259915 Share on other sites More sharing options...
biohazardep Posted May 23, 2007 Share Posted May 23, 2007 Maybe you can do it like this: <?php for ($i=1; $i<5; $i++) { include('dumb_coworkers_edit_this_file.php'); } ?> Then you can have the "markup with a few php variables echoed inside" in the dumb_coworkers_edit_this_file.php file, and have your coworkers edit that file without messing the php code. Quote Link to comment https://forums.phpfreaks.com/topic/52558-solved-is-a-2-part-loop-possible/#findComment-259922 Share on other sites More sharing options...
per1os Posted May 23, 2007 Share Posted May 23, 2007 Maybe you can do it like this: <?php for ($i=1; $i<5; $i++) { include('dumb_coworkers_edit_this_file.php'); } ?> Then you can have the "markup with a few php variables echoed inside" in the dumb_coworkers_edit_this_file.php file, and have your coworkers edit that file without messing the php code. On that note, you can revise my function above to use an include file instead. like so: // function.php <?php function loop($include, $numtimes) { if ($numtimes < 1) { die('Numtimes must be greater than 0.'); } for ($i=1; $i<$numtimes;$i++) { include($include); } } ?> //include.php <?php echo 'This is the ' . $i . ' time this is ran<br />'; ?> //main.php <?php include('function.php'); loop('include.php', 5); ?> Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/52558-solved-is-a-2-part-loop-possible/#findComment-259927 Share on other sites More sharing options...
448191 Posted May 23, 2007 Share Posted May 23, 2007 Yes splitting up a loop is possible using the alternative syntax. <?php for ($i=1; $i<5; $i++): ?> <?php echo 'foo'; ?> <?php endfor; ?> Quote Link to comment https://forums.phpfreaks.com/topic/52558-solved-is-a-2-part-loop-possible/#findComment-259997 Share on other sites More sharing options...
pee_aych_pee Posted May 25, 2007 Author Share Posted May 25, 2007 Absolutely great! Everyone here has contributed greatly and given me several viable options - all of which will be implemented either in my present project, or in future ones. It may have seemed like a rudimentary question - but it was perplexing. I am steering toward the solution given by frost110 & biohazardep...having the markup in the include() which is nested inside the loop. This makes sense, for it will still isolate the easy stuff and preserve the more complex code from getting damaged by noob tinkering. Nice function idea BTW. Although, the last example given by 448191 is definitely inviting - and I will certainly play around with it. Thanks again - phpfreaks is the best! Quote Link to comment https://forums.phpfreaks.com/topic/52558-solved-is-a-2-part-loop-possible/#findComment-261308 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.