Hi, haven't been here in ages. I have an issue with PHP when trying to print out an object of values from my database. The values are categories actually which appear in groups.

The html looks like this:

PHP Code:
 <div class="cat-block-group">

   <
div class="cat-block">
     <
h2>Security</h2>
     <
ul class="cat-list">
       <
li>one</li>   
       <
li>two</li>   
       <
li>three</li>   
     </
ul>
   </
div>
   
   <
div class="cat-block">
     <
h2>Hardware</h2>
       <
ul class="cat-list">
         <
li>one</li>   
     <
li>two</li>   
     <
li>three</li>   
      </
ul>
    </
div>

    </
div
So it prints a group, then two blocks then ends the group. Each block contains a category such as Security plus all the security subcategories. The HTML/css works find and prints great, the problem I have is in printing it all out programatically.

The database looks like this:

PHP Code:
[cat_id]  [catname]  [subcatname]
1            Security      Viruses
2            Security      Firewalls
3            Hardware   Troubleshooting etc 
All this is changed so it can look nice and simple for the forum.

My PHP currently looks like this. It's really complicated, I've tried to use a series of booleans to simplify things.

I've commented it throughout:

PHP Code:
<? 
   $last='';
     $pos=0;
      $prevCat='';$prevCat2='';
    
      ?>
    <?     
         $i=2;//Begins at one because of next 'if'
        $j=0;//index
        $lastCat='Security';
      foreach($cats->result() as $key =>$value)
        {
            if($j==0)//First time through loop
            {
                $startBlock=true;
                $endBlock=false;
                $closingBlock=false;
            }
            else//Not first time
            {
                if($lastCat==$value->catname)//Same main category as last time = No new group / block needed.
                {
                    $startBlock=false;//Start a new <ul> container & <div class="cat-block">
                    $endBlock=false;//Finish <ul> & </div>
                    $closingBlock=false;//Close group of 2 blocks, move to new row.
                }
                else//Different category reached, new group / block needed.
                {
                    if($i==1)//Position left=1, pos right=2
                    {
                        $i=2;
                        $startBlock=true;
                        $endBlock=false;
                        $closingBlock=true;
                    }
                    elseif($i==2)
                    {
                        $i=1;
                        $closingBlock=true;
                        $startBlock=true;
                        $endBlock=false;                        
                    }
                }                    
            }

            if($closingBlock)//Close off last group
            {
                ?>
                        </ul>
                    </div>
                <?
            }
            
            if($i==1)//Left
            {
                if($startBlock)//New block & required
                {
                    ?>
                    <div class="cat-block">
                        <h2><?=$value->catname?></h2>
                        <ul class="cat-list">
                <?
        }
        ?>
                            <li><a href="<?=base_url().$value->sname.'/'.$value->subcat_id;?>/"><?=$value->sname;?></a></li>   
                <?
        if($endBlock)//New block & required
        {
        ?>
                        </ul>
                    </div>
                    <?
                }
            }
            elseif($i==2)//Right
            {
                if($startBlock)//New block & required
                {
                ?>
        <div class="cat-block">
          <h2><?=$value->catname?></h2>
          <ul class="cat-list">
        <?
                }
                ?>
            <li><a href="<?=base_url().$value->sname.'/'.$value->subcat_id;?>/"><?=$value->sname;?></a></li>   
        <?
                if($endBlock)//New block & required
                {
                ?>
          </ul>
        </div>
                <?
                }
            }
            
            $j++;
            $lastCat=$value->catname;//Record last category for next time.
        }
?>
I've been at this one for weeks and I'm stuck, any help much appreciated.