Visualizzazione dei risultati da 1 a 9 su 9

Discussione: do while

  1. #1
    Utente di HTML.it
    Registrato dal
    May 2002
    Messaggi
    2,929

    do while

    ciao a tutti,

    qualcuno mi sa dire perchè mi brucia sempre un record?

    codice:
    function cat_staff($conn){
    	
    	$query = "SELECT *FROM `cat_staff`  ORDER BY `posizione` ASC";
    	$es = mysql_query($query, $conn) or die(mysql_error());
    	$totalRows_es = mysql_num_rows($es);
    
    	$stampa .='<tr>';
    	
    	if($totalRows_es > 0){
    	
    		$row_es = mysql_fetch_assoc($es);
    		$divido = $totalRows_es / 2;
    		$nm = floor($divido);
    		$xn = 0;
    		
    		$stampa .= '<td>';
    	
    		do{
    		
    		if($xn < $nm){
    		
    		$stampa .= '<table width="339" border="0" cellspacing="0" cellpadding="0">';
    		
    		$id = $row_es['id'];
    		$categoria = $row_es['categoria'];
    		$posizione = $row_es['posizione'];
    	
    		$query_staff = "SELECT *  FROM `staff` WHERE `id_staff` = '$id'";
    		$es_staff = mysql_query($query_staff, $conn) or die(mysql_error());
    		$totalRows_es_staff = mysql_num_rows($es_staff);
    
    			if($totalRows_es_staff > 0){
    
    				$stampa .= '<tr><td colspan="2" align="right" class="text_aran " style="padding-right:10px">'.$categoria.'</td></tr>
    							<tr><td colspan="2" align="right" bgcolor="#E67106" class="text_aran " style="padding-right:10px">[img]img/spacer.gif[/img]</td></tr>
    							<tr><td height="8" colspan="2">[img]img/spacer.gif[/img]</td></tr>';
    	
    				$row_es_staff = mysql_fetch_assoc($es_staff);
    				
    				do{
    	
    				$id_staff = $row_es_staff['id'];
    				$id_posizione = $row_es_staff['id_staff'];
    				$foto = $row_es_staff['foto'];
    				$descrizione = $row_es_staff['descrizione'];
    
    				$stampa .= '<tr bgcolor="#EEEEEE">
    								<td width="61" align="center">[img]img/staff/'.$foto.'[/img]</td>
    								<td style="padding-left:10px">variabile incremento: '.$xn.'
    n. colonne: '.$nm.'
    ris. totali: '.$totalRows_es.'
    Id Staff: '.$id_posizione.'</td></tr>
    								<tr><td colspan="2"></td>
    							</tr>';
    
    				}while($row_es_staff = mysql_fetch_assoc($es_staff));
    				
    			}
    			
    			$xn++;
    			$stampa .= '</table>';
    			
    		 }else{
    		 		$stampa .= '</td><td>';
    		 		$stampa .= '<table width="339" border="0" cellspacing="0" cellpadding="0">';
    				$xn = 0;
    		 }
    		
    		}while($row_es = mysql_fetch_assoc($es));
    		
    		$stampa .= '</td>';
    	}
    	
    	$stampa .='</tr>';
    	
    	return $stampa;
    }
    se i record sono 5 ne stampa 4, se sono 4 ne stampa 3....
    grazie mille

  2. #2
    if($totalRows_es > 0){

    $row_es = mysql_fetch_assoc($es);

    perche' il primo te lo fumi qui.....

    Il silenzio è spesso la cosa migliore. Pensa ... è gratis.

  3. #3
    anzi ... magari forse no. Ma hai troppi annidamenti e while per capire al volo di che si tratta esattamente...

    Di certo e' che estrai un record prima del ciclo while e quindi successivamente l'indice del buffer si sposta avanti di un record ....

    Il silenzio è spesso la cosa migliore. Pensa ... è gratis.

  4. #4
    Utente di HTML.it
    Registrato dal
    May 2002
    Messaggi
    2,929
    grazie mille per l'attenzione, adesso ho fatto cosi:
    codice:
    function cat_staff($conn){
    	
    	$query = "SELECT *FROM `cat_staff`  ORDER BY `posizione` ASC";
    	$es = mysql_query($query, $conn) or die(mysql_error());
    	$totalRows_es = mysql_num_rows($es);
    
    	$stampa .='<tr>';
    	
    	if($totalRows_es > 0){
    	
    		$divido = $totalRows_es / 2;
    		$nm = floor($divido);
    		$xn = 0;
    		
    		$stampa .= '<td>';
    	
    		while($row_es = mysql_fetch_assoc($es)){
    		
    		if($xn <= $nm){
    		
    		$stampa .= '<table width="339" border="0" cellspacing="0" cellpadding="0">';
    		
    		$id = $row_es['id'];
    		$categoria = $row_es['categoria'];
    		$posizione = $row_es['posizione'];
    	
    		$query_staff = "SELECT *  FROM `staff` WHERE `id_staff` = '$id'";
    		$es_staff = mysql_query($query_staff, $conn) or die(mysql_error());
    		$totalRows_es_staff = mysql_num_rows($es_staff);
    
    			if($totalRows_es_staff > 0){
    
    				$stampa .= '<tr><td colspan="2" align="right" class="text_aran " style="padding-right:10px">'.$categoria.'</td></tr>
    							<tr><td colspan="2" align="right" bgcolor="#E67106" class="text_aran " style="padding-right:10px">[img]img/spacer.gif[/img]</td></tr>
    							<tr><td height="8" colspan="2">[img]img/spacer.gif[/img]</td></tr>';
    	
    								
    				while($row_es_staff = mysql_fetch_assoc($es_staff)){
    	
    				$id_staff = $row_es_staff['id'];
    				$id_posizione = $row_es_staff['id_staff'];
    				$foto = $row_es_staff['foto'];
    				$descrizione = $row_es_staff['descrizione'];
    
    				$stampa .= '<tr bgcolor="#EEEEEE">
    								<td width="61" align="center">[img]img/staff/'.$foto.'[/img]</td>
    								<td style="padding-left:10px">variabile incremento: '.$xn.'
    n. colonne: '.$nm.'
    ris. totali: '.$totalRows_es.'
    Id Staff: '.$id_posizione.'</td></tr>
    								<tr><td colspan="2"></td>
    							</tr>';
    
    				}; // FINE WHILE
    				
    			}; // FINE IF
    			
    			$xn++;
    			$stampa .= '</table>';
    			
    		 }else{
    		 		$stampa .= '</td><td>';
    		 		$stampa .= '<table width="339" border="0" cellspacing="0" cellpadding="0">';
    				$xn = 0;
    		 }; // FINE IF
    		
    		}; // FINE WHILE
    		
    		$stampa .= '</td>';
    	}; // FINE IF
    	
    	$stampa .='</tr>';
    	
    	return $stampa;
    }

    queste sono le tabelle

    codice:
    -- 
    -- Struttura della tabella `cat_staff`
    -- 
    
    CREATE TABLE `cat_staff` (
      `id` int(11) NOT NULL auto_increment,
      `categoria` varchar(100) NOT NULL default '',
      `posizione` int(11) NOT NULL default '0',
      PRIMARY KEY  (`id`)
    ) TYPE=MyISAM AUTO_INCREMENT=6 ;
    
    -- 
    -- Dump dei dati per la tabella `cat_staff`
    -- 
    
    INSERT INTO `cat_staff` VALUES (1, 'Direzione', 1);
    INSERT INTO `cat_staff` VALUES (2, 'Amministrazione', 2);
    INSERT INTO `cat_staff` VALUES (3, 'Estero', 3);
    INSERT INTO `cat_staff` VALUES (4, 'Arredatori', 4);
    INSERT INTO `cat_staff` VALUES (5, 'Assistenza', 5);
    
    -- --------------------------------------------------------
    
    -- 
    -- Struttura della tabella `staff`
    -- 
    
    CREATE TABLE `staff` (
      `id` int(11) NOT NULL auto_increment,
      `id_staff` int(11) NOT NULL default '0',
      `foto` varchar(255) NOT NULL default '',
      `descrizione` text NOT NULL,
      PRIMARY KEY  (`id`)
    ) TYPE=MyISAM AUTO_INCREMENT=17 ;
    
    -- 
    -- Dump dei dati per la tabella `staff`
    -- 
    
    INSERT INTO `staff` VALUES (1, 1, 'direzione.jpg', 'STAFF 1');
    INSERT INTO `staff` VALUES (2, 2, 'direzione.jpg', 'STAFF 2');
    INSERT INTO `staff` VALUES (5, 2, 'direzione.jpg', 'STAFF 2');
    INSERT INTO `staff` VALUES (4, 2, 'direzione.jpg', 'STAFF 2');
    INSERT INTO `staff` VALUES (6, 2, 'direzione.jpg', 'STAFF 2');
    INSERT INTO `staff` VALUES (7, 3, 'direzione.jpg', 'STAFF 3');
    INSERT INTO `staff` VALUES (8, 3, 'direzione.jpg', 'STAFF 3');
    INSERT INTO `staff` VALUES (9, 3, 'direzione.jpg', 'STAFF 3');
    INSERT INTO `staff` VALUES (10, 4, 'direzione.jpg', 'STAFF 4');
    INSERT INTO `staff` VALUES (11, 4, 'direzione.jpg', 'STAFF 4');
    INSERT INTO `staff` VALUES (12, 4, 'direzione.jpg', 'STAFF 4');
    INSERT INTO `staff` VALUES (13, 5, 'direzione.jpg', 'STAFF 4');
    INSERT INTO `staff` VALUES (14, 5, 'direzione.jpg', 'STAFF 5');
    INSERT INTO `staff` VALUES (15, 5, 'direzione.jpg', 'STAFF 5');
    INSERT INTO `staff` VALUES (16, 5, 'direzione.jpg', 'STAFF 5');
    midaresti una mano....


    grazie

  5. #5
    Utente di HTML.it
    Registrato dal
    May 2002
    Messaggi
    2,929
    risolto!!!
    codice:
    function cat_staff($conn){
    	
    	$query = "SELECT *FROM `cat_staff`  ORDER BY `posizione` ASC";
    	$es = mysql_query($query, $conn) or die(mysql_error());
    	$totalRows_es = mysql_num_rows($es);
    
    	$stampa .='<tr>';
    	
    	if($totalRows_es > 0){
    	
    		$divido = $totalRows_es / 2;
    		$nm = floor($divido);
    		$xn = 0;
    		
    		$stampa .= '<td>';
    	
    		while($row_es = mysql_fetch_assoc($es)){
    		
    		
    		
    		$stampa .= '<table width="339" border="0" cellspacing="0" cellpadding="0">';
    		
    		$id = $row_es['id'];
    		$categoria = $row_es['categoria'];
    		$posizione = $row_es['posizione'];
    	
    		$query_staff = "SELECT *  FROM `staff` WHERE `id_staff` = '$id'";
    		$es_staff = mysql_query($query_staff, $conn) or die(mysql_error());
    		$totalRows_es_staff = mysql_num_rows($es_staff);
    
    			if($totalRows_es_staff > 0){
    
    				$stampa .= '<tr><td colspan="2" align="right" class="text_aran " style="padding-right:10px">'.$categoria.'</td></tr>
    							<tr><td colspan="2" align="right" bgcolor="#E67106" class="text_aran " style="padding-right:10px">[img]img/spacer.gif[/img]</td></tr>
    							<tr><td height="8" colspan="2">[img]img/spacer.gif[/img]</td></tr>';
    	
    								
    				while($row_es_staff = mysql_fetch_assoc($es_staff)){
    	
    				$id_staff = $row_es_staff['id'];
    				$id_posizione = $row_es_staff['id_staff'];
    				$foto = $row_es_staff['foto'];
    				$descrizione = $row_es_staff['descrizione'];
    
    				$stampa .= '<tr bgcolor="#EEEEEE">
    								<td width="61" align="center">[img]img/staff/'.$foto.'[/img]</td>
    								<td style="padding-left:10px">variabile incremento: '.$xn.'
    n. colonne: '.$nm.'
    ris. totali: '.$totalRows_es.'
    Id Staff: '.$id_posizione.'</td></tr>
    								<tr><td colspan="2"></td>
    							</tr>';
    
    				}; // FINE WHILE
    				
    			}; // FINE IF
    			
    			$xn++;
    			$stampa .= '</table>';
    			
    		 if($xn > $nm){
    		 		$stampa .= '</td><td>';
    		 		$stampa .= '<table width="339" border="0" cellspacing="0" cellpadding="0">';
    				$xn = 0;
    		 }; // FINE IF
    		
    		}; // FINE WHILE
    		
    		$stampa .= '</td>';
    	}; // FINE IF
    	
    	$stampa .='</tr>';
    	
    	return $stampa;
    }
    grazie ho riflettuto su quello che mi hai detto!

  6. #6
    azzolina... manco se fossi mia suocera....

    dove ti brucia il record???? su quale if/while ....

    Il silenzio è spesso la cosa migliore. Pensa ... è gratis.

  7. #7
    Originariamente inviato da Rossella_75
    risolto!!!
    grazie ho riflettuto su quello che mi hai detto!
    Grande.... sei meglio di mia suocera.... a saperlo prima...

    Il silenzio è spesso la cosa migliore. Pensa ... è gratis.

  8. #8
    Utente di HTML.it
    Registrato dal
    May 2002
    Messaggi
    2,929
    il problema era nell'if, nel momento in cui la condizione risultava vera saltava un record e ripartiva.

    Perchè mia suocera?

  9. #9
    Utente di HTML.it
    Registrato dal
    May 2002
    Messaggi
    2,929
    ops...
    ho risposta alla prima.


    grazie mitico! sei sempre disponibile

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.