【问题标题】:How to echo two items per one time by using PHP Foreach?如何使用 PHP Foreach 一次回显两个项目?
【发布时间】:2016-04-18 18:13:50
【问题描述】:

我正在使用 PHP 从 DB 中回显我的产品。如果我们只使用 foreach 将得到每个循环一个项目的结果,但实际上我希望它每次回显两个项目,如下函数

这是我使用 foreach 从数据库中获取数据的 PHP 函数

我使用 row item 选择器来包装 product 选择器,所以我想回显 product 两次,然后它会回显 row item

示例:行项目 = 1 然后 产品 = 2

public function last_upated_products() {

    $data = $this->newest_products_from_db('products');
    $out = '';
    if ($data) {
        foreach ($data as $k => $row) {

            $out .= '<div class="row item">';

            $out .= '<div class="product">';
            $out .= '<div class="image">';
            $out .= '<a href=""><img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive"></a>';
            $out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
            $out .= '</div>';
            $out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4><a href="#">' . $row['prod_name'] . '</a></h4>';
            $out .= '<p>' . $row['prod_descrip'] . '</p>';
            $out .= '</div>';
            $out .= '</div>';

            $out .= '</div>';
        }
    }
    return $out;
}

此函数将在循环中回显一个项目。

【问题讨论】:

  • 您必须添加一堆if() 代码来检测您正在进行的迭代,并根据需要输出“脚手架”html。例如if ($row is odd) { start new div} output stuff; if ($row is even) { end new div}.
  • 我已经测试过 if($k$2 ==0){} 但不行

标签: php foreach


【解决方案1】:

您不能在一次循环中打印两次。您可以有条件的HTML 输出来完成这项工作。 考虑一下:

function last_upated_products() {

    $data = $this->newest_products_from_db('products');
    $out = '';
    $counter = 1;
    $length = count($data);
    if ($data) {
        foreach ($data as $k => $row) {

            if ($counter % 2 != 0) {
                $out .= '<div class="row item">';
            }

            $out .= '<div class="product">';
            $out .= '<div class="image">';
            $out .= '<a href=""><img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive"></a>';
            $out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
            $out .= '</div>';
            $out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4><a href="#">' . $row['prod_name'] . '</a></h4>';
            $out .= '<p>' . $row['prod_descrip'] . '</p>';
            $out .= '</div>';
            $out .= '</div>';


            if ($counter % 2 == 0 || $length == $counter) {
                $out .= '</div>';
            }

            $counter++;
        }
    }
    return $out;
}

【讨论】:

  • 当产品数量为奇数时,这是否包括最后的结束行 div?
  • @HengSopheak 添加了最后的收盘价div
  • 对于奇数的产品。
  • @Sumit 你怎么知道我已经累了一天但我做不到
  • @Sumit 我很抱歉,我不应该在喝咖啡之前使用 stackoverflow。我看到了底部,但没有意识到您从 1 而不是 0 开始计数。
【解决方案2】:

您可以使用modulus operator 进行检查。如果您的迭代器是 2 的倍数,它将输出适当的元素(它通过检查余数是否为零来做到这一点):

public function last_upated_products() {

    $data = $this->newest_products_from_db('products');
    $out = '';
    if ($data) {

        $i = 0; 

        foreach ($data as $k => $row) {

            if( ($i % 2) === 0) {
                $out .= '<div class="row item">';
            }
            $out .= '<div class="product">';
            $out .= '<div class="image">';
            $out .= '<a href=""><img src="' . base_url('asset/img/main/9.jpg') . '" alt="img" class="img-responsive"></a>';
            $out .= '<div class="promotion"><span class="discount">' . $row['prod_id'] . '% OFF</span> </div>';
            $out .= '</div>';
            $out .= '<div class="description"><div class="price"><span>$' . $row['prod_price'] . '</span></div><h4><a href="#">' . $row['prod_name'] . '</a></h4>';
            $out .= '<p>' . $row['prod_descrip'] . '</p>';
            $out .= '</div>';
            $out .= '</div>';

            if( ($i + 1) % 2 === 0 || ($i + 1) === count($data) ) {
                $out .= '</div>';
            }

            $i++;
        } 
    }
    return $out;
}

请注意,最后一位 ($i + 1) === count($data) 在您的集合包含奇数的情况下很重要。

【讨论】:

  • @HengSopheak 确实,正在更新。
  • @HengSopheak 已更新,之前关闭 div 的逻辑存在缺陷。
  • 天哪,你怎么会知道这个逻辑?我尝试了越来越多的时间,但我找不到解决方案,为什么您可以使用此代码来解决此问题。我真的很想成为像你这样的专业人士。我能怎么做?有什么有用的网站或在线课程可供我学习以提高我的知识?
猜你喜欢
  • 2016-08-08
  • 2012-08-15
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
  • 2017-01-13
  • 2012-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多