开发者

PHP image gallery and items in row

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>

  &l开发者_C百科t;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


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜