簡體   English   中英

我有 6 個產品,我想使用 php foreach 在每行顯示 3 個產品

[英]i have 6 products and i want to show 3 products on per row using php foreach

我正在使用 php 開發電子商務網站。 在那里我想使用 php foreach每行顯示 3 個產品(我有 6 個)。 這是我的代碼:

<div class="section">
    <div class="container">
        <?php
        $numOfCols = 4;
        $rowCount = 0;
        $bootstrapColWidth = 12 / $numOfCols;
        ?>
        <div class="row">
            <?php foreach ($results as $product) { ?>
            <div class="col-md-<?php echo $bootstrapColWidth;?>">
                <div class="well" style="width: 250px;height: auto;">
                    <div><?php echo'<img src="' . base_url().'uploads/' . $product- 
>product_image . '">'; ?></div>
                <?php echo $product->name;?> 
            </div>
        </div>
        <?php
        $rowCount++;
        if($rowCount % $numOfCols == 0) {echo '</div><div 
        class="row">'.'<br>'.'<br>';
        }
        ?>
        <?php };?>
    </div>
</div>

此代碼使其在單個列中顯示所有產品。 我不知道如何讓它在 2 列中顯示。

//initialize a counter 
$count = 0;

//start the table 
echo("<table><tr>"); 
foreach ($products as $product) {

//if it's divisible by 5 then we echo a new row 
if(($count % 5) == 0){

echo("</tr><tr>\n"); 
$count++;

}//if 
else{ 
$count++; 
}
echo("<td>$product<td>");

}//foreach

//close the table 
echo("</tr></table>");

您可以在產品數組上使用array_chunk()函數,該函數將數組拆分為新數組塊。 循環遍歷新數組並根據需要顯示產品。 有關array_chunk()更多信息,請訪問http://php.net/manual/en/function.array-chunk.php

暗示:

$productsArray = array_chunk($results,3);//chunks 3 products on each sub array
foreach($productsArray as $products)
{
  //loop here your products with <div class="row"></div>..
}

一個簡單的方法是使用計數器變量。 在循環之前,將計數器變量的值設置為 0,並在每次打印產品后增加它。

此外,將 foreach 循環體保持在 if 條件下,它將檢查計數器變量的值。 如果該值等於 3,則將其重置為 0,然后在添加新的</div><div class="row">標簽后打印產品。

代碼如下:

<div class="section">
    <div class="container">
        <div class="row">
            <?php
                $bootstrapColWidth = 12 / $numOfCols;
                //Setting the counter variable
                $counter = 0;
                foreach ($results as $product) { 
                    //Checking the value of counter variable and then implementing the corresponding code
                    if($counter == 3){
                        $counter=0;
            ?>
                        </div>
                        <div class="row">
            <?php
                    }else{
                        $counter++;
                    }
            ?>
            <div class="col-md-<?php echo $bootstrapColWidth;?>">
                <div class="well" style="width: 250px;height: auto;">
                    <div><?php echo'<img src="' . base_url().'uploads/' . $product->product_image . '">'; ?></div>
                <?php echo $product->name;?> 
                </div>
            </div>
        <?php };?>
        </div>
    </div>
</div>

希望這個邏輯對你有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM