简体   繁体   English

如何使用PHP Foreach每次回显两个项目?

[英]How to echo two items per one time by using PHP Foreach?

I'm using PHP to echo my products from DB. 我正在使用PHP来回应来自DB的产品。 If we just use foreach will get the result one item per a loop but, Actually I want it echo two items per one times as below function 如果我们只使用foreach将得到每个循环一个项目的结果,但实际上我希望它每次回显两个项目,如下所示

This is my PHP function using foreach to fetch data from DB 这是我的PHP函数,使用foreach从DB获取数据

I've used row item selector to wrap product selector so I want to echo block of product two times then it will echo row item . 我用行项目选择包装的产品选择,所以我想呼应产品的块,然后两次将呼应行项目

Example: row item = 1 then product = 2 示例:row item = 1 then product = 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;
}

This function will echo one item for a loop. 此函数将回显一个项目以进行循环。

You can not print two times in one iteration of loop. 您不能在一次循环迭代中打印两次。 You can conditional HTML output to do this job. 您可以通过条件HTML输出来完成此任务。 Consider this: 考虑一下:

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;
}

You can use the modulus operator to make the check. 您可以使用模数运算符进行检查。 If your iterator is a multiple of two it will output the appropriate elements (it does this by checking that the remainder is zero): 如果你的迭代器是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;
}

Note that the last bit ($i + 1) === count($data) is important in the event that your set holds an uneven number. 请注意,如果您的集合包含不均匀的数字,则最后一位($i + 1) === count($data)非常重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM