반복되는것 간단하게 하고자합니다.
본문
안녕하세요.
어찌저찌 짜집기로 수정중인데 반복되는게 길어지니 수정하거나 추가할때 여간 손이가는게 아니라서 간단하게 줄이려면 아래것을 어떻게 해야할지요?
$sql = " select sum(wr_a1) as wr_a1_danga ";
$sql .= " , sum(wr_a2) as wr_a2_danga ";
$sql .= " , sum(wr_a3) as wr_a3_danga ";
$sql .= " , sum(wr_a4) as wr_a4_danga ";
$sql .= " , sum(wr_a5) as wr_a5_danga ";
$sql .= " , sum(wr_a6) as wr_a6_danga ";
$sql .= " , sum(wr_a7) as wr_a7_danga ";
$sql .= " , sum(wr_a8) as wr_a8_danga ";
$sql .= " , sum(wr_a9) as wr_a9_danga ";
$sql .= " , sum(wr_a10) as wr_a10_danga ";
$sql .= " , sum(wr_a11) as wr_a11_danga ";
$sql .= " , sum(wr_a12) as wr_a12_danga ";
$sql .= " , sum(wr_a13) as wr_a13_danga ";
$sql .= " , sum(wr_a14) as wr_a14_danga ";
$sql .= " , sum(wr_a15) as wr_a15_danga ";
$sql .= " , sum(wr_a16) as wr_a16_danga ";
$sql .= " , sum(wr_a17) as wr_a17_danga ";
$sql .= " , sum(wr_a18) as wr_a18_danga ";
$sql .= " , sum(wr_a19) as wr_a19_danga ";
$sql .= " , sum(wr_a20) as wr_a20_danga ";
답변 2
$str = [];
$sql = "SELECT ";
for($i = 1; $i <= 20; $i++) $str[] = "SUM(wr_a".$i.") AS wr_a".$i."_danga, ";
echo $sql.implode("", $str);
배열에 담아서 하셔도 괜찮아요.
뭐 대략 이런식 아닐까요
for($i=1; $i<=20; $i++){
$str .= " sum(wr_a{$i}) as wr_a{$i}_danga,";
}
echo $str;
아.. 마지막 콤마 지우는 거는 따로 안했습니다.
!-->