【问题标题】:Get two results while looping trough associative array in PHP在 PHP 中循环遍历关联数组时获得两个结果
【发布时间】:2020-02-24 06:50:53
【问题描述】:

我想在 PHP 中使用 while 循环通过关联数组时一次获得两个结果。 我需要每行回显两个结果,如下所示:

<?
    while($row = mysqli_fetch_assoc($result)) {
    $client_name = $row['client_name'];
    $review = $row['review'];
    echo('
        <div class="row">
        <div>
            <p>'.$client_name.'</p>
            <p >'.$review.'</p>
        </div>
        <div>
            <p>'.$client_name.'</p>
            <p >'.$review.'</p>
        </div>
        </div>
    ');
    }
    }
?>

现在它给了我两次相同的结果,而不是下一次。

【问题讨论】:

    标签: php arrays mysqli


    【解决方案1】:

    您可以使用计数器来控制外部div 的输出,这样您就可以得到两个内部div 输出,每个外部输出:

    $i = 0;
    while($row = mysqli_fetch_assoc($result)) {
        $client_name = $row['client_name'];
        $review = $row['review'];
        if ($i % 2 == 0) echo '<div class="row">';
        echo '<div><p>'.$client_name.'</p><p>'.$review.'</p></div>';
        if ($i % 2 == 1) echo '</div>';
        $i++;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-26
      • 2012-03-03
      • 2013-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-05
      • 2019-07-10
      相关资源
      最近更新 更多