Add header and footer every n times (php loop)


/ Published in: PHP
Save to your folder(s)

Simple loop for adding header and footer avery n times in loop. Useful for printing tables.


Copy this code and paste it in your HTML
  1. /* how many items to loop: */
  2. $count = 6;
  3. /* how many items in row: */
  4. $n = 3;
  5. for( $i=1; $i<=$count; $i++ ) {
  6. // add header
  7. if( $i%$n === 1 || $i === 1 ) {
  8. echo 'header<br>';
  9. }
  10.  
  11. // echo content
  12. echo "content no. $i<br>";
  13.  
  14. // add footer
  15. if( $i%$n === 0 || $i === $count ) {
  16. echo 'footer<br>';
  17. }
  18. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.