逻辑清理

时间:2014-03-31 23:54:19

标签: php

我目前正遇到逻辑结构错误,我似乎无法想出一种正确的写法,因此看起来很干净。目前,我的数据库中有一个名为USER的表。用户有6个字段,分别为:firstsecondthirdfourthfifthsixth。 我的代码所做的是用两个字段切换数据。例如:first中的数据随sixth中的数据而变化。

之前:首先 - 1,第6 - 6 之后:第一个 - 第六个,第六个 - 第一个

目前,我所拥有的东西看起来非常混乱,而且我不确定如何清理它。

if($switch == true){ /* $switch just indicates if the numbers being switched is one of the fields i.e. first, second, etc.. */
                if($slot == 1){ // The number is being switched into the first slot
                    if($first == $number){
                        return 1; // error, can't switch with itself
                    } else if ($second == $number){
                        $temp = $first;
                        // 1st QUERY TO UPDATE THE DATA IN FIRST, WITH THE DATA IN THE SECOND
                        // 2nd QUERY TO UPDATE THE DATA IN SECOND, WITH THE INFO STORED IN VARIABLE $temp
                    } else if ($third == $number){
                        $temp = $first;
                        // 1st QUERY TO UPDATE THE DATA IN FIRST, WITH THE DATA IN THE THIRD
                        // 2nd QUERY TO UPDATE THE DATA IN THIRD, WITH THE INFO STORED IN VARIABLE $temp 
                    } // ... continues to check for 4, 5, 6...
                } else if($slot == 2){ /* Then checks to see if it was slot 2, i.e. Second */
                    // ....
                } else if($slot == 3){ /* Then checks to see if it was slot 3, i.e. Third */
                    // ....
                } else if($slot == 4){ /* Then checks to see if it was slot 4, i.e. Fourth */
                    // ....
                } else if($slot == 5){ /* Then checks to see if it was slot 5, i.e. Fifth */
                    // ....
                } else if($slot == 6){ /* Then checks to see if it was slot 6, i.e. Sixth */
                    // ....
                }

}

每个插槽都会继续,每个插槽6次......我知道编程错误,但我刚开始学习PHP(它是我的第一语言,所以请耐心等待)。有什么建议可以让我更干净吗?甚至是更好的写作方式?如果有人关心通过Skype或任何事情向我解释,我将非常感激。谢谢。

2 个答案:

答案 0 :(得分:2)

使用switch statement

if($switch == true){ 
    switch($slot) {
        case 1 :
            if($first == $number){
                return 1; // error, can't switch with itself
            } else if ($second == $number){
                $temp = $first;
                // 1st QUERY TO UPDATE THE DATA IN FIRST, WITH THE DATA IN THE SECOND
                // 2nd QUERY TO UPDATE THE DATA IN SECOND, WITH THE INFO STORED IN VARIABLE $temp
            } else if ($third == $number){
                $temp = $first;
                // 1st QUERY TO UPDATE THE DATA IN FIRST, WITH THE DATA IN THE THIRD
                // 2nd QUERY TO UPDATE THE DATA IN THIRD, WITH THE INFO STORED IN VARIABLE $temp 
            } 
            break;
        case 2 :
            // ....
            break;
        case 3 :
            // ....
            break;
        case 4 :
            // ....
            break;
        case 5 :
            // ....
            break;
        case 6 :
            // ....
            break;
        default:
            // ...
    }    
}    

您会在底部看到default。如果你的条件没有匹配,那么一个人就可以了。

另请注意break关键字。如果没有它,代码块不会在结束时停止,并且会在#34;到下一个街区。这有时候很方便,但不是你的情况。

答案 1 :(得分:1)

你可以这样做:

// This function returns the column name for the specified slot
function getSlotColumn($slotNum)
{
    switch($slotNum)
    {
        case 1:
            retrun "first";

        case 2:
            return "second";

        ......
    }
}

然后在切换功能

// This function swaps the values of the two specified slots
function swapValues($slot1Num, $slot2Num)
{
    if($slot1Num == $slot2Num) return;
    if($slot1Num < 1 || $slot1Num > 6) return;
    if($slot2Num < 1 || $slot2Num > 6) return;

    $slot1Col = getSlotColumn($slot1Num);
    $slot2Col = getSlotColumn($slot2Num);

            //get value of the first slot
    $slot1Val = //SELECT $slot1Col FROM ... WHERE ... slot

    // Now since we have the value of the first slot, we can update it from the second slot with this pseudo query
    //UPDATE ... SET $slot1Col = $slot2Col WHERE ...

    // Then update second slot from the value in $slot1Val
    //UPDATE ... SET $slot2Col = $slot1Val WHERE ...
}

然后像这样使用它:

if(switch == true)
{
    swapValues(1, 6);
}