Can someone give an example how the following task can be achieved smartly?

I have this Database Table with 2 fields
1. Brand_ID
2. Brand_Name

SQL = SELECT Brand_ID, Brand_Name from brands limit 0,12;

and I have to loop through it and place the values in the following HTML code

<ul class="w105 fltL mr10">
            <li><a href="[B]{BRAND_ID HERE}[/B]">[B]{BRAND_NAME HERE}[/B]</a></li>
            <li><a href="#"> Motorolla </a></li>
            <li><a href="#"> Blackbarry </a></li>
            <li><a href="#"> HTC </a></li>
        <ul class="w105 fltL mr10">
            <li><a href="#"> Apple</a></li>
            <li><a href="#"> LG </a></li>
            <li><a href="#"> Sony-Ericsson </a></li>
            <li><a href="#"> Samsung </a></li>
        <ul class="w105 fltL mr10">
            <li><a href="#"> China Mobile </a></li>
            <li><a href="#"> I-mate </a></li>
            <li><a href="#"> O2 </a></li>
            <li><a href="#"> Trend </a></li>

The challenge with this html code is that we have to loop between the UL tags 4 times and then move to the Next UL Tag to again loop 4 times until all 12 values are placed.

Any Ideas? :)

Can you Demonstrate how?

{for $outer=1 to 3}
  {for $inner=1 to 4}
    <li>{$foo[$inner * $outer - 1]}</li>