brianmfalls
asked on
Using PHP include withina PHP for loop
I need to output the code below fifty-two times. I could copy and paste it over and over again and change the numbers.... or... I could build a PHP for loop to do it for me. Then, if chapters are added later, all I have to do is up the ante, that is, change the loop to attribute.
This is the content for chapter 1. Please note that the chapter number MUST be in a two digit format.
I've tried this and that, but haven't had any success. Here's where I left off:
Note: I couldn't get past an error on the line of the include, so there is nothing there yet for formatting the number to a two digit format.
This is the content for chapter 1. Please note that the chapter number MUST be in a two digit format.
<div class="chaptertext" id="01" ng-class="{'openChapter': currentChapter == 01}">
<div id="chapterContent01">
<?php include '../docs/chapter01.md'; ?>
</div>
</div>
I've tried this and that, but haven't had any success. Here's where I left off:
Note: I couldn't get past an error on the line of the include, so there is nothing there yet for formatting the number to a two digit format.
<?php
for ($x = 1; $x <= 52; $x++) {
echo '<div class="chaptertext" id="', $x, '" ng-class="{\'openChapter\': currentChapter == ', $x, '}">
<div id="chapterContent', $x, '">'
include "../docs/chapter$x.md";
'</div>
</div>'
};
?>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You're welcome, glad you got it working.
@brianmfalls: When you get a chance, take a look at PHP HEREDOC notation - perfect for templates like this!
I am confused - why are you doing this in PHP and not using ng-repeat in Angular?
If you load your chapters into your model then using ng-repeat Angualr does all the work.
If you load your chapters into your model then using ng-repeat Angualr does all the work.
ASKER
I tagged my solution as 'best', because it's exactly what I needed. :)
ASKER
Open in new window