如何循环2 div并且只包含2个内容/文章/新闻

时间:2015-02-16 04:29:23

标签: php for-loop while-loop

我创建网站使用模板, 但我很困惑 while loop / for loop 这个模板的脚本之一

这个循环 2 div但只包含2个内容/文章/新闻

此屏幕截图

http://i.stack.imgur.com/0OHsV.png

此代码

<?php 
$data_news = mysql_query("select * from news");
while($news=mysql_fetch_object($data_news)){
?>

<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                        <ul>
                      <li class="clearfix">
                        <div class="scale_image_container">
                          <a href="#"><img src="images/flex_small_img1.jpg" alt="" class="scale_image"></a>
                        </div>
                        <div class="post_text">
                          <a href="#"><h4><?php echo $news->title ?></h4></a>
                        <div class="event_date"><?php echo tgl($news->datenews) ?></div>
                        </div>
                      </li>
                      <li class="clearfix">
                        <div class="scale_image_container">
                          <a href="#"><img src="images/img1.jpg" alt="" class="scale_image"></a>
                        </div>
                       <div class="post_text">
                          <a href="#"><h4><?php echo $news->title ?></h4></a>
                        <div class="event_date"><?php echo tgl($news->datenews) ?></div>
                        </div>
                      </li>
                        </ul>
                      </div>


<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                    <ul>
                      <li class="clearfix">
                        <div class="scale_image_container">
                          <a href="#"><img src="images/flex_small_img1.jpg" alt="" class="scale_image"></a>
                        </div>
                        <div class="post_text">
                          <a href="#"><h4><?php echo $news->title ?></h4></a>
                        <div class="event_date"><?php echo tgl($news->datenews) ?></div>
                        </div>
                      </li>
                      <li class="clearfix">
                        <div class="scale_image_container">
                          <a href="#"><img src="images/img1.jpg" alt="" class="scale_image"></a>
                        </div>
                        <div class="post_text">
                          <a href="#"><h4><?php echo $news->title ?></h4></a>
                        <div class="event_date"><?php echo tgl($news->datenews) ?></div>
                        </div>
                      </li>
                    </ul>
       </div>
<?php } ?>

我如何 while loop / for loop 就像我的截图一样?

谢谢:)

1 个答案:

答案 0 :(得分:0)

1你可以使用这样的东西,请尝试

<?php 
$data_news = mysql_query("select * from news");
$count=1;
while($news=mysql_fetch_object($data_news)){
?>

        <?php if($count % 2!=0 ) {?>
                <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
                <ul>
                <?php } ?>

                  <li class="clearfix">
                    <div class="scale_image_container">
                      <a href="#"><img src="images/flex_small_img1.jpg" alt="" class="scale_image"></a>
                    </div>
                    <div class="post_text">
                      <a href="#"><h4><?php echo $news->title ?></h4></a>
                    <div class="event_date"><?php echo tgl($news->datenews) ?></div>
                    </div>
                  </li>

            <?php if($count % 2==0) {?>
                </ul></div>
                <?php } ?>

 <?php  $count++;} ?>