【问题标题】:Change data in the while loop while looping循环时更改while循环中的数据
【发布时间】:2016-06-25 03:54:27
【问题描述】:

我正在使用 php,现在我正在将 while 循环应用于我的代码。 我正在从数据库中获取数据。 现在我必须在页面中的一个 Div 上应用该数据。

我的问题是循环中的 "div class="item active"" 每次活动类都需要。现在我想在第一个循环过程之后进行更改,当第二次开始时我想将该 div 更改为此 "div class="item""

我对这个循环过程并不陌生,所以我无法解决这个问题。需要帮忙。谢谢。

<?php 

                $sqlEvent = "SELECT * FROM eventdata WHERE id='$id'";

                $resultEvent = mysqli_query($connection, $sqlEvent);

                if (mysqli_num_rows($resultEvent) > 0) {

                while ($rowEvent = mysqli_fetch_array($resultEvent)) {

                ?><div class="item active"> <img src="images/event/1.jpg" alt="..." class="img-responsive">
                              <div class="carousel-caption wedding-area">
                                <h2><?php echo $rowEvent['eventTitle']; ?></h2>
                                <h4><?php echo $rowEvent['eventDate']; ?></h4>
                                <h4><?php echo $rowEvent['eventTime']; ?></h4>
                                <div class="details hidden-xs">
                                  <p><?php echo $rowEvent['eventDesc']; ?> </p>
                                </div>
                                <a href="#" class="btn btn-default" role="button">Read More</a> </div>
                            </div><?php

                }

                }

                ?>

【问题讨论】:

标签: php while-loop


【解决方案1】:

使用计数器检查第一次迭代

$i = 1;// initialized counter
while ($rowEvent = mysqli_fetch_array($resultEvent)) {
    if ($i == 1) {// check for first iteration
        ?><div class="item active"> <img src="images/event/1.jpg" alt="..." class="img-responsive">
    <?php } else { ?>

            <div class="item"> <img src="images/event/1.jpg" alt="..." class="img-responsive">
                <?php
            }
            $i++;// increase counter

        ?>

【讨论】:

  • 让我试试这个
  • 如果是,请提出问题。
【解决方案2】:

您只希望循环的第一个 div 具有“活动”类... 那么这段代码应该适合你

只需设置 variable = "actìve" 然后在主体循环的末尾设置 = ""。

在每次迭代中打印 div 的 class 属性中的变量

$sqlEvent = "SELECT * FROM eventdata WHERE id='$id'";

$resultEvent = mysqli_query($connection, $sqlEvent);

if (mysqli_num_rows($resultEvent) > 0) {
    $classActive = "active";
    while ($rowEvent = mysqli_fetch_array($resultEvent)) {
        ?>
            <div class="item <?php echo $classActive; ?>"> 
                <img src="images/event/1.jpg" alt="..." class="img-responsive">
                <div class="carousel-caption wedding-area">
                    <h2><?php echo $rowEvent['eventTitle']; ?></h2>
                    <h4><?php echo $rowEvent['eventDate']; ?></h4>
                    <h4><?php echo $rowEvent['eventTime']; ?></h4>
                    <div class="details hidden-xs">
                      <p><?php echo $rowEvent['eventDesc']; ?> </p>
                </div>
                <a href="#" class="btn btn-default" role="button">Read More</a> </div>
            </div>
        <?php
        $classActive = "";
    }

}

【讨论】:

    【解决方案3】:

    使用更容易使用的 pdo 函数

    http://php.net/manual/pl/pdostatement.fetchall.php

    并且总是在 sql 查询中转义值!

    <?php 
    
    
                 $sth = $dbh->prepare("SELECT * FROM eventdata WHERE id=?);
                 $sth->execute([$id]);
                 foreach ($sth->fetchAll(PDO::FETCH_ASSOC) as $i => $rowEvent) {
    
                ?><div class="item  <?php if(1 === $i) echo 'active';?>"> <img src="images/event/1.jpg" alt="..." class="img-responsive">
                              <div class="carousel-caption wedding-area">
                                <h2><?php echo $rowEvent['eventTitle']; ?></h2>
                                <h4><?php echo $rowEvent['eventDate']; ?></h4>
                                <h4><?php echo $rowEvent['eventTime']; ?></h4>
                                <div class="details hidden-xs">
                                  <p><?php echo $rowEvent['eventDesc']; ?> </p>
                                </div>
                                <a href="#" class="btn btn-default" role="button">Read More</a> </div>
                            </div><?php
    
                }
    
    
                ?>
    

    【讨论】:

    • 不要将链接发布为答案,而是添加一些文本来解释此答案如何帮助 OP 解决当前问题。谢谢
    猜你喜欢
    • 2018-08-13
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 2014-03-15
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    相关资源
    最近更新 更多