我有以下查詢生成器。有些案例有相同的代碼(那些有參數的)。查詢生成器改進
我知道它不好重複代碼,如果有的話。
你會如何建議我改善這個循環? 謝謝!
for ($i = 0; $i < $len; ++$i) {
switch ($conditions[$i]['condition']) {
case 'version':
$value_counter++;
$sql .= ' AND `musers`.`app_version` = :value'.$value_counter;
$values[$value_counter] = $conditions[$i]['value'];
break;
case 'except_version':
$value_counter++;
$sql .= ' AND `musers`.`app_version` != :value'.$value_counter;
$values[$value_counter] = $conditions[$i]['value'];
break;
case 'from_credits':
$value_counter++;
$sql .= ' AND `musers`.`credits` >= :value'.$value_counter;
$values[$value_counter] = $conditions[$i]['value'];
break;
case 'to_credits':
$value_counter++;
$sql .= ' AND `musers`.`credits` <= :value'.$value_counter;
$values[$value_counter] = $conditions[$i]['value'];
break;
case 'no_credits':
$sql .= ' AND `musers`.`credits` = 0';
break;
case 'register_atleast':
$value_counter++;
$sql .= ' AND DATEDIFF(NOW(), `musers`.`creation_date`) > :value'.$value_counter;
$values[$value_counter] = $conditions[$i]['value'];
break;
case 'talked_atleast':
$value_counter++;
$sql .= " AND `musers`.`total_talktime` >= :value".$value_counter;
$values[$value_counter] = $conditions[$i]['value'];
break;
case 'never_talked':
$sql .= " AND `musers`.`total_talktime` = 0";
break;
case 'purchase_atleast':
$value_counter++;
$sql .= " AND `musers`.`purchase_times` >= :value".$value_counter;
$values[$value_counter] = $conditions[$i]['value'];
break;
case 'never_purchase':
$sql .= ' AND `musers`.`purchase_times` = 0';
break;
case 'from_birthdate':
$value_counter++;
$sql .= " AND (`musers`.`birthdate` = '0000-00-00' OR `musers`.`birthdate` >= :value".$value_counter.")";
$values[$value_counter] = date('Y-m-d', strtotime($conditions[$i]['value']));
break;
case 'to_birthdate':
$value_counter++;
$sql .= " AND (`musers`.`birthdate` = '0000-00-00' OR `musers`.`birthdate` <= :value".$value_counter.")";
$values[$value_counter] = date('Y-m-d', strtotime($conditions[$i]['value']));
break;
case 'from_creation_date':
$value_counter++;
$sql .= " AND (`musers`.`creation_date` = '0000-00-00' OR `musers`.`creation_date` >= :value".$value_counter.")";
$values[$value_counter] = date('Y-m-d', strtotime($conditions[$i]['value']));
break;
case 'to_creation_date':
$value_counter++;
$sql .= " AND (`musers`.`creation_date` = '0000-00-00' OR `musers`.`creation_date` <= :value".$value_counter.")";
$values[$value_counter] = date('Y-m-d', strtotime($conditions[$i]['value']));
break;
case 'user_id':
$value_counter++;
$sql .= ' AND `musers`.`id` = :value'.$value_counter;
$values[$value_counter] = $conditions[$i]['value'];
break;
case 'visit_app_aleast':
$value_counter++;
$sql .= ' AND `musers`.`visit_app_times` >= :value'.$value_counter;
$values[$value_counter] = $conditions[$i]['value'];
break;
case 'visit_app_less':
$value_counter++;
$sql .= ' AND `musers`.`visit_app_times` < :value'.$value_counter;
$values[$value_counter] = $conditions[$i]['value'];
break;
case 'total_calls_atleast':
$value_counter++;
$sql .= ' AND `musers`.`total_calls` >= :value'.$value_counter;
$values[$value_counter] = $conditions[$i]['value'];
break;
case 'total_calls_less':
$value_counter++;
$sql .= ' AND `musers`.`total_calls` < :value'.$value_counter;
$values[$value_counter] = $conditions[$i]['value'];
break;
case 'hourdiff':
$value_counter++;
$sql .= ' AND `hourdiff` = :value'.$value_counter;
$values[$value_counter] = $conditions[$i]['value'];
break;
}
}
http://codereview.stackexchange.com/ – Mihai
這個問題屬於上http://codereview.stackexchange.com/ – SirDarius
- 張貼在那裏,謝謝。 – DanR