Categories: Snippets

Adding divs to a foreach loop every 4 times

The output look like:

<div>
   kicks brand
   kicks brand
   kicks brand
   kicks brand
</div>
<div>
   kicks brand
   kicks brand
   kicks brand
   kicks brand
</div>
<div>
   kicks brand
   kicks brand
   kicks brand
   kicks brand
</div>

 

 

$count = 1;
foreach( $array_var as $var ) 
{
    if ($count%4 == 1)
    {  
         echo "<div>";
    }
    // Code
    if ($count%4 == 0)
    {
        echo "</div>";
    }
    $count++;
}
if ($count%4 != 1) echo "</div>"; //This is to ensure there is no open div if the number of elements in user_kicks is not a multiple of 4
Share

Recent Posts

Clear the browser cache of CSS or JavaScript Using PHP

Other than caching every hour, or every week, you may cache according to file data.…

7 years ago

Current Site URL – Codeigniter

To get host url of current server simply replace application\config\config.php [crayon-6925d16171a6f147387411/] with [crayon-6925d16171a76562106606/]  

7 years ago

PHP: fopen error handling

You should first test the existence of a file by file_exists(). [crayon-6925d16171c86129885076/] or simple solution…

7 years ago

Random String PHP

[crayon-6925d16171f27916821184/] Output the random string with the call below: [crayon-6925d16171f2d300176110/]  

7 years ago

PHP function to make slug (URL string)

Note: from WordPress Use it like this: [crayon-6925d16172699146575507/] Code: [crayon-6925d1617269f324079173/]  

7 years ago

Include all files with in the folder

[crayon-6925d161729e9590730784/]  

7 years ago