doudao2407 2016-09-05 11:44
浏览 31
已采纳

如何在同一时间迭代2个div?

I am trying to iterate through 2 divs at the same time to echo out data from the database. Each div has it's own style and I want to keep iterating without duplicate each div twice, without losing my divs style. Even when I delete any post from my database, I don't want to lose div design Arrangement (odd/even display), any help or advice?

// class getAll to get all posts from db tables

class getALL { 
  private $pdo;

  public function __construct() {
    $this->pdo = db::getInstance();
  }

  public function get() {
    $sql = "SELECT * FROM `posts`";
    $q = $this->pdo->prepare($sql);
    $q->execute();
    if($q->rowCount()) {
      return $q->fetchAll(PDO::FETCH_OBJ);
    }
    return false;
  } 
}

here the part of the 2 divs

// the 2 divs with iteration

<? if(is_array($getArticles) || is_object($getArticles)): ?>
<? foreach($getArticles as $getArticle): ?>
<? if($getArticle->id % 2 == 0) { ?>  //odd/even logic 
    <!-- One -->
        <article id="one" class="post style1">
            <div class="image">
                <img src="<?='images/'.$getArticle->image_name?>" alt="" data-position="75% center" />
            </div>
            <div class="content">
                <div class="inner">
                    <header>
                        <h2><a href="post.html"><?=$getArticle->title?></a></h2>
                        <p class="info"><?=$getArticle->post_time?> by <a href="#"><?=$getArticle->username?></a></p>
                    </header>
                    <p><?=$getArticle->post?></p>
                    <ul class="actions">
                        <li><a href="post.php" class="button alt">Read More</a></li>
                    </ul>
                </div>
            </div>
        </article>

<? }else{ ?>

    <!-- two -->
        <article id="four" class="post invert style2 alt">
            <div class="image">
                <img src="<?='images/'.$getArticle->image_name?>" alt="" data-position="60% center" />
            </div>
            <div class="content">
                <div class="inner">
                    <header>
                        <h2><a href="post.php"><?=$getArticle->title?></a></h2>
                        <p class="info"><?=$getArticle->post_time?> by <a href="#"><?=$getArticle->username?></a></p>
                    </header>
                    <p><?=$getArticle->post?></p>
                    <ul class="actions">
                        <li><a href="post.php" class="button alt">Read More</a></li>
                    </ul>
                </div>
            </div>
        </article>

<? } ?>
<? endforeach; ?>
<? endif; ?>
  • 写回答

1条回答 默认 最新

  • duanshan5259 2016-09-05 12:04
    关注

    Instead of $getArticle->id, use a simple $counter variable to combine two divs together and to implement odd-even logic. That way, when you delete a post, it won't destroy the div arrangement. Here's the solution:

    <?php $counter = 0; ?>
    <?php if(is_array($getArticles) || is_object($getArticles)): ?>
    <?php foreach($getArticles as $getArticle): ?>
    
    <article id=<?php if($counter % 2 == 0){ echo "one"; }else{ echo "four"; } ?> class="post<?php if($counter % 2 == 0){ echo " style1"; }else{ echo " invert style2 alt"; } ?>">
        <div class="image">
            <img src="<?='images/'.$getArticle->image_name?>" alt="" data-position="<?php if($counter % 2 == 0){ echo "75%"; }else{ echo "60%"; } ?> center" />
        </div>
        <div class="content">
            <div class="inner">
                <header>
                    <h2><a href="post.php"><?=$getArticle->title?></a></h2>
                    <p class="info"><?=$getArticle->post_time?> by <a href="#"><?=$getArticle->username?></a></p>
                </header>
                <p><?=$getArticle->post?></p>
                <ul class="actions">
                    <li><a href="post.php" class="button alt">Read More</a></li>
                </ul>
            </div>
        </div>
    </article>
    
    <?php ++$counter; ?>
    <?php endforeach; ?>
    <?php endif; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?