How to add an addtional count per 4 items

I would like to add another count that after every 4 items, it loops and create a added piece of script, by adding <ul><li> tags after every 4 items.

I want to addthis piece of code, every fours items after it close </div>


            <ul>				
               <li>
                <div class="box">
             <!--All fields and information here-->
                </div>
               <div class="h2"></div>
                <div class="box">
             <!--All fields and information here-->
                </div>
               <div class="h2"></div>
                <div class="box">
             <!--All fields and information here-->
                </div>
               <div class="h2"></div>
                <div class="box">
             <!--All fields and information here-->
                </div>
               <div class="h2"></div>
                   </li>
              </ul>

I currently have this piece of code, which builds a list per every <div class=“box”>, then closes the item </div> and start another one as same.
SEE current CODE.



 <?php
    $dealList = $this->dealList;
    if(count($dealList)==0) echo '<h3>'.JText::_('NO_DEAL_MESSAGE').'</h3>';
	$count = 1; 
	foreach ( $dealList as $row): 
		$link = 'index.php?xxx=' . $row->id;
		if(!Helper::is_urlEncoded($row->pic_dir))
		 {
		 	$imageUrl = $row->pic_dir;
		 }
		 else
		 {
			$imageUrlArr= unserialize(urldecode($row->pic_dir));
			$imageUrl = str_replace("\\\\","/",$imageUrlArr[0]);
		 }
?>

<div class="box">
information fields here 
</div>


  <?php if ($count%2==1){?>
     <div class="h2"></div>
    <?php }
    else if($count%2==0){?>
    <div class="h3"></div>
   <?php  }$count++ ; endforeach;?>

How do I added the Additonal count for efter four items <div class=box"> it places and start over with


<ul>
  <li>
     <div class="box">
      information fields 
     </div>
 </li>
</ul>

I think this is the principle you are seeking:


// create a spoof array of elements with range
$r = range(1,11);

$cnt = 1;  // set the counter
foreach($r as $v){
if($cnt % 4 === 0){  // use modulo % (look it up)
    echo '----' . PHP_EOL;  // when triggered, echo the line - or </ul><ul>
}
echo $v . PHP_EOL ;  // otherwise print the value
$cnt++;  // increment the count on 
}
gives:
----
1
2
3
4
----
5
6
7
8
----
9
10
11
// you'd have to close your last </ul> manually

You’d have to open your first <ul> manually too. Might need some adjustment for your needs - but modulo is the way to go.

You could put the code you want to repeat into an include file and then do something like below.

<?php

$count = 1;
for ($i = 0; $i < 10; $i++) {
    if ($count++ % 4 == 0) {
        include('includeFile.php');
    }
}
?>