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 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳