Grupos dependiendo de cantidad de registros con while

 

  <?
  //cuento los registros de la consulta
$sqlw = "SELECT count(idt) as total FROM textos_r where idr=17   order by ubica asc";
$resw = @mysql_query($sqlw); if (!$resw) { exit(mysql_error()); }
$roww = mysql_fetch_array($resw);
 
$total=$roww['total'];
$conteo=1;
//limite por grupo
$limitar=4;
while 
$sql = "SELECT * FROM textos_r where idr=17   order by ubica asc";
                $res = @mysql_query($sql); if (!$res) { exit(mysql_error()); }
                 while ($row = mysql_fetch_array($res))  {
 
$i=$i+1;
 
if ($conteo==1) {
 
//armo grupos
?>
 
<div class="cont_item" >
 
   <div class="item_nav1" style="border:1px solid #333333;">
                        <p><?php echo utf8_encode($row['titulo']) ?></p>
                        <a href="<?php echo limpiar_url(utf8_encode($row['titulo'])) ?>_<?php echo utf8_encode($row['idt']) ?>_<?php echo utf8_encode($row['idr']) ?>_p">ver mas</a>
                    </div>           
<?php  } 
 
if ($conteo>1) {
?>       
 <div class="item_nav1" style="border:1px solid #333333;">
                        <p><?php echo utf8_encode($row['titulo']) ?></p>
                        <a href="<?php echo limpiar_url(utf8_encode($row['titulo'])) ?>_<?php echo utf8_encode($row['idt']) ?>_<?php echo utf8_encode($row['idr']) ?>_p">ver mas</a>
                    </div>  
      
  <?php }
  //cerrar primer divisor 
  if ($conteo==$limitar or $i==$total) { 
  
  ?>     
   </div>      
<?php 
//cierro grupos 
 }   
if($conteo==$limitar){ $conteo=1; } else {$conteo=$conteo+1;}
 }?>