I have made a function which sort of works, it displays okay but not really how it's suposed to lol.

Here's the code:
PHP Code:
<?php
//Description to make rows of 3 then start new row

function getProductList($itemsPerPage 12$itemsPerRow 3$returnPagination true$random false)
{
    global 
$pageLink;
                        
    if (
$random == true)
    {
        
$sql "SELECT `pd_id`, `cat_id`, `pd_name`, `pd_description_short` 
                FROM `tbl_product` WHERE `pd_statues` = 'show' ORDER BY RAND()"
;
    }
    else
    {
        
$sql "SELECT `pd_id`, `cat_id`, `pd_name`, `pd_description_short` 
                FROM `tbl_product` WHERE `pd_statues` = 'show' ORDER BY `pd_date`"
;
    }
        
    
$result mysql_query($pageLink->getPagingQuery($sql$itemsPerPage)) or die(mysql_error());
        
    if (
$returnPagination == true)
    {
        
$paginLink $pageLink->getPagingLink($sql$itemsPerPage);
    }
        
    
$numItem mysql_num_rows($result);
    
    if (
$numItem 0)
    {
        for (
$i 0$i $numItem$i++)
        {                
            if (
$i $itemsPerRow == 0)
            {
                
$prod .= '<div id="modules">' "\n";
            }
            
            
$c 0;
            while (
$row = @extract(mysql_fetch_array($result)))
            {
                
$prod .= '<div id="mod'$c .'">' "\n" 
                            
'<div class="padding">' .
                                
'<div class="text">' .
                                    
'<b><a href="./view.php?id='.$pd_id.'" title="'.$pd_name.'">'$this->shortenLengh($pd_name43).'</a></b>' .
                                    
'<p>' $this->shortenLengh($pd_description_short140) . '</p>' .
                                
'</div>' .
                            
'</div>' .
                        
'</div>' "\n";
                
$c++;
                if (
$c == $itemsPerRow){ $c 0;}
            }
            
            if (
$i $itemsPerRow == $itemsPerRow 1)
            {
                
$prod .= '</div>';
            }
            
        }
            
        if (
$i $itemsPerRow 0)
        {
            
$prod .= '</div>';
        }
    }

    if (
$returnPagination == true)
    {
        return 
$prod '<p align="center">'$paginLink .'</p>';
    }
    else
    {
        return 
$prod;
    }
}

//Usage: getProductList(12,3,true,false);
?>
Every 3 products i would like it to repeat the process so at the beginning display:
Code:
<div id="modules">
then every 3 the same thing.

It prints out like this atm:
Code:
<div id="modules">
<div id="mod0"><div class="padding"><div class="text"><!-- Content !--></div></div></div>
<div id="mod1"><div class="padding"><div class="text"><!-- Content !-->/div></div></div>
<div id="mod2"><div class="padding"><div class="text"><!-- Content !--></div></div></div>
<!-- Now should close and start it again with <div id="modules"> !-->

<div id="mod0"><div class="padding"><div class="text"></div></div></div>
</div><div id="modules">
</div>
I've tryed all diffrent things to make it work please help