我目前有逻辑结构错误,我似乎想不出一种适当的方式写出来,所以它看起来很干净。目前,我的数据库中有一个名为USER的表。用户在众多字段中有6个字段,命名为:first、second、third、fourth、fifth、sixth。我的代码所做的就是用两个字段切换数据。因此,例如:first中的数据随着sixth中的数据而改变。
前:第一- 1,第六-6后,:第一- 6,第六-1
目前,我所拥有的是一些看起来非常混乱的东西,我不知道如何清理它。
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或其他方式向我解释,我会非常感激的。谢谢。
发布于 2014-04-01 00:57:49
你可以这样做:
// 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);
}发布于 2014-04-01 00:46:01
使用 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关键字。如果没有它,代码块不会在结束时停止,并将“通过”到下一个块。这有时很方便,但在你的情况下就不行了。
https://stackoverflow.com/questions/22774795
复制相似问题