简体   繁体   中英

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

I'm using PHP to echo my products from 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

This is my PHP function using foreach to fetch data from 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

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. 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):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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