php代码在两个不同的显示中显示行

时间:2014-12-16 16:15:04

标签: php html css

我创建了两个设计来显示新闻。

第一个DIV 显示BIGGER图像和单独的css类,只显示两行。

第二个DIV 显示较小的图片和单独的css类并显示五行..

我从查询中按asc顺序选择行。 我的查询是= select * from news order by priority asc.

我唯一的需要是在First div中显示两行,然后在Second DIV中显示另一行。

这是可以使用if else或任何其他条件......如何做到这一点.........

但保留所有div标签,因为它是页面设计目的的bcoz ......

<!-- FIRST DIV -->
    <div class="post">
          <div class="buffer">
           <?php foreach($top2 as $top){    ?>
            <div class="content"> <a href="detail.html"><img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" style="width:285px; height:100px;" /></a>
              <h2><a href="detail.html"><?php echo htmlspecialchars_decode($top['headline']); ?></a></h2>        
            </div>
            <?php } ?>
             <!--<p class="details2"><a href="#">8 Comments</a> / <a href="#">Read More</a></p>-->
          </div>
        </div>  
        <!-- end post -->

<!-- SECOND DIV -->
        <!-- begin post -->
        <div class="post">  
          <div class="buffer">
          <?php foreach($top2 as $top){ ?>
            <div class="content1"><a href=""><img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" /></a>
              <h2><a href=""><?php echo htmlspecialchars_decode($top['headline']); ?></a></h2>        
            </div>
        <?php } ?>

          </div>
        </div>
        <!-- end post -->

3 个答案:

答案 0 :(得分:0)

是的,这是可能的。假设您的$top2数组具有标准的数字/顺序键,那么:

$split = 2;
for($i = 0; $i < $split; $i++) {
   ... print $top2[0] and $top2[1]
}

for ($i = $split; $i < count($top2); $i++) {
   ... print the rest of the rows
}

答案 1 :(得分:0)

可以使用array_slice()

<!-- FIRST DIV -->
<div class="post">
<div class="buffer">
    <?php $arr = array_slice($top2, 0, 2) ?>
    <?php foreach($arr as $top){    ?>
        <div class="content"> <a href="detail.html"><img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" style="width:285px; height:100px;" /></a>
            <h2><a href="detail.html"><?php echo htmlspecialchars_decode($top['headline']); ?></a></h2>
        </div>
    <?php } ?>
    <!--<p class="details2"><a href="#">8 Comments</a> / <a href="#">Read More</a>   </p>-->
</div>
</div>
<!-- end post -->

<!-- SECOND DIV -->
<!-- begin post -->
<div class="post">
<div class="buffer">
    <?php $arr = array_slice($top2, 2) ?>
    <?php foreach($arr as $top){ ?>
        <div class="content1"><a href=""><img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" /></a>
            <h2><a href=""><?php echo htmlspecialchars_decode($top['headline']); ?></a></h2>
        </div>
    <?php } ?>
</div>

答案 2 :(得分:0)

添加一个在foreach循环中递增的计数,然后在超过两个时断开:

<!-- FIRST DIV --> <div class="post"> <div class="buffer"> <?php $rowCount = 0; foreach($top2 as $top){ ?> <div class="content"> <a href="detail.html"><img src="bhaskarcms/uploads/<?php echo $top['photo']; ?>" style="width:285px; height:100px;" /></a> <h2><a href="detail.html"><?php echo htmlspecialchars_decode($top['headline']); ?></a></h2>
</div> <?php $rowCount++; if($rowCount > 2){ break; } } ?> <!--<p class="details2"><a href="#">8 Comments</a> / <a href="#">Read More</a></p>--> </div> </div>
<!-- end post -->