테스트 사이트 - 개발 중인 베타 버전입니다

PHP FOR문 활용법 문의드려요

빗물소리 6년 전 조회 2,445

   echo "\n";       echo "403\n";    for($i = 1; $i <= 5; $i++) {     $totSUM_s[$i] = $sProduct_dio_s[$i];     $totSUM_w[$i] = $sProduct_dio_w[$i];     $totSUM_sw[$i] = $sProduct_dio_sw[$i]; 

    echo "".number_format($sProduct_dio_sw[$i],0)."\n";     echo "".number_format($sProduct_dio_s[$i],0)."\n";        echo "".number_format($sProduct_dio_w[$i],0)."\n";    }       echo "\n";        echo "\n";       for($i = 1; $i <= 5; $i++) {       $totSUM_s[$i] = $totSUM_s[$i] + $sProduct_wiz_s[$i];     $totSUM_w[$i] = $totSUM_w[$i] + $sProduct_wiz_w[$i];     $totSUM_sw[$i] = $totSUM_sw[$i] + $sProduct_wiz_sw[$i];        echo "".number_format($sProduct_wiz_sw[$i],0)."\n";     echo "".number_format($sProduct_wiz_s[$i],0)."\n";       echo "".number_format($sProduct_wiz_w[$i],0)."\n";    }       echo "\n";

?>

 

 

2개의 FOR 문을 1개로 만들수는 없을까요??? 라인이 너무 길어지네요 . 관리하기도 어렵고요

배열변수만 다른데 가능할까요???

 

 

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

답변 2개

그누위즈

각 for문이 실행전

tr이 열리고 for문이 끝나면

tr이 닫기기 때문에 억지로 만들면되겠지만

오히려 가독성이 더 떨어질수 있습니다.

지금처럼의 방식으로 사용하시는게 더 보기도 쉬우니

불필요하게 1개로 만들필요성은 안보입니다.

 

로그인 후 평가할 수 있습니다

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

플래토
6년 전

</p>

<p><?php</p>

<p>   $str1 = "";

   $str2 = "";

   $str1 .= "<tr>\n";   

   $str2 .= "<tr>\n";   

   $str1 .= "<td rowspan='4'>403</td>\n";

   for($i = 1; $i <= 5; $i++) {

    $totSUM_s[$i] = $sProduct_dio_s[$i];

    $totSUM_w[$i] = $sProduct_dio_w[$i];

    $totSUM_sw[$i] = $sProduct_dio_sw[$i]; </p>

<p>    $str1 .= "<td class='$strClass'>".number_format($sProduct_dio_sw[$i],0)."</td>\n";

    $str1 .= "<td class='$strClass'>".number_format($sProduct_dio_s[$i],0)."</td>\n";   

    $str1 .= "<td class='$strClass'>".number_format($sProduct_dio_w[$i],0)."</td>\n";

    $totSUM_s[$i] += $sProduct_wiz_s[$i];

    $totSUM_w[$i] += $sProduct_wiz_w[$i];

    $totSUM_sw[$i] +=  $sProduct_wiz_sw[$i];   

    // 여기아래의 출력항목의 변수값을 합에 해당되는 내용으로 변경했습니다.

    $str2 .= "<td class='$strClass'>".number_format($totSUM_sw[$i],0)."</td>\n";

    $str2 .= "<td class='$strClass'>".number_format($totSUM_s[$i],0)."</td>\n";  

    $str2 .= "<td class='$strClass'>".number_format($totSUM_w[$i],0)."</td>\n";

   

   }   

   $str1 .= "</tr>\n";

   $str2 .= "</tr>\n";</p>

<p>   echo $str1;

   echo $str2;

?></p>

<p>

 

를 원하시는거죠?

 

그런데, 합계에 해당되는 내용을 출력하는게 매줄마다 나와야 하나요?

로그인 후 평가할 수 있습니다

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

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

로그인