phpjqueryajax

Getting an accurrate Results from Personality Test


What is wrong with this code, need ideas Thanks. I have Used another Method which was fine for me, but my head of my Team said it was not Okay, because we needed one of the matched Results of the user input to echo back after submit. so i come up with something like. but it still not working Okay.

 <?php
            //One of this should be my Results after input submited.
                $type = array(
                    0 => array(
                        "val" => 0,
                        "title" => "Du bist Weltenbummler"
                    ),
                    1 => array(
                        "val" => 0,
                        "title" => "Luxusliebhaber"
                    ),
                    2 => array(
                        "val" => 0,
                        "title" => "Naturbursche/-madle"
                    ),
                    3 => array(
                        "val" => 0,
                        "title" => "Sunnyboy/-girl"
                    ),
                    4 => array(
                        "val" => 0,
                        "title" => "Wasserratte"
                    ),
                    5 => array(
                        "val" => 0,
                        "title" => "Wellnessliebhaber"
                    ),
                    6 => array(
                        "val" => 0,
                        "title" => "Adrenalinjunkie"
                    ),
                    7 => array(
                        "val" => 0,
                        "title" => "Städtebummler"
                    )
                );

    //this are my post data which is being looped to store the user input value
            $type = "";
            $current = 0;
            $exp = explode(',', $_POST['expenditure']);
           foreach($exp as $key => $arrE){
                $arrEx = $arrE;
               if($arrE < 300) {
                   $type[0]["val"] += 0;
                   $type[1]["val"] += 0.1;
                   $type[2]["val"] += 0.4;
                   $type[3]["val"] += 0.3;
                   $type[4]["val"] += 0.7;
                   $type[5]["val"] += 1;
                   $type[6]["val"] += 0.7;
                   $type[7]["val"] += 1;

               }
                if($arrEx >= 300 && $arrEx <= 500) {
                   $type[0]["val"] += 0;
                   $type[1]["val"] += 1;
                   $type[2]["val"] += 0.4;
                   $type[3]["val"] += 1;
                   $type[4]["val"] += 0.7;
                   $type[5]["val"] += 1;
                   $type[6]["val"] += 0.7;
                   $type[7]["val"] += 1;

               }   
                  if($arrEx >= 600 && $arrEx <= 800) {
                   $type[0]["val"] += 1;
                   $type[1]["val"] += 1;
                   $type[2]["val"] += 0.4;
                   $type[3]["val"] += 1;
                   $type[4]["val"] += 0.7;
                   $type[5]["val"] += 1;
                   $type[6]["val"] += 0.7;
                   $type[7]["val"] += 1;

               } 
               if($arrEx >= 900 && $arrEx <= 1000) {
                   $type[0]["val"] += 2;
                   $type[1]["val"] += 1.4;
                   $type[2]["val"] += 0.4;
                   $type[3]["val"] += 1;
                   $type[4]["val"] += 0.7;
                   $type[5]["val"] += 1.3;
                   $type[6]["val"] += 1.4;
                   $type[7]["val"] += 1.5;

               }
               if($arrEx >= 1500 && $arrEx <= 3000) {
                   $type[0]["val"] += 2;
                   $type[1]["val"] += 1.4;
                   $type[2]["val"] += 0.4;
                   $type[3]["val"] += 1;
                   $type[4]["val"] += 0.7;
                   $type[5]["val"] += 1.3;
                   $type[6]["val"] += 2.2;
                   $type[7]["val"] += 2.1;

               }

           }
     $Season = explode(',', $_POST['seasonString']);
          foreach($Season as $key => $arrS){
               $arrSe = $arrS;
                if( $arrSe == "1"){
                   $type[0]["val"] += 1;
                   $type[1]["val"] += 1.4;
                   $type[2]["val"] += 0.4;
                   $type[3]["val"] += 1;
                   $type[4]["val"] += 2.0;
                   $type[5]["val"] += 2.0;
                   $type[6]["val"] += 2.0;
                   $type[7]["val"] += 2.0;} 
                if( $arrSe == "2"){
                   $type[0]["val"] += 0.5;
                   $type[1]["val"] += 1.1;
                   $type[2]["val"] += 1.4;
                   $type[3]["val"] += 1.0;
                   $type[4]["val"] += 2.0;
                   $type[5]["val"] += 2.0;
                   $type[6]["val"] += 2.0;
                   $type[7]["val"] += 2.0;}
                if( $arrSe == "3"){             
                   $type[0]["val"] += 0.5;
                   $type[1]["val"] += 1.1;
                   $type[2]["val"] += 1.4;
                   $type[3]["val"] += 1.0;
                   $type[4]["val"] += 2.0;
                   $type[5]["val"] += 2.0;
                   $type[6]["val"] += 2.0;
                   $type[7]["val"] += 2.0;}
                if( $arrSe == "4"){
                    $type[0]["val"] += 0.5;
                   $type[1]["val"] += 1.1;
                   $type[2]["val"] += 1.4;
                   $type[3]["val"] += 0.5;
                   $type[4]["val"] += 2.0;
                   $type[5]["val"] += 2.0;
                   $type[6]["val"] += 2.0;
                   $type[7]["val"] += 2.0;
                }

                }



    for($i=0;$i<count($type);$i++){
     //First loop
            if($i == 0) {
                $title = $type[0]["title"];
                $current = $type[0]["val"];
            }

            if($type[$i]["val"] > $current) {
                $title = $type[$i]["title"];
            }

    }
    echo $type;

        ?>

Solution

  • Complete guess ...

    $index = 0;
    $max = $type[0]["val"];
    
    for($i=1; $i<count($type); $i++) {
        if($type[$i]["val"] > $max) {
            $index = $i;
            $max = $type[$i]["val"];
        }
    }
    echo "Du bist " . $type[$index]["title"];
    

    or, even simpler :

    $maxItem = $type[0];
    for($i = 1; $i < count($type); $i++) {
        if($type[$i]["val"] > $maxItem["val"]) {
            $maxItem = $type[$i];
        }
    }
    echo "Du bist " . $maxItem["title"];