有没有办法简化这个代码太多,如果其他的声明

时间:2016-03-09 14:04:53

标签: php ajax if-statement switch-statement

该代码应该评估用户输入并为用户获得正确的答案。它工作正常,但只是想最小化它可能是在MVC中的implment。 我从Ajax帖子中获取变量。 如果可能的话,需要你的帮助。

<?php
    function getResults($type) {

        // variables to store Ajax post Data.
        $period = $_POST ['duration'];
        $place = $_POST ['vacationplace'];
        $serv = $_POST ['services'];
        $disc = $_POST ['discovery'];
        $Trans = $_POST ['transport'];
        $accomp = $_POST ['accompanied'];
        $stars = $_POST ['stars'];

        // Results Array
        $type = array (
                0 => array (
                        "val" => 0,
                        "title" => "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" 
                ) 
        );

        $type [] = "";
        // Expenditure ranges
        $exp = explode ( ',', $_POST ['userBudget'] );
        foreach ( $exp as $key => $arrE ) {
            $arrEx = $arrE;
            if ($arrE < 300) {
                $type [0] ["val"] += 0;
                $type [1] ["val"] += .1;
                $type [2] ["val"] += .5;
                $type [3] ["val"] += .3;
                $type [4] ["val"] += .7;
                $type [5] ["val"] += 1;
                $type [6] ["val"] += .1;
                $type [7] ["val"] += .1;
            }
            if ($arrEx >= 300 && $arrEx <= 500) {
                $type [0] ["val"] += 0;
                $type [1] ["val"] += 1;
                $type [2] ["val"] += 1;
                $type [3] ["val"] += 1;
                $type [4] ["val"] += 1;
                $type [5] ["val"] += 1;
                $type [6] ["val"] += 0;
                $type [7] ["val"] += 1;
            }
            if ($arrEx >= 600 && $arrEx <= 800) {
                $type [0] ["val"] += 1;
                $type [1] ["val"] += 1;
                $type [2] ["val"] += 1;
                $type [3] ["val"] += 1;
                $type [4] ["val"] += 1;
                $type [5] ["val"] += 1;
                $type [6] ["val"] += 1;
                $type [7] ["val"] += 1;
            }
            if ($arrEx >= 900 && $arrEx <= 1000) {
                $type [0] ["val"] += 1;
                $type [1] ["val"] += 1;
                $type [2] ["val"] += 1;
                $type [3] ["val"] += 1;
                $type [4] ["val"] += 1;
                $type [5] ["val"] += 1;
                $type [6] ["val"] += 1;
                $type [7] ["val"] += 1;
            }
            if ($arrEx >= 1500 && $arrEx <= 3000) {
                $type [0] ["val"] += 1;
                $type [1] ["val"] += 1;
                $type [2] ["val"] += 1;
                $type [3] ["val"] += 1;
                $type [4] ["val"] += 1;
                $type [5] ["val"] += 1;
                $type [6] ["val"] += 1;
                $type [7] ["val"] += 1;
            }
        }

        // seasons
        $Season = explode ( ',', $_POST ['seasonString'] );
        foreach ( $Season as $key => $arrS ) {
            $arrSe = $arrS;
            if ($arrSe == "1") {
                $type [0] ["val"] += .5;
                $type [1] ["val"] += .5;
                $type [2] ["val"] += .5;
                $type [3] ["val"] += .5;
                $type [4] ["val"] += .5;
                $type [5] ["val"] += .5;
                $type [6] ["val"] += .5;
                $type [7] ["val"] += .5;
            }
            if ($arrSe == "2") {
                $type [0] ["val"] += .5;
                $type [1] ["val"] += .5;
                $type [2] ["val"] += .5;
                $type [3] ["val"] += .5;
                $type [4] ["val"] += .5;
                $type [5] ["val"] += .5;
                $type [6] ["val"] += .5;
                $type [7] ["val"] += .5;
            }
            if ($arrSe == "3") {
                $type [0] ["val"] += .5;
                $type [1] ["val"] += .5;
                $type [2] ["val"] += .5;
                $type [3] ["val"] += .5;
                $type [4] ["val"] += .5;
                $type [5] ["val"] += .5;
                $type [6] ["val"] += .5;
                $type [7] ["val"] += .5;
            }
            if ($arrSe == "4") {
                $type [0] ["val"] += .5;
                $type [1] ["val"] += .5;
                $type [2] ["val"] += .5;
                $type [3] ["val"] += .3;
                $type [4] ["val"] += .3;
                $type [5] ["val"] += .5;
                $type [6] ["val"] += .3;
                $type [7] ["val"] += .5;
            }
        }

        // user desires
        $desires = explode ( ',', $_POST ['desiresString'] );
        foreach ( $desires as $key => $arrD ) {
            $arrDe = $arrD;
            if ($arrDe == "1") {
                $type [0] ["val"] += 0;
                $type [1] ["val"] += 0;
                $type [2] ["val"] += .5;
                $type [3] ["val"] += 0;
                $type [4] ["val"] += 0;
                $type [5] ["val"] += 0;
                $type [6] ["val"] += 0;
                $type [7] ["val"] += .2;
            }
            if ($arrDe == "2") {
                $type [0] ["val"] += .5;
                $type [1] ["val"] += .5;
                $type [2] ["val"] += .5;
                $type [3] ["val"] += 1;
                $type [4] ["val"] += 1;
                $type [5] ["val"] += .5;
                $type [6] ["val"] += .7;
                $type [7] ["val"] += 1;
            }
            if ($arrDe == "3") {
                $type [0] ["val"] += .5;
                $type [1] ["val"] += .5;
                $type [2] ["val"] += 1;
                $type [3] ["val"] += .7;
                $type [4] ["val"] += 1;
                $type [5] ["val"] += .5;
                $type [6] ["val"] += .7;
                $type [7] ["val"] += .5;
            }
            if ($arrDe == "4") {
                $type [0] ["val"] += 1;
                $type [1] ["val"] += .5;
                $type [2] ["val"] += .5;
                $type [3] ["val"] += 1;
                $type [4] ["val"] += .7;
                $type [5] ["val"] += .7;
                $type [6] ["val"] += 1;
                $type [7] ["val"] += 1;
            }
            if ($arrDe == "5") {
                $type [0] ["val"] += .5;
                $type [1] ["val"] += .3;
                $type [2] ["val"] += .5;
                $type [3] ["val"] += .7;
                $type [4] ["val"] += .8;
                $type [5] ["val"] += .3;
                $type [6] ["val"] += .5;
                $type [7] ["val"] += .8;
            }
            if ($arrDe == "6") {
                $type [0] ["val"] += 0;
                $type [1] ["val"] += .7;
                $type [2] ["val"] += .5;
                $type [3] ["val"] += .7;
                $type [4] ["val"] += 1;
                $type [5] ["val"] += 1;
                $type [6] ["val"] += .4;
                $type [7] ["val"] += .3;
            }
            if ($arrDe == "7") {
                $type [0] ["val"] += .8;
                $type [1] ["val"] += .3;
                $type [2] ["val"] += .3;
                $type [3] ["val"] += .3;
                $type [4] ["val"] += .3;
                $type [5] ["val"] += .3;
                $type [6] ["val"] += .8;
                $type [7] ["val"] += 1;
            }
            if ($arrDe == "8") {
                $type [0] ["val"] += .8;
                $type [1] ["val"] += .3;
                $type [2] ["val"] += .9;
                $type [3] ["val"] += .7;
                $type [4] ["val"] += 1;
                $type [5] ["val"] += 1;
                $type [6] ["val"] += .7;
                $type [7] ["val"] += .3;
            }
        }

        // vacations period

        if ($period == "1") {
            $type [0] ["val"] += 1;
            $type [1] ["val"] += .5;
            $type [2] ["val"] += .8;
            $type [3] ["val"] += 1;
            $type [4] ["val"] += .8;
            $type [5] ["val"] += .5;
            $type [6] ["val"] += .7;
            $type [7] ["val"] += .6;
        }
        if ($period == "2") {
            $type [0] ["val"] += 1;
            $type [1] ["val"] += .9;
            $type [2] ["val"] += .8;
            $type [3] ["val"] += .8;
            $type [4] ["val"] += .9;
            $type [5] ["val"] += 1;
            $type [6] ["val"] += .9;
            $type [7] ["val"] += 1;
        }
        if ($period == "3") {
            $type [0] ["val"] += 1;
            $type [1] ["val"] += 1;
            $type [2] ["val"] += 1;
            $type [3] ["val"] += 1;
            $type [4] ["val"] += 1;
            $type [5] ["val"] += 1;
            $type [6] ["val"] += 1;
            $type [7] ["val"] += 1;
        }
        if ($period == "4") {
            $type [0] ["val"] += 1;
            $type [1] ["val"] += 1;
            $type [2] ["val"] += 1;
            $type [3] ["val"] += 1;
            $type [4] ["val"] += 1;
            $type [5] ["val"] += 1;
            $type [6] ["val"] += 1;
            $type [7] ["val"] += 1;
        }

        // Vacation places
        if ($place == "1") {
            $type [0] ["val"] += 0;
            $type [1] ["val"] += .9;
            $type [2] ["val"] += .7;
            $type [3] ["val"] += .7;
            $type [4] ["val"] += .7;
            $type [5] ["val"] += .9;
            $type [6] ["val"] += .5;
            $type [7] ["val"] += 1;
        }
        if ($place == "2") {
            $type [0] ["val"] += 1;
            $type [1] ["val"] += .9;
            $type [2] ["val"] += .7;
            $type [3] ["val"] += .9;
            $type [4] ["val"] += .9;
            $type [5] ["val"] += .9;
            $type [6] ["val"] += .9;
            $type [7] ["val"] += .9;
        }
        if ($place == "3") {
            $type [0] ["val"] += 1;
            $type [1] ["val"] += 1;
            $type [2] ["val"] += .9;
            $type [3] ["val"] += 1;
            $type [4] ["val"] += .9;
            $type [5] ["val"] += .8;
            $type [6] ["val"] += 1;
            $type [7] ["val"] += .9;
        }

        // Hotel Services

        if ($serv == "1") {
            $type [0] ["val"] += .08;
            $type [1] ["val"] += .08;
            $type [2] ["val"] += .08;
            $type [3] ["val"] += .08;
            $type [4] ["val"] += .08;
            $type [5] ["val"] += .08;
            $type [6] ["val"] += .08;
            $type [7] ["val"] += .08;
        }
        if ($serv == "2") {
            $type [0] ["val"] += .09;
            $type [1] ["val"] += .09;
            $type [2] ["val"] += .09;
            $type [3] ["val"] += .09;
            $type [4] ["val"] += .09;
            $type [5] ["val"] += .09;
            $type [6] ["val"] += .09;
            $type [7] ["val"] += .09;
        }
        if ($serv == "3") {
            $type [0] ["val"] += 1;
            $type [1] ["val"] += 1;
            $type [2] ["val"] += 1;
            $type [3] ["val"] += 1;
            $type [4] ["val"] += 1;
            $type [5] ["val"] += 1;
            $type [6] ["val"] += 1;
            $type [7] ["val"] += 1;
        }

        // Place Discoveries

        if ($disc == "1") {
            $type [0] ["val"] += .09;
            $type [1] ["val"] += .07;
            $type [2] ["val"] += .08;
            $type [3] ["val"] += .08;
            $type [4] ["val"] += .08;
            $type [5] ["val"] += .08;
            $type [6] ["val"] += .05;
            $type [7] ["val"] += .05;
        }
        if ($disc == "2") {
            $type [0] ["val"] += 1;
            $type [1] ["val"] += .09;
            $type [2] ["val"] += .08;
            $type [3] ["val"] += .06;
            $type [4] ["val"] += .05;
            $type [5] ["val"] += 1.05;
            $type [6] ["val"] += 1;
            $type [7] ["val"] += 1;
        }

        // Transportation
        if ($Trans == "1") {
            $type [0] ["val"] += .09;
            $type [1] ["val"] += .06;
            $type [2] ["val"] += .05;
            $type [3] ["val"] += .05;
            $type [4] ["val"] += .05;
            $type [5] ["val"] += 1;
            $type [6] ["val"] += .09;
            $type [7] ["val"] += 1;
        }
        if ($Trans == "2") {
            $type [0] ["val"] += 1;
            $type [1] ["val"] += .09;
            $type [2] ["val"] += .06;
            $type [3] ["val"] += 1;
            $type [4] ["val"] += .09;
            $type [5] ["val"] += .04;
            $type [6] ["val"] += 1;
            $type [7] ["val"] += .09;
        }
        if ($Trans == "3") {
            $type [0] ["val"] += .09;
            $type [1] ["val"] += .09;
            $type [2] ["val"] += .08;
            $type [3] ["val"] += .05;
            $type [4] ["val"] += .07;
            $type [5] ["val"] += .09;
            $type [6] ["val"] += .06;
            $type [7] ["val"] += 1;
        }

        // Hotel Ratting

        if ($stars == "1") {
            $type [0] ["val"] += .05;
            $type [1] ["val"] += .05;
            $type [2] ["val"] += .05;
            $type [3] ["val"] += .05;
            $type [4] ["val"] += .05;
            $type [5] ["val"] += .05;
            $type [6] ["val"] += .05;
            $type [7] ["val"] += .05;
        }
        if ($stars == "2") {
            $type [0] ["val"] += .06;
            $type [1] ["val"] += .06;
            $type [2] ["val"] += .06;
            $type [3] ["val"] += .06;
            $type [4] ["val"] += .06;
            $type [5] ["val"] += .06;
            $type [6] ["val"] += .06;
            $type [7] ["val"] += .06;
        }
        if ($stars == "3") {
            $type [0] ["val"] += .07;
            $type [1] ["val"] += .07;
            $type [2] ["val"] += .07;
            $type [3] ["val"] += .07;
            $type [4] ["val"] += .07;
            $type [5] ["val"] += .07;
            $type [6] ["val"] += .07;
            $type [7] ["val"] += .07;
        }
        if ($stars == "4") {
            $type [0] ["val"] += .09;
            $type [1] ["val"] += .09;
            $type [2] ["val"] += .09;
            $type [3] ["val"] += .09;
            $type [4] ["val"] += .09;
            $type [5] ["val"] += .09;
            $type [6] ["val"] += .09;
            $type [7] ["val"] += .09;
        }
        if ($stars == "5") {
            $type [0] ["val"] += 1;
            $type [1] ["val"] += 1;
            $type [2] ["val"] += 1;
            $type [3] ["val"] += 1;
            $type [4] ["val"] += 1;
            $type [5] ["val"] += 1;
            $type [6] ["val"] += 1;
            $type [7] ["val"] += 1;
        }

        // Traveling with Partner, Family or Alone
        if ($accomp == "1") {
            $type [0] ["val"] += .19;
            $type [1] ["val"] += .19;
            $type [2] ["val"] += .18;
            $type [3] ["val"] += .20;
            $type [4] ["val"] += .20;
            $type [5] ["val"] += .19;
            $type [6] ["val"] += 1;
            $type [7] ["val"] += 1;
        }
        if ($accomp == "2") {
            $type [0] ["val"] += .20;
            $type [1] ["val"] += .20;
            $type [2] ["val"] += .16;
            $type [3] ["val"] += .20;
            $type [4] ["val"] += .20;
            $type [5] ["val"] += .20;
            $type [6] ["val"] += .17;
            $type [7] ["val"] += .15;
        }
        if ($accomp == "3") {
            $type [0] ["val"] += .16;
            $type [1] ["val"] += .15;
            $type [2] ["val"] += .19;
            $type [3] ["val"] += .20;
            $type [4] ["val"] += .19;
            $type [5] ["val"] += .16;
            $type [6] ["val"] += .16;
            $type [7] ["val"] += .19;
        }

        // loop and count value Entered for Results outcome
        $maxVal = $type [0];
        for($i = 1; $i < count ( $type ); $i ++) {
            if ($type [$i] ["val"] > $maxVal ["val"]) {
                $maxVal = $type [$i];
            }
        }

        echo "<h3> Du bist " . $maxVal ["title"] . " </h3>";

    }
    ?>

2 个答案:

答案 0 :(得分:1)

嘿,你确定你的代码需要这些行

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

您可以将范围存储到数组示例Lower = {600,900,1500}和High = {800,1000,3000}以及一个包含增量/增量的2D数组,您现在需要增加这些增量/增量迭代循环并检查数组arrEx的哪个索引谎言并更新你的类型数组的值我希望你理解(抱歉我的英语不好)。

EDITED --------------

我说的是假设你有2个一维数组$ Lower = {L1,L2,L3 .. Ln}和$ High = {H1,H2,H3 ...... Hn}和一个二维数组$ increment = {{0_val_increment_value,1_val_increment_value ,,,,, 7。},...... n次}现在代码将是

foreach ( $exp as $key => $arrE ) {
        $arrEx = $arrE;
        Loop: iterate n times using i {
                  low = $Lower[i] , high = $High[i]
                  IF arrEx lies in range THEN
                         Loop: iterate 8 times using j {
                                         $type[j]["val"] += increment[i][j] 
                        }
                   END IF
           }
   }

上面是伪代码,我认为很容易在PHP代码中转换:)

答案 1 :(得分:0)

使用switch case代替那么多else if语句

http://www.w3schools.com/php/php_switch.asp