2

I am trying to loop through an array in PHP and at the same time, create some HTML to output.

When the loop first starts I need to output <li class="row"> I then need to add 3 divs then close the <li> and start the process again.

I have tried this,

<?php $count = 1; ?>
        <?php foreach ($results as $k => $v) : ?>
            <?php echo $count % 3; ?>
            <?php if ($count % 3 == 0) : ?>
            <li class="row">
            <?php endif; ?>
                <div class="grid_8">
                    <div class="candidate">
                        <div class="model_image shadow_50"></div>
                        <dl>
                            <dt><?php echo $v['first_name']; ?> <?php echo $v['surname']; ?></dt>
                            <dd>
                                <?php echo $v['talent']; ?>
                                <ul>
                                    <li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "View Details", array('class' => 'details')); ?></li>
                                    <li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "View Showreel", array('class' => 'showreel')); ?></li>
                                    <li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "Shortlist", array('class' => 'shortlist')); ?></li>
                                </ul>
                            </dd>
                        </dl>
                    </div>
                </div>
            <?php if ($count % 3 == 0) : ?>
            </li>
            <?php endif; ?>
            <?php $count ++; ?>
            <?php if($count >= 3) $count = 1; ?>
        <?php endforeach; ?>

however all my li get the class row.

Desired Output:

<li class="row">
            <div class="grid_8">
                <div class="candidate">
                    <div class="model_image shadow_50"></div>
                    <dl>
                        <dt>Jessica Womersley</dt>
                        <dd>
                            actress &amp; presenter
                            <ul>
                                <li><a href="" class="details">View Details</a></li>
                                <li><a href="" class="showreel">Showreel</a></li>
                                <li><a href="" class="shortlist">Shortlist</a></li>
                            </ul>
                        </dd>
                    </dl>
                </div>
            </div>
            <div class="grid_8">
                <div class="candidate">
                    <div class="model_image shadow_50"></div>
                    <dl>
                        <dt>Jessica Womersley</dt>
                        <dd>
                            actress &amp; presenter
                            <ul>
                                <li><a href="" class="details">View Details</a></li>
                                <li><a href="" class="showreel">Showreel</a></li>
                                <li><a href="" class="shortlist">Shortlist</a></li>
                            </ul>
                        </dd>
                    </dl>
                </div>
            </div>
            <div class="grid_8">
                <div class="candidate end">
                    <div class="model_image shadow_50"><span class="banner"></span></div>
                    <dl>
                        <dt>Jessica Womersley</dt>
                        <dd>
                            actress &amp; presenter
                            <ul>
                                <li><a href="" class="details">View Details</a></li>
                                <li><a href="" class="showreel">Showreel</a></li>
                                <li><a href="" class="shortlist">Shortlist</a></li>
                            </ul>
                        </dd>
                    </dl>
                </div>
            </div>
        </li>
2
  • Wait, what are you trying to do? Please post an example of the desired result. Commented Oct 11, 2011 at 16:11
  • Im not following the use of $count... You need to repeat the content for class="row" 3 times? Commented Oct 11, 2011 at 16:16

1 Answer 1

1

You are overcomplicating things. ¿Why not just use a for loop inside the for each? Like this

    <?php foreach ($results as $k => $v) : ?>
        <li class="row">
        <?php for($count = 1; $count <= 3; $count++) : ?>

            <div class="grid_8">
                <div class="<?php if($count == 3) echo "candidate_end"; else echo "candidate";?>">
                    <div class="model_image shadow_50"></div>
                    <dl>
                        <dt><?php echo $v['first_name']; ?> <?php echo $v['surname']; ?></dt>
                        <dd>
                            <?php echo $v['talent']; ?>
                            <ul>
                                <li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "View Details", array('class' => 'details')); ?></li>
                                <li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "View Showreel", array('class' => 'showreel')); ?></li>
                                <li><?php echo anchor("/candidates/card/" . strtolower($v['first_name']) . "-" . strtolower($v['surname']), "Shortlist", array('class' => 'shortlist')); ?></li>
                            </ul>
                        </dd>
                    </dl>
                </div>
            </div>
        <?php endfor; ?>
        </li>
    <?php endforeach; ?>
Sign up to request clarification or add additional context in comments.

3 Comments

This loops through the results array 3 times, so shows my results 3 times per result
no every div class="candidate"> should hold a different value from the results array
I made a small modification. will this make the desired output?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.