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 & 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 & 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 & 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>
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; ?>
精彩评论