Использование счетчиков для определения результатов в PHP - PullRequest
0 голосов
/ 25 апреля 2018

Я использую викторину, чтобы напечатать 15 вопросов, на каждый из которых можно согласиться или не согласиться с возможными ответами.Каждый ответ направлен на определение типа ответа.Есть 5 разных измерений.

Я стремлюсь отождествлять каждое измерение с двумя противоположными вариантами.Например, чей-то «разум» является либо «интровертированным», либо «экстравертным» и так далее.Я использовал тот же стиль викторины раньше, но не могу понять, где я ошибаюсь с кодом.

<?php
if (isset($_SESSION["currentUser"])) {
    $testID = 2;    
    $dbQuery = $db->prepare("select * FROM questions WHERE testID = '".$testID."'");
    $dbQuery-> execute();

    echo '<form action="personalityType.php" method="post" id="quiz">';

    while ($dbRow = $dbQuery->fetch (PDO::FETCH_ASSOC)) {
        $questionID=$dbRow["questionID"];
        $questionText=$dbRow["questionText"];
        $dbQuery1 = $db->prepare("select * FROM answerPT WHERE questionID = '".$questionID."'");
        $dbQuery1-> execute($dbParams1);
        echo $questionID;
        echo $questionText; 
        echo "<fieldset id=$questionID>";

        while ($dbRow1 = $dbQuery1->fetch (PDO::FETCH_ASSOC)) {
            echo '<br>';
            $answerID=$dbRow1["answerID"];
            $answerText=$dbRow1["answerText"];
            echo "<input type='radio' id=$answerID value=$answerID name=$questionID><label for='answer'>$answerText</label>";

        }   echo "</fieldset>";
    }
    echo "<input type='submit' name='submit' value='Submit Answers'>";
}
echo '</form>';
?>
<?php 

$introverted =0;
$extroverted =0;
$observant =0;
$intuitive =0;
$thinking =0;
$feeling =0;
$judging =0;
$prospecting =0;
$assertive =0;
$turbulent =0;

if (isset ($_POST['submit'])){
    foreach ($_POST as $key=>$value){
        if ($key != "submit"){
            $dbQuery1 = $db->prepare("select * FROM answerPT WHERE answerID = '".$value."'");
            $dbQuery1-> execute();  

            while ($dbRow = $dbQuery1->fetch (PDO::FETCH_ASSOC)){
                $answerTypeID=$dbRow["answerTypeID"];
                $dbQuery2 = $db->prepare("select * FROM answerType WHERE answerTypeID = '".$answerTypeID."'");
                $dbQuery2-> execute();

                while ($dbRow2=$dbQuery2->fetch()){
                    $answerType=$dbRow2["answerType"];

                    if($answerType=='introverted'){
                        $introverted++;
                    }
                    if($answerType=='extroverted'){
                        $extroverted++;
                    }
                    if($answerType=='observant'){
                        $observant++;
                    }
                    if($answerType=='intuitive'){
                        $intuitive++;
                    }
                    if($answerType=='thinking'){
                        $thinking++;
                    }   
                    if($answerType=='feeling'){
                        $feeling++;
                    }
                    if($answerType=='judging'){
                        $judging++;
                    }
                    if($answerType=='prospecting'){
                        $prospecting++;
                    }
                    if($answerType=='assertive'){
                        $assertive++;
                    }
                    if($answerType=='turbulent'){
                        $turbulent++;
                    }                           


                }
            }

            $dbQuery=$db->prepare("INSERT INTO `userResponse` VALUES (:studentID, :questionID, :answerID)");
            $dbParams=array('studentID'=>$_SESSION["currentUser"], 'questionID'=>$key, 'answerID'=>$value);
            $dbQuery->execute($dbParams); 


        }   
    }
}

$introvertedStore = "empty";
$extrovertedStore = "empty";
$observantStore = "empty";
$intuitiveStore = "empty";
$thinkingStore = "empty";
$feelingStore = "empty";
$judgingStore = "empty";
$prospectingStore = "empty";
$assertiveStore = "empty";
$turbulentStore = "empty";      

if ($introverted>$extroverted){
    $total = $introverted - $extroverted;
    if ($total == 1){
        echo "Mildly introverted";
        $introvertedStore = 'Mildly introverted';
    }
    if ($total == 2){
        echo "Moderately introverted";
        $introvertedStore = 'Moderately introverted';
    }
    if ($total == 3){
        echo "Strongly introverted";
        $introvertedStore = 'Strongly introverted';
    }   
}
else {
    $total = $extroverted - $introverted;
    if ($total == 1){
        echo "Mildly extroverted";
        $extrovertedStore = 'Mildly extroverted';
    }
    if ($total == 2){
        echo "Moderately extroverted";
        $extrovertedStore = 'Moderately extroverted';
    }
    if ($total == 3){
        echo "Strongly extroverted";
        $extrovertedStore = 'Strongly extroverted';
    }   
}

if ($introvertedStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, mind) VALUE ('$currentUser', '$introvertedStore')");        
    $dbQuery2->execute(); 

}
if ($extrovertedStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, mind) VALUE ('$currentUser', '$extrovertedStore')");        
    $dbQuery2->execute(); 

}


if ($observant>$intuitive){
    $total = $observant - $intuitive;

    if ($total == 1){
        echo "Mildly observant";
        $observantStore = 'Mildly observant';
    }
    if ($total == 2){
        echo "Moderately observant";
        $observantStore = 'Moderately observant';
    }
    if ($total == 3){
        echo "Strongly observant";
        $observantStore = 'Strongly observant';
    }   
}
else {
    $total = $intuitive - $observant;
    if ($total == 1){
        echo "Mildly intuitive";
        $intuitiveStore = 'Mildly intuitive';
    }
    if ($total == 2){
        echo "Moderately intuitive";
        $intuitiveStore = 'Moderately intuitive';
    }
    if ($total == 3){
        echo "Strongly intuitive";
        $intuitiveStore = 'Strongly intuitive';
    }   
}

if ($observantStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, energy) VALUE ('$currentUser', '$observantStore')");        
    $dbQuery2->execute(); 

}
if ($intuitiveStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, energy) VALUE ('$currentUser', '$intuitiveStore')");        
    $dbQuery2->execute(); 

}


if ($thinking>$feeling){
    $total = $thinking - $feeling;
    if ($total == 1){
        echo "Mildly thinking";
        $thinkingStore = 'Mildly thinking';
    }
    if ($total == 2){
        echo "Moderately thinking";
        $thinkingStore = 'Moderately thinking';
    }
    if ($total == 3){
        echo "Strongly thinking";
        $thinkingStore = 'Strongly thinking';
    }   
}
else {
    $total = $feeling - $thinking;
    if ($total == 1){
        echo "Mildly feeling";
        $feelingStore = 'Mildly feeling';
    }
    if ($total == 2){
        echo "Moderately feeling";
        $feelingStore = 'Moderately feeling';
    }
    if ($total == 3){
        echo "Strongly feeling";
        $feelingStore = 'Strongly feeling';
    }   
}

if ($introvertedStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, nature) VALUE ('$currentUser', '$thinkingStore')");     
    $dbQuery2->execute(); 

}
if ($extrovertedStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, nature) VALUE ('$currentUser', '$feelingStore')");      
    $dbQuery2->execute(); 

}

if ($judging>$prospecting){
    $total = $judging - $prospecting;
    if ($total == 1){
        echo "Mildly judging";
        $judgingStore = 'Mildly judging';
    }
    if ($total == 2){
        echo "Moderately judging";
        $judgingStore = 'Moderately judging';
    }
    if ($total == 3){
        echo "Strongly judging";
        $judgingStore = 'Strongly judging';
    }   
}
else {
    $total = $prospecting - $judging;
    if ($total == 1){
        echo "Mildly prospecting";
        $prospectingStore = 'Mildly prospecting';
    }
    if ($total == 2){
        echo "Moderately prospecting";
        $prospectingStore = 'Moderately prospecting';
    }
    if ($total == 3){
        echo "Strongly prospecting";
        $prospectingStore = 'Strongly prospecting';
    }   
}

if ($judgingStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, tactics) VALUE ('$currentUser', '$judgingStore')");     
    $dbQuery2->execute(); 

}
if ($prospectingStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, tactics) VALUE ('$currentUser', '$prospectingStore')");     
    $dbQuery2->execute(); 

}

if ($assertive>$turbulent){
    $total = $assertive - $turbulent;
    if ($total == 1){
        echo "Mildly assertive";
        $assertiveStore = 'Mildly assertive';
    }
    if ($total == 2){
        echo "Moderately assertive";
        $assertiveStore = 'Moderately assertive';
    }
    if ($total == 3){
        echo "Strongly assertive";
        $assertiveStore = 'Strongly assertive';
    }   
}
else {
    $total = $turbulent - $assertive;
    if ($total == 1){
        echo "Mildly turbulent";
        $turbulentStore = 'Mildly turbulent';
    }
    if ($total == 2){
        echo "Moderately turbulent";
        $turbulentStore = 'Moderately turbulent';
    }
    if ($total == 3){
        echo "Strongly turbulent";
        $turbulentStore = 'Strongly turbulent';
    }   
}

if ($assertiveStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, identity) VALUE ('$currentUser', '$assertiveStore')");      
    $dbQuery2->execute(); 

}
if ($turbulentStore != "empty"){
    $dbQuery2=$db->prepare("INSERT INTO personalityType (studentNumber, identity) VALUE ('$currentUser', '$turbulentStore')");      
    $dbQuery2->execute(); 

}


?>
...