你能帮我缩短代码吗?重点是,我希望以不同的顺序从数据库中获取数据,这取决于给定的$T
。任何的想法?
public function rewriteQuery($T){
switch ($T) {
case 0:
$query="SELECT id, title FROM kshome WHERE del=0 AND active=1 ORDER BY FIELD(id,1,2,3,4,5,6)";
break;
case 1:
$query="SELECT id, title FROM kshome WHERE del=0 AND active=1 ORDER BY FIELD(id,2,3,4,5,6,1)";
break;
case 2:
$query="SELECT id, title FROM kshome WHERE del=0 AND active=1 ORDER BY FIELD(id,3,4,5,6,1,2)";
break;
case 3:
$query="SELECT id, title FROM kshome WHERE del=0 AND active=1 ORDER BY FIELD(id,4,5,6,1,2,3)";
break;
case 4:
$query="SELECT id, title FROM kshome WHERE del=0 AND active=1 ORDER BY FIELD(id,5,6,1,2,3,4)";
break;
default:
$query="SELECT id, title FROM kshome WHERE del=0 AND active=1 ORDER BY FIELD(id,6,1,2,3,4,5)";
break;
}
return $query;
}
答案 0 :(得分:1)
缩短这一点的一种方法是创建一个返回order by
字段的数组。
这样的事情:
$order_by_array = array(
0 => "(id,1,2,3,4,5,6)",
1 => "(id,2,3,4,5,6,1)",
2 => "(id,3,4,5,6,1,2)",
3 => "(id,4,5,6,1,2,3)",
4 => "(id,5,6,1,2,3,4)"
);
然后您的rewriteQuery($T)
函数可以重写为:
public function rewriteQuery($T){
$order_by = "(id,6,1,2,3,4,5)"; // Default
if (array_key_exists($T, $order_by_array)) {
$order_by = $order_by_array[$T];
}
$query = "SELECT id, title FROM kshome WHERE del=0 AND active=1 ORDER BY FIELD{$order_by}";
...
}
答案 1 :(得分:0)
Otherthan在构造查询字符串时删除了冗余,我看不到可以缩短的更多内容:
public function rewriteQuery($T){
$query = "SELECT id, title FROM kshome WHERE del=0 AND active=1 ORDER BY FIELD";
switch ($T) {
case 0:
$query.="(id,1,2,3,4,5,6)";
break;
case 1:
$query.="(id,2,3,4,5,6,1)";
break;
case 2:
$query.="(id,3,4,5,6,1,2)";
break;
case 3:
$query.="(id,4,5,6,1,2,3)";
break;
case 4:
$query.="(id,5,6,1,2,3,4)";
break;
default:
$query.="(id,6,1,2,3,4,5)";
break;
}
return $query;
答案 2 :(得分:0)
虽然我更喜欢其他答案,以防您的订购得到修复,但这是一个更灵活的解决方案:
function rewriteQuery($T) {
$query = "SELECT id, title FROM kshome WHERE del=0 AND active=1 ORDER BY FIELD(id,";
$order = array("1", "2", "3", "4", "5", "6");
if($T < count($order)) {
for($i = $T, $end = false;;$i++) {
if($i == count($order)) {
$i = 0;
$end = true;
}
if($end && $i == $T) {
break;
}
$query .= $order[$i] . ",";
}
$query = substr($query, 0, -1) . ")";
return $query;
} else {
return "ERROR";
}
}
$index = 5;
echo rewriteQuery($index);
请参阅PHPfiddle。
基本上你必须意识到你的$T
只不过是你想要开始订单的索引(给定你的订单作为数组)。一旦你意识到这一点,你可以实现逻辑:
以$T
作为开始,我们遍历数组直到结束,然后将$i
设置为零并激活结束条件以不运行无限循环。 $i
将继续增加,直至达到$T
,然后退出循环。剩下的就是摆脱尾随,
。我只需将其替换为)
即可完成查询。
答案 3 :(得分:0)
好吧,如果你想变得特别可怕,你可以计算整个事情:
public function rewriteQuery($T){
$num_cols = 6;
// Build a naughty little array to pick numbers from.
$numbers = array_merge(range(1, $num_cols, 1), range(1, $num_cols, 1));
// Pick the section from the array corresponding to our $T
$rotated = array_slice($numbers, $T, $num_cols);
// Shove "id" on the front
array_unshift($rotated, 'id');
// Build a string
$final_list = implode(", ", $rotated);
// Build the SQL
$query = "SELECT id, title FROM kshome WHERE del = 0 AND active = 1 ORDER BY FIELD($final_list)";
return $query;
}
但坦率地说,老实说,我认为你所做的事情并没有太大的错误。如其他答案所示,从主查询中分离出排序字符串,可以防止在主查询发生变化时更改大量行,这很好。
我们的想法是在复制粘贴代码之间找到一个快乐的平衡,这个代码难以维护,并且代码非常聪明,当你在三周的时间内回到它时,你将无法理解它。我总是站在前者身边。
我的意思是:不要认真对待这个答案 - 这只是一个例子,说明你在可读性和可维护性方面可能出现的问题,如果你走得太远“我绝不能走模糊地重复自己。“
答案 4 :(得分:0)
好的,我知道你希望代码很简短,但是如果需要调用大量的开关,你可以尝试循环而不是手动数组,因为你的订单有一个很好的模式。
public function rewriteQuery($T)
{
$query = "SELECT id, title FROM kshome WHERE del=0 AND active=1 ORDER BY FIELD";
$fieldOrder =" (id, ";
$j = $T;
$i=1;
if($j > 0 && $j < 5) // or whatever you wish
{
while(1)
{ // replace 6 (below) by whatever your limit is
$fieldOrder .= ($j % 6) + 1;
if($i < 6)
$fieldOrder .= ", ";
else
break;
$j++; $i++;
}
}
else
{ $fieldOrder .= "6, 1, 2, 3, 4, 5";
// or maybe a loop again! :-D
}
$fieldOrder .= " )";
return($query.$fieldOrder);
}