多次显示数组中的每个项目

时间:2013-11-23 17:24:04

标签: php html

大家好,这似乎是一个非常基本的问题, 我有一个类别数组,并希望每次显示4次。它们都向左浮动,因此每个类别显示为一行4个框。

然而,有一些div是带有边框的框,每次迭代都会显示。它似乎工作,但我不知道这是否是正确的方式,因为所有 类别没有组织。我希望每次循环跳转到数组中的下一个项时,特定类别的标题首先出现; 欢迎任何帮助。请参阅下面的代码。

[CODE]
<section class="prodList">
                <?php  
                 $loop = 4;
                 $prodNum = 0;


                 $categories =    array("All","Clothes","Gadgets","Games");


                 foreach($categories as $cat){                       
                    echo "<h2> new cat: ".$cat."</h2>";
                     while($prodNum < count($categories)*$loop){


                            echo "<div class='viewerWishlistProdContainer'>

                                        <div class='prodImageContainer'>

                                        </div>

                            </div>
                            ";
                             echo "  <p>
                                            <button class='sortBtns' onClick='alert(\'See all V'\)'>See all V</button>

                                        </p>";

                            $prodNum ++;                        

                         }



                 }

[/ CODE]

1 个答案:

答案 0 :(得分:0)

这会有用吗?

<section class="prodList">
<?php  

$categories =    array("All","Clothes","Gadgets","Games");


foreach($categories as $cat){                      
  /** Since you know it should repeat four times, why not echo 4 times? */ 
  echo "<h2> new cat: ".$cat."</h2>";
  echo "<div style='display:inline-block;padding:5px'>". $cat . "</div>"; 
  echo "<div style='display:inline-block;padding:5px'>". $cat . "</div>";
  echo "<div style='display:inline-block;padding:5px'>". $cat . "</div>";
?>
<!-- You don't have to echo plain HTML -->
<div class='viewerWishlistProdContainer'>

  <div class='prodImageContainer'>

  </div>
</div>
<p>
    <button class='sortBtns' onClick='alert(\'See all V'\)'>See all V</button>
</p>    
<?php  } /** closing the foreach block */ ?>
</section>