Reputation: 5802
My images gallery has a xhtml like below:
<div class="row">
<ul class="ul">
<li><a href="#"><img src=....></a></li>
<li><a href="#"><img src=....></a></li>
<li><a href="#"><img src=....></a></li>
</ul>
</div>
<div class="row">
<ul class="ul">
<li><a href="#"><img src=....></a></li>
<li><a href="#"><img src=....></a></li>
<li><a href="#"><img src=....></a></li>
</ul>
</div>
I want to do this: Insert only 3 <li>
in <ul>
and close the after 3 <li>
What is the best way to do this? position ($pos = 0, $pos++) , array_chunk or anything else?
Thanks in advance
Upvotes: 1
Views: 306
Reputation: 157872
You have to prepare array of data.
We way you prepare it doesn't really matter.
You can use $pos++ and set column markers in the data array.
and then output in in the template like this:
<? foreach ($DATA as $row): ?>
<? if($row['ul']): ?>
<div class="row">
<ul class="ul">
<? endif ?>
<li><a href="<?=$row['url']?>"><img src="<?=$row['src']?>"></a></li>
<? if($row['/ul']): ?>
</ul>
</div>
<? endif ?>
or you can use array_chunk and then use nested foreaches:
<? foreach ($DATA as $arr): ?>
<div class="row">
<ul class="ul">
<? foreach ($arr as $row): ?>
<li><a href="<?=$row['url']?>"><img src="<?=$row['src']?>"></a></li>
<? endforeach ?>
</ul>
</div>
<? endforeach ?>
The latter one looks neat, I have to admit
Upvotes: 1
Reputation: 1320
you can do
for ($i=0 ; $i < 5; $i++){
echo '<div class="row">
<ul class="ul">';
for ($j=0;$j<3;$j++){
echo '<li><a href="#"><img src=....></a></li>';
}
echo '</ul>
</div>';
}
Upvotes: 0