PHP 代码以两种不同的显示方式显示行


php code to display rows in two different display

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

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

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

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

我唯一需要的是在第一个div中显示两行,然后在第二个div中显示另一行。

是 这是可能的,如果其他或任何其他条件...怎么做...

但是由于页面设计目的,请保持所有div标签不变......

<!-- 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 -->

是的,这是可能的。假设您的$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
}

可以使用 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>

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

<!-- 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 -->