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

前端 未结 2 892
孤街浪徒
孤街浪徒 2021-01-14 23:06

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 func

相关标签:
2条回答
  • 2021-01-14 23:48

    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.

    0 讨论(0)
  • 2021-01-14 23:58

    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;
    }
    
    0 讨论(0)
提交回复
热议问题