代码之家  ›  专栏  ›  技术社区  ›  dennismonsewicz

PHP循环问题

  •  0
  • dennismonsewicz  · 技术社区  · 14 年前

    我有一个应用程序,其中包含一个用户上传的图片画廊页面。我试图使用foreach循环显示页面上的图像,但是在构建foreach循环时遇到了一些问题。

    这就是HTML的形成方式

    <div class="item">
         <ul>
         <li><a href="images/gallery/love1.jpg" rel="example1" ><img src="images/gallery/thumb_love1.jpg" alt="#" /></a></li>
         <li><a href="images/gallery/love2.jpg" rel="example1" ><img src="images/gallery/thumb_love2.jpg" alt="#" /></a></li>
         <li><a href="images/gallery/love3.jpg" rel="example1" ><img src="images/gallery/thumb_love3.jpg" alt="#" /></a></li>
         <li><a href="images/gallery/love4.jpg" rel="example1"><img src="images/gallery/thumb_love4.jpg" alt="#" /></a></li>
         <li><a href="images/gallery/love5.jpg" rel="example1"  ><img src="images/gallery/thumb_love5.jpg" alt="#" /></a></li>
         <li><a href="images/gallery/love6.jpg" rel="example1"><img src="images/gallery/thumb_love6.jpg" alt="#" /></a></li>
         <li><a href="images/gallery/life1.jpg" rel="example1" ><img src="images/gallery/thumb_life1.jpg" alt="#" /></a></li>
         <li><a href="images/gallery/life2.jpg" rel="example1"><img src="images/gallery/thumb_life2.jpg" alt="#" /></a></li>
         <li><a href="images/gallery/life3.jpg" rel="example1"><img src="images/gallery/thumb_life3.jpg" alt="#" /></a></li>
         </ul>
        </div><!-- end item -->
    

    所以基本上当LI命中9个项目时,中断并开始class=“item”的新DIV

    下面是我一直尝试使用的PHP代码

    <?php
                    $x = range(1,100);
                    $counter = 1;
                    foreach($x as $item):
                    if($item == 9) {
                ?>
                    <div class="item">
                        <ul>
                            <?php foreach($pictures->result() as $p): ?>
                             <li><a href="images/gallery/<?=$p->category;?>/<?=$p->photo_name;?>" rel="example1" ><img src="images/gallery/<?=$p->category;?>/thumb_<?=$p->photo_name;?>" alt="#" /></a></li>
                        <?php endforeach; ?>
                        </ul>
                    </div><!-- end item -->
                    <?php $counter = 1; 
                    } else {
                        $counter++;
                    } ?>
                <?php endforeach; ?>
    

    我什么都试过了,但想不出怎样才能成功。谢谢你的帮助!

    3 回复  |  直到 14 年前
        1
  •  1
  •   Galen    14 年前

    如果这是不正确的,那么您要使用的是 array_chunk

    未经测试。。。

    $picture_chunks = array_chunk( $pictures->result(), 9 ); // split the long array into a multidimensional array with 9 objects in each
    
    <?php foreach( $picture_chunks as $chunk ): ?> // loop through the outer array creating the <div><ul></ul></div>
    <div class="item">
        <ul>
        <?php foreach( $chunk as $p ): ?> //loop through the inner array creating the LIs
            <li><a href="images/gallery/<?=$p->category;?>/<?=$p->photo_name;?>" rel="example1" ><img src="images/gallery/<?=$p->category;?>/thumb_<?=$p->photo_name;?>" alt="#" /></a></li>
        <?php endforeach; ?>
        </ul>
    </div>
    <?php endforeach; ?>
    
        2
  •  0
  •   dmgig    14 年前

    如果你需要让它在每九个项目上开始一个新的div,你需要使用一个模数。

    for($i=0;$i<10;$i++)
    {
      if($i % 9)
      {
       <li><a href="images/gallery/<?=$p->category;?>/<?=$p->photo_name;?>" rel="example1" ><img src="images/gallery/<?=$p->category;?>/thumb_<?=$p->photo_name;?>" alt="#" /></a></li>
       echo '</div>;
       echo '<div>';
      }else{
        <li><a href="images/gallery/<?=$p->category;?>/<?=$p->photo_name;?>" rel="example1" ><img src="images/gallery/<?=$p->category;?>/thumb_<?=$p->photo_name;?>" alt="#" /></a></li>
      }
    }
    
        3
  •  0
  •   TH1981    14 年前

    我想你可能太复杂了。试试下面的。我没有添加你的画廊变量,因为我无法测试他们,但它应该工作,因为你想要的。

            <div class="item"> <ul>
    <?php
        foreach(range(1,100) as $item){
            if($item%9 == 0){
    ?></ul></div>
            <div class="item"> <ul>
    <?php   } ?>
        <li> insert gallery output here <?php echo $item ?></li>
    <?php   
        }
    
    ?>          
    </ul></div>