2015-11-21 17 views
0

這段代碼有什麼問題,需要思路謝謝。 我已經使用了另一種方法,對我來說很好,但我的團隊的負責人表示它並不好,因爲我們需要用戶輸入的匹配結果之一在提交後回顯。所以我想出了類似的東西。但它仍然不起作用。從個性測試中獲得一個令人鼓舞的結果

<?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; 

     ?> 
+3

問題需要一個更好的介紹。代碼應該做什麼?什麼不工作? –

+0

是一種性格測試,它給我一個匹配的用戶輸入結果庫。正如我所說的,multid index array的第一個代碼就是我堆疊的地方。但第二個工作正常,我認爲但不接受球隊。 – Wandaga1

回答

0

完全猜測...

$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"]; 

,或者更簡單:

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

謝謝,但它接縫你的代碼只會給我從用戶的最大價值,但不是「標題」,這是我的問題。我得到的最大值,但要獲得附加結果,並在頁面上回顯是我的問題。 – Wandaga1

+0

所以請在問題中說明你想要回應的內容。 –

+1

對不起,你的代碼是絕對的,並且工作也很完美。只是今天證明,並非常感謝。 – Wandaga1