Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    655

    calcolo dei radio button

    ciao a tutti, ho creato 12 domande a scelta multipla.

    Per mostrare il risultato ho utilizzato questo codice :

    codice:
    if (isset($_POST['submit']))
    {
    $a=0;
    $b=0;
    $c=0;
    //prima domanda 
    if ($_POST['domanda_1'] == "a")
    { 
          $a++;
    }	  
    if ($_POST['domanda_1'] == "b")
    { 
          $b++;
    }
    if ($_POST['domanda_1'] == "c")
    { 
          $c++;
    }
    //seconda domanda 
    if ($_POST['domanda_2'] == "a")
    { 
          $a++;
    }	  
    if ($_POST['domanda_2'] == "b")
    { 
          $b++;
    }
    if ($_POST['domanda_2'] == "c")
    { 
          $c++;
    }
    //terza domanda 
    if ($_POST['domanda_3'] == "a")
    { 
          $a++;
    }	  
    if ($_POST['domanda_3'] == "b")
    { 
          $b++;
    }
    if ($_POST['domanda_3'] == "c")
    { 
          $c++;
    }
    //quarta domanda 
    if ($_POST['domanda_4'] == "a")
    { 
          $a++;
    }	  
    if ($_POST['domanda_4'] == "b")
    { 
          $b++;
    }
    if ($_POST['domanda_4'] == "c")
    { 
          $c++;
    }
    //quinta domanda 
    if ($_POST['domanda_5'] == "a")
    { 
          $a++;
    }	  
    if ($_POST['domanda_5'] == "b")
    { 
          $b++;
    }
    if ($_POST['domanda_5'] == "c")
    { 
          $c++;
    }
    //sesta domanda 
    if ($_POST['domanda_6'] == "a")
    { 
          $a++;
    }	  
    if ($_POST['domanda_6'] == "b")
    { 
          $b++;
    }
    if ($_POST['domanda_6'] == "c")
    { 
          $c++;
    }
    //settima domanda 
    if ($_POST['domanda_7'] == "a")
    { 
          $a++;
    }	  
    if ($_POST['domanda_7'] == "b")
    { 
          $b++;
    }
    if ($_POST['domanda_7'] == "c")
    { 
          $c++;
    }
    //ottava domanda 
    if ($_POST['domanda_8'] == "a")
    { 
          $a++;
    }	  
    if ($_POST['domanda_8'] == "b")
    { 
          $b++;
    }
    if ($_POST['domanda_8'] == "c")
    { 
          $c++;
    }
    //nona domanda 
    if ($_POST['domanda_9'] == "a")
    { 
          $a++;
    }	  
    if ($_POST['domanda_9'] == "b")
    { 
          $b++;
    }
    if ($_POST['domanda_9'] == "c")
    { 
          $c++;
    }
    //decima domanda 
    if ($_POST['domanda_10'] == "a")
    { 
          $a++;
    }	  
    if ($_POST['domanda_10'] == "b")
    { 
          $b++;
    }
    if ($_POST['domanda_10'] == "c")
    { 
          $c++;
    }
    //undicesima domanda 
    if ($_POST['domanda_11'] == "a")
    { 
          $a++;
    }	  
    if ($_POST['domanda_11'] == "b")
    { 
          $b++;
    }
    if ($_POST['domanda_11'] == "c")
    { 
          $c++;
    }
    //dodicesima domanda 
    if ($_POST['domanda_12'] == "a")
    { 
          $a++;
    }	  
    if ($_POST['domanda_12'] == "b")
    { 
          $b++;
    }
    if ($_POST['domanda_12'] == "c")
    { 
          $c++;
    }
    
    $massimo = max($a,$b,$c);
    
    switch ($massimo) {
        case $a:
            echo "profilo A";
            break;
        case $b:
            echo "profilo B";
            break;
        case $c:
            echo "profilo C";
            break;
    	}
    }
    ?>
    entra nel primo if quando clicco sul bottone submit.

    Il problema è che i profili per la maggior parte mi vengono sballati.
    Ovvero anche se seleziono tutte b alle volte compare come risultato il profilo A.

    Sapreste spiegarmi dove sbaglio??

  2. #2
    il problema DOVREBBE essere che la funzione max() prende in imput un array quindi dovresti fare una cosa tipo

    Codice PHP:
    if ($a<$b){
    if (
    $b<$c){
    echo 
    "profilo c";
    }else {
    echo 
    "profilo b";
    }
    }elseif(
    $c<$a){
    echo 
    "profilo a";
    }else{
    echo 
    "profilo c";


  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    655
    http://php.net/manual/en/function.max.php

    dice che può prendere anche altri valori, non necessariamente un array

  4. #4
    mmm si è vero...hai ragione.

  5. #5
    Utente di HTML.it
    Registrato dal
    Jan 2007
    Messaggi
    655
    ho risolto!!

    Il problema è che non avevo racchiuso i bottongroup in un form con il metodo post!!

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.