questo script non mi funziona ha un conportamento molto strano

Codice PHP:
<?
$i
=0$nn=array();
while (
$w mysql_fetch_assoc($ris)) { 
    
    
$nn[] = $w['id_acquisto'];
    
    if (
$i!=0) {
    
        
$rr $i-1;
    
        if (
$nn[$i-1]!=$nn[$i]) {
            
            echo 
'<table width="100%"  border="1">';
    
        }
            
    } else {
    
        echo 
'<table width="100%"  border="1">';
    
    }
?>
  <tr>
    <th width="2%" scope="col"></th>
    <th width="9%" scope="col"><span class="Stile11">Utente</span></th>
    <th width="8%" scope="col"><span class="Stile11">Data</span></th>
    <th width="8%" scope="col"><span class="Stile11">Prodotto </span></th>
    <th width="8%" scope="col"><span class="Stile11">Quantità Prodotto</span></th>
    <th width="8%" scope="col"><span class="Stile11">Importo</span></th>
  </tr>
  <tr class="Stile9"><? $i++; 
  
$utente get_user_from_id($w['id_utente']);
  
$data $w['giorno']."/".$w['mese']."/".$w['anno']." ".$w['ore'].":".$w['minuti'].":".$w['secondi'];
  
?>
    <th width="2%" scope="col"><?=$i;?></th>
    <th width="9%" scope="col"><span class="Stile11"><?=$utente;?></span></th>
    <th width="8%" scope="col"><span class="Stile11"><?=$data;?></span></th>
    <th width="8%" scope="col"><span class="Stile11">[img]<?=$sito.$w[[/img]' height="150"></span></th>
    <th width="8%" scope="col"><span class="Stile11"><?=$w['
quantit_prodotto'];?></span></th>
    <th width="8%" scope="col"><span class="Stile11"><?=$w['
costo'];?></span></th>
  </tr>
  <?
   $soldi1 = $soldi1 + $w['
costo'];
   $soldi2 = $soldi2 + $w['
costo'];
 
    if ($i!=0) {
        
        if ($nn[$i-1]!=$nn[$i]) {
    
            echo "</table>";
            
        }
    
    } else {
    
        echo '
</table>';
    }

$i++;
il problema è che non stampa <table> quando deve e che mi stampa un casino di
per esempio
Notice: Undefined offset: 7 in /home/lxuser/htdocs/lavoro/foto/fatturato.php on line 216

help me!

il codice che crea problemi è questo
if ($i!=0) {

$rr = $i-1;

if ($nn[$i-1]!=$nn[$i]) {

echo '<table width="100%" border="1">';

}

} else {

echo '<table width="100%" border="1">';

}


in pratica ho una tabella dove ho gli acquisti di un carrello, i pezzi acquistati però gli metto in una tabella con record separati ma con un id_acquisto comune, voglio stampare i dati im modo che ci sia una tabella per ogni id_acquisto