Visualizzazione dei risultati da 1 a 6 su 6
  1. #1

    Tabella alimentata da 2 cicli while...si può?

    Ciao ragazzi,
    come da titolo topic. Vi posto il codice che non funziona:

    codice:
    <?php
    echo '
    
    
    ';
    
    $qry3="SELECT blablablablabla";
    $result3 = mysql_query($qry3);
    $qry4="SELECT blablabla";
    $result4 = mysql_query($qry4);
    
    echo '
    <CENTER><table><table border="1" cellpadding="8" cellspacing="5">
    <tr>
    <td>Limiti</td>
    <td>Potenzialità</td>
    <td></td>
    <td></td>
    </tr>
     
    ';
     
    while ($row3 = mysql_fetch_array($result3))
    while ($row4 = mysql_fetch_array($result4))
    {
    	echo '
    <tr>
    <td>' . $row3['caratteristiche'] . '</td>
    <td>' . $row4['caratteristiche'] . '</td>
    ';
    }
     
    echo '</table></CENTER>
     
    ';
    
    ?>

    Ovviamente i singoli cicli while producono correttamente un output. Il problema è combinarli assieme in modo che il primo popoli una colonna ed il secondo l'altra (nella medesima tabella).

    Qualche anima pia vuol darmi una dritta?



    Tiziano

  2. #2
    Utente di HTML.it
    Registrato dal
    Apr 2004
    Messaggi
    3,709
    non si può così... o fai un ciclo oppure:

    Codice PHP:
    $row = array();
    $i 0;
    while (
    $row3 mysql_fetch_array($result3)) {
      
    $row[$i++][0] = $row3['caratteristiche'];
    };
    $i 0;
    while (
    $row4 mysql_fetch_array($resul4)) {
      
    $row[$i++][1] = $row4['caratteristiche'];
    };
    foreach (
    $row as $cols) {
        echo 
    '
        <tr>
        <td>' 
    $cols[0] . '</td>
        <td>' 
    $cols[1] . '</td>
        '
    ;
    }; 
    supponendo le lunghezze coincidano, altrimenti devi aggiungere dei controlli

  3. #3
    E se facessi fare tutto ad sql?
    codice:
    <?php
    echo '
    
    
    ';
    
    $qry3="SELECT caratteristiche AS LIMITI FROM utenti JOIN caract ON aspetto1 = valori WHERE aspetto1 LIKE '1%' AND id='$_GET[id]' UNION SELECT caratteristiche AS POTENZIALITA FROM utenti JOIN caract ON aspetto1 = valori WHERE aspetto1 LIKE '2%' AND id='$_GET[id]'";
    $result3 = mysql_query($qry3);
    
    echo '
    <CENTER><table><table border="1" cellpadding="8" cellspacing="5">
    <tr>
    <td>LIMITI</td>
    <td>POTENZIALITA</td>
    <td></td>
    <td></td>
    </tr>
     
    ';
     
    while ($row3 = mysql_fetch_array($result3))
    {
    	echo '
    <tr>
    <td>' . $row3['LIMITI'] . '</td>
    <td>' . $row3['POTENZIALITA'] . '</td>
    
    ';
    }
     
    echo '</table></CENTER>
     
    ';
    
    ?>
    In questo modo la colonna LIMITI mi viene riempita....nella colonna POTENZIALITA' non vedo nulla (e dovrebbe esserci un valore invece!) e php mi restituisce l'errore che recita come l'indice POTENZIALITA non è definito....?????????

  4. #4
    Ciao,
    ho seguito la tue indicazione così:

    Codice PHP:
    <?php
    $qry3
    ="SELECT caratteristiche FROM utenti JOIN caract ON aspetto1 = valori WHERE aspetto1 LIKE '1%' AND id='$_GET[id]' UNION SELECT caratteristiche FROM utenti JOIN caract ON aspetto1 = valori WHERE aspetto2 LIKE '1%' AND id='$_GET[id]'";
    $result3 mysql_query($qry3);
    $qry4="SELECT caratteristiche FROM utenti JOIN caract ON aspetto1 = valori WHERE aspetto1 LIKE '2%' AND id='$_GET[id]' UNION SELECT caratteristiche FROM utenti JOIN caract ON aspetto1 = valori WHERE aspetto2 LIKE '2%' AND id='$_GET[id]'";
    $result4 mysql_query($qry4);
    $row = array(); 
    $i 0
    while (
    $row3 mysql_fetch_array($result3)) { 
      
    $row[$i++][0] = $row3['caratteristiche']; 
    }; 
    $i 0
    while (
    $row4 mysql_fetch_array($result4)) { 
      
    $row[$i++][1] = $row4['caratteristiche']; 
    }; 

    echo 
    '
    <CENTER><table><table border="1" cellpadding="8" cellspacing="5">
    <tr>
    <td>[b]LIMITI[/b]</td>
    <td>[b]POTENZIALITA\'[/b]</td>
    </tr>
    '
    ;
    foreach (
    $row as $cols) { 
        echo 

        <tr> 
        <td>' 
    $cols[0] . '</td> 
        <td>' 
    $cols[1] . '</td> 
        </tr>
        '

    }
    echo 
    '</table></CENTER>
    '
    ;
    ?>
    E vedo valorizzata soltanto la colonna LIMITI.
    Il debug di php mi restituisce il seguente errore:
    codice:
    Notice: Undefined offset: 1 in C:\Users\X\Desktop\mowes_portable\www\test\potenzlimit_solegio.php on line 1112
    Dove sbaglio?

    Tiziano



    [QUOTE]Originariamente inviato da eiyen
    non si può così... o fai un ciclo oppure:

    Codice PHP:
    $row = array();
    $i 0;
    while (
    $row3 mysql_fetch_array($result3)) {
      
    $row[$i++][0] = $row3['caratteristiche'];
    };
    $i 0;
    while (
    $row4 mysql_fetch_array($resul4)) {
      
    $row[$i++][1] = $row4['caratteristiche'];
    };
    foreach (
    $row as $cols) {
        echo 
    '
        <tr>
        <td>' 
    $cols[0] . '</td>
        <td>' 
    $cols[1] . '</td>
        '
    ;
    }; 
    supponendo le lunghezze coincidano, altrimenti devi aggiungere dei controlli [/QUOTE

  5. #5
    Risolto in modo SPORCO E CATTIVO:
    ho creato una tabella statica con due celle ed ho schiaffato due cicli while di creazione tabella ognuno dentro ad una cella....il risultato è un'apparente tabella con due colonne...in realtà è una tabella statica con due colonne ed ognuna delle due colonne ospita una tabella dinamica con una colonna sola...non so se rendo l'idea...ma funziona!

  6. #6
    Utente di HTML.it
    Registrato dal
    Apr 2004
    Messaggi
    3,709
    mah... secondo me è meglio correggere lo script dove ti da errore sull'indice 1...

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.