Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2013
    Messaggi
    19

    checkbox per estrarre righe dal database

    Ho un database MySQL contenente una tabella (entries) strutturata in due campi:
    type | text
    dove "type" può avere 5 valori, e "text" è una stringa di testo.

    Tramite un form di checkboxes vorrei selezionare dal database le stringhe con uno o più type:

    codice:
    <html>
     <head>
      <title>select</title>
      <META http-equiv="Content-Type" content="text/html; charset=latin-1">
     </head>
     <body bgcolor="#F5FAE6">
    [img]img/pencil.png[/img]
    <center>
    <h2><p align="center">Make your test</p></h2>
    </center>
    
    <form action="output.php" method="POST">
    ✔Select the type(s) of exercise you need:
    
    
    
    <table border="1" cellpadding='4' cellspacing='4' style='border-collapse: collapse' bordercolor='#9999DD'>
    <tr><td><input type="checkbox" name="type[]" value="abc"/> multiple choice</td></tr>
    <tr><td><input type="checkbox" name="type[]" value="error"/> mistake correction</td></tr>
    <tr><td><input type="checkbox" name="type[]" value="cloze"/> cloze</td></tr>
    <tr><td><input type="checkbox" name="type[]" value="makeq"/> make a question</td></tr>
    <tr><td><input type="checkbox" name="type[]" value="trans"/> translate (IT-->EN)</td></tr>
    </table>
    
    <p align="center">
    <input type="submit" name= "get" value="get your entries!"/>
    </p>
    
    </form>
    </body>
    </html>
    Purtroppo, il seguente codice PHP mi restituisce solo le stringhe relative ad un "type", ignorando del tutto la selezione multipla di più checkboxes:

    Codice PHP:
    <html>
     <head>
      <title>output</title>
      <META http-equiv="Content-Type" content="text/html; charset=latin-1">

    </head>
    <body bgcolor="#EBEBFF">

    <center>
    <h2><p align="center">Here are the entries for your test!</p></h2>
    </center>

    <?
    $objConnect 
    mysql_connect("localhost","root","password") or die("No DB to select.");
    $objDB mysql_select_db("exercises");
    $tipo $_POST['type'];
    for (
    $i=0$i<sizeof($tipo);$i++) {
    $strSQL "SELECT * FROM entries WHERE type = '".$tipo[$i]."'"
    $objQuery mysql_query($strSQL);
    }
    ?>

    <?
    $tipo
    =$_POST['type'];
    if (isset(
    $tipo)){
    echo 
    "You selected the exercise type(s):"."
    "
    ;
    foreach (
    $tipo as $key => $value )
        echo 
    "- ".$value."
    "
    ;
    }
    else{
        echo 
    "You didn't check anything."."
    "
    ;
    }
    ?>




    <?
    if (isset($_POST['type'])){
     while (
    $row mysql_fetch_array($objQuery)) {
        echo 
    $row['text']." --> ".$row['focus']."
    "
    ;  
    }
    }
    else{
        echo 
    "Please select at least one type.";
    }
    ?>

    <?
    mysql_close
    ($objConnect);
    ?>

    </body>
    </html>
    Avete qualche suggerimento? Forse sto sbagliando a formulare la query?
    Grazie.

  2. #2
    Utente di HTML.it L'avatar di bstefano79
    Registrato dal
    Feb 2004
    Messaggi
    2,520
    è sbagliato tutto il concetto del codice, prendi i risultati solo dell'ultima quey eseguita per questo ti prende un solo type, poi il controllo isset va fatto subito e sul vettore post, e poi devi eseguire la query e subito prendere i risultati

    Codice PHP:
    if(isset($_POST['type']))
    {
         
    //OK
         
    $tipo $_POST['type']; 
        
    // poi fai il foreach sul tipo
       
    foreach ($tipo as $key => $value 
       {
          
    //a questo punto costruisco la query e la eseguo
           
    $strSQL "SELECT * FROM entries WHERE type = '".$value."'";  
           
    $objQuery mysql_query($strSQL); 

           
    //e poi prendo i risultati di ogni query
            
    while ($row mysql_fetch_array($objQuery)) 
            { 
                echo 
    $row['text']." --> ".$row['focus']."
    "
    ;   
            } 
           
       } 
    }
    else
    {
       
    //ERRORE


  3. #3
    Utente di HTML.it L'avatar di boots
    Registrato dal
    Oct 2012
    Messaggi
    1,626
    oppure, ancora meglio:

    Codice PHP:
    $values=array();
    foreach(
    $_POST['type'] as $t){
        
    $values[] = mysql_real_escape_string($t);
    }
    $cond "type= '".implode("' OR type='"$values) ."'";
    $strSQL "SELECT * FROM entries WHERE $cond ";   
    $objQuery mysql_query($strSQL);  

    while (
    $row mysql_fetch_array($objQuery))  
    {  
         echo 
    $row['text']." --> ".$row['focus']."
    "
    ;    

    così fai solo una query

  4. #4
    Utente di HTML.it
    Registrato dal
    Jun 2013
    Messaggi
    19
    Grazie mille ad entrambi. Le vostre soluzioni funzionano. Ora ci ragiono un po' sopra.

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 © 2024 vBulletin Solutions, Inc. All rights reserved.