채택완료

창피하지만 if문 올려봅니다. 함 봐주심 감사하겠습니다.

Paid : Balace 즉 내야할 돈 (wr_17) 값이 0.00 이면 표기

Overdue : 지정 날짜(wr_14)가 오늘 날짜보다 지나버리면 Overdue라고 표기

Unpaind : 상품가격(wr_15)와 내야할 돈(wr_17)이 같으면 표기

Partial : 내야할돈(wr_17)이 0이 아니고 조금이라도 낸 금액, 즉 0보다 크면 표기

Draft : wr_18(활성, 환불, 취소)에 넣어준 값 중에서 환불, 혹은 취소를 했을경우 표기

Copy
<?
$endDate = date($list[$i]['wr_14']); //Due Date
$today = date("m-d-y"); //오늘 날짜
?>
                

<?php if($list[$i]['wr_17'] == '0' && $list[$i]['wr_18'] == 'Active' ) { ?> 
<span style="padding:5px 15px; background-color: #45d864; border-radius: 5px; text-align: center; color: #FFFFFF; font-weight: 700; display:inline-block; width:90px;">Paid</span>
<?php } elseif($list[$i]['wr_17'] == $list[$i]['wr_15'] && $list[$i]['wr_18'] == 'Active' ) { ?> 
<span style="padding:5px 15px; background-color: #e85fc6; border-radius: 5px; text-align: center; color: #FFFFFF; font-weight: 700; display:inline-block; width:90px;">Unpaid<span>
 <?php } elseif($list[$i]['wr_17'] > '0' && $endDate < $today && $list[$i]['wr_18'] == 'Active' ) { ?> 
  <span style="padding:5px 15px; background-color: #ff381e; border-radius: 5px; text-align: center; color: #FFFFFF; font-weight: 700; display:inline-block; width:90px;">Overdue<span>
 <?php } elseif($list[$i]['wr_17'] > '0' && $endDate > $today && $list[$i]['wr_18'] == 'Active' ) { ?> 
 <span style="padding:5px 15px; background-color: #ebb048; border-radius: 5px; text-align: center; color: #FFFFFF; font-weight: 700; display:inline-block; width:90px;">Partial</span>
  <?php } elseif($list[$i]['wr_18'] == 'Cancel' || 'Refund' && $list[$i]['wr_17'] >= '0') { ?> 
  <span style="padding:5px 15px; background-color: #a0a0a0; border-radius: 5px; text-align: center; color: #FFFFFF; font-weight: 700; display:inline-block; width:90px;">Draft</span>
  <?php } else { ?> 
   N/A
   <?php } ?>

 

이렇게 사용하고 있는데 이렇게 해서 써도 되는지 잘 모르겠습니다. 

내야할 돈(wr_17)이 일부 남아있으면서 오늘 날짜가 지나면 Overdue로 띄울려로 하는데 unpaid로 나오네요.. 뭔가가 공식이 틀린것 같은데.. 식을 잘못 썻더니 다 잘못 바뀌기도 하고 그러더라구요.. 그래서 딱 이렇게만 해놨는데 이리 써도 문제가 없을런지요? ㅎㅎ 어느날 갑자기 전혀 생각치도 못했던 곳에서 오류가 날까봐서요.. 한번 검토해주시면 감사하겠습니다. 

답변 3개 / 댓글 1개

채택된 답변
+20 포인트

여러 조건을 한 조건문에 같이 섞어 쓰면서 if, else if 를 사용하면 가독성도 떨어지고 

개발자가 생각한 상황 이외의 경우의 수도 많이 발생합니다. 

조금 번거롭더라도 

if($wr_17==0){

    // 0일경우의 다른 조건 if(....)

}

else if($wr_17>0){

}

 

이런식으로 구분 지어서 그안에 조건문을 다시 활용하는게 훨씬 좋을것 같아 보입니다. 

그래야 오류가 나도 어느 부분에서 났는지 파악하기 쉬울것 같네요. 

 

윗분들의 의견을 참고해서 간단하게 다시 짜봤습니다.

 

Copy
<?php

$list = array(

    array("wr_14" => "07-06-22", "wr_15" => 1000.00, "wr_17" => 0.00, "wr_18" => "Active")

    , array("wr_14" => "07-07-22", "wr_15" => 1000.00, "wr_17" => 0.00, "wr_18" => "Active")

    , array("wr_14" => "07-06-22", "wr_15" => 1000.00, "wr_17" => 1000.00, "wr_18" => "Active")

    , array("wr_14" => "07-05-22", "wr_15" => 1000.00, "wr_17" => 100.00, "wr_18" => "Active")

    , array("wr_14" => "07-05-22", "wr_15" => 1000.00, "wr_17" => 100.00, "wr_18" => "Refund")

    , array("wr_14" => "07-05-22", "wr_15" => 1000.00, "wr_17" => 100.00, "wr_18" => "Cancel")

    , array("wr_14" => "07-05-22", "wr_15" => 1000.00, "wr_17" => 0.00, "wr_18" => "Refund")

    , array("wr_14" => "07-05-22", "wr_15" => 1000.00, "wr_17" => 0.00, "wr_18" => "Cancel")

);

$count = count($list);

for ($i = 0; $i < $count; $i++) {

    $endDate    = date($list[$i]['wr_14']); //Due Date

    $today      = date("m-d-y");            //오늘 날짜

 

    $status = null;

    $backgroundColor = null;

 

    if ($list[$i]['wr_18'] == 'Active') {

        if ($list[$i]['wr_17'] == '0') {

            $status             = "Paid";

            $backgroundColor    = "#45d864";

        } elseif ($list[$i]['wr_17'] == $list[$i]['wr_15']) {

            $status             = "Unpaid";

            $backgroundColor    = "#e85fc6";

        } else {

            if ($endDate < $today) {

                $status             = "Overdue";

                $backgroundColor    = "#ff381e";

            } else {

                $status             = "Partial";

                $backgroundColor    = "#ebb048";

            }

        }

    } else {

        // 내야할 돈의 범위가 양수라면 향필요없는 조건문인듯..

        if ($list[$i]['wr_17'] >= '0') {

            $status             = "Draft";

            $backgroundColor    = "#a0a0a0";

        }

    }

 

    if (isset($status) && isset($backgroundColor)) {

?>

    <span style="padding:5px 15px; background-color: <?php echo $backgroundColor ?>; border-radius: 5px; text-align: center; color: #FFFFFF; font-weight: 700; display:inline-block; width:90px;">

        <?php echo $status ?>

    </span>

    <br>

<?php

    }

}

답변에 대한 댓글 1개

아고 이렇게 까지.. 시간을 내주셔서 감사합니다. 정말로 많은 도움이 되었습니다.

조건의 우선순위가 중요 할 것 같습니다.

본문에 적어주신 조건을 확인해봤을 때 다소 복잡한 여러가지의 상황이 나오는걸로 추측됩니다.

따라서 우선순위를 다시 정해주시는게 좋을 것 같습니다.

답변을 작성하려면 로그인이 필요합니다.