Link to home
Start Free TrialLog in
Avatar of brianmfalls
brianmfallsFlag for United States of America

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.
<div class="chaptertext" id="01" ng-class="{'openChapter': currentChapter == 01}">
    <div id="chapterContent01">
        <?php include '../docs/chapter01.md'; ?>
    </div>
</div>

Open in new window


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

Open in new window

Avatar of brianmfalls
brianmfalls
Flag of United States of America image

ASKER

Close, but not quite...  Seems to work, but now I'm seeing a 'no such file exists' error.

<?php
    for ($x = 10; $x <= 15; $x++) {
        echo '<div class="chaptertext" id="', $x, '" ng-class="{\'openChapter\': currentChapter == ', $x, '}">
            <div id="chapterContent', $x, '">';
        $filePath = '../docs/chapter' + $x + '.md';
        include $filePath;
        echo '</div>
        </div>';
    }
?> 

Open in new window

SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
I tagged my solution as 'best', because it's exactly what I needed.  :)