혹시, echo문 안에서 3항 연산문 사용 가능한가요?

혹시, echo문 안에서 3항 연산문 사용 가능한가요?

QA

혹시, echo문 안에서 3항 연산문 사용 가능한가요?

본문

echo "<div class=\"item\">".latest("AAA", $latest_table, 5, 50, 0)."</div>"; 

 

위 AAA 표현 자리에 게시판이 B01이면 BBB 스킨, 그외는 AAA 스킨 사용이라는 조건문 삽입 가능한가요?

이 질문에 댓글 쓰기 :

답변 4

echo "<div class=\"item\">".$bo_table == "b01" ? latest("BBB", $latest_table, 5, 50, 0) : latest("AAA", $latest_table, 5, 50, 0) ."</div>";

 

이런 식으로 바꿔 보세요.

저렇게 하면 복잡해 보이지 않나요? 가독성도 떨어지구요.

 

$skin = $bo_table == "b01" ? "BBB" : "AAA";

echo "<div class=\"item\">".latest($skin, $latest_table, 5, 50, 0)."</div>"; 

답변 고맙습니다.


근데, 아래처럼 넣어봤는데, 에러 나네요.


<div class="latest">
<?php
$i = 1;
while ($latest_table = array_shift($list)) {
    $mw_skin_config = mw_skin_config($latest_table);
    if ($mw_skin_config['cf_attribute'] == "1:1") continue;
    echo "<div class=\"item\">".($latest_table == "S02") ? latest("mw5-gallery", $latest_table, 5, 50, 0) : latest("mw5", $latest_table, 5, 50, 0) ."</div>";

    if (($i++)%2==0) echo "</div><div class=\"latest\">";
}
?>
</div>


아래처럼 넣어봐도 에러나구요.



<div class="latest">
<?php
$i = 1;
while ($latest_table = array_shift($list)) {
    $mw_skin_config = mw_skin_config($latest_table);
    if ($mw_skin_config['cf_attribute'] == "1:1") continue;
$skin = $latest_table == "S02" ? "mw5-gallery" : "mw5";
    echo "<div class=\"item\">".latest($skin, $latest_table, 5, 50, 0)."</div>";

    if (($i++)%2==0) echo "</div><div class=\"latest\">";
}
?>
</div>

echo 조건문 ? true : false;
echo 안에 쓰는 게 맞습니다.

echo $bo_table == "b01" ? "<div class='item'>".latest("BBB", $latest_table, 5, 50, 0)."</div>" : "<div class='item'>".latest("AAA", $latest_table, 5, 50, 0)."</div>";

이런 식으로 바꿔 보세요.

근데 while 문부터 조금 이상하네요.
while($latest_table = array_shift($list))
여기 $latest_table엔 배열이 들어갈 텐데 그 밑에 $latest_table == "S02" 이게 비교가 되나요?
앞은 배열이고 뒤는 문자열인데...

어쨌든 위처럼 바꿔서 사용해 보세요.
그리고 오류가 나면 어떤 오류가 나는지 알려 주세요.

아래처럼 넣었습니다.


<div class="latest">
<?php
$i = 1;
while ($latest_table = array_shift($list)) {
    $mw_skin_config = mw_skin_config($latest_table);
    if ($mw_skin_config['cf_attribute'] == "1:1") continue;
echo $latest_table == "S02" ? "<div class='item'>".latest("mw5-gallery", $latest_table, 5, 50, 0)."</div>" : "<div class='item'>".latest("mw5", $latest_table, 5, 50, 0)."</div>";

    if (($i++)%2==0) echo "</div><div class=\"latest\">";
}
?>
</div>



그랬더니, 아래와 같은 에러가 뜨네요.


Warning: include(C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php): failed to open stream: No such file or directory in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(): Failed opening 'C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php' for inclusion (include_path='.') in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(C:/AutoSet9/public_html/mw5/skin/latest/mw5-gallery/latest.skin.php): failed to open stream: No such file or directory in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(): Failed opening 'C:/AutoSet9/public_html/mw5/skin/latest/mw5-gallery/latest.skin.php' for inclusion (include_path='.') in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php): failed to open stream: No such file or directory in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(): Failed opening 'C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php' for inclusion (include_path='.') in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php): failed to open stream: No such file or directory in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(): Failed opening 'C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php' for inclusion (include_path='.') in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php): failed to open stream: No such file or directory in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(): Failed opening 'C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php' for inclusion (include_path='.') in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php): failed to open stream: No such file or directory in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(): Failed opening 'C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php' for inclusion (include_path='.') in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php): failed to open stream: No such file or directory in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(): Failed opening 'C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php' for inclusion (include_path='.') in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php): failed to open stream: No such file or directory in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(): Failed opening 'C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php' for inclusion (include_path='.') in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php): failed to open stream: No such file or directory in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(): Failed opening 'C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php' for inclusion (include_path='.') in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php): failed to open stream: No such file or directory in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

Warning: include(): Failed opening 'C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php' for inclusion (include_path='.') in C:\AutoSet9\public_html\mw5\lib\latest.lib.php on line 76

알려주신 방법대로 하면 분명 나와야 하는데.....



<div class="latest">
<?php
$i = 1;
while ($latest_table = array_shift($list)) {
    $mw_skin_config = mw_skin_config($latest_table);
    if ($mw_skin_config['cf_attribute'] == "1:1") continue;
    $skin = $latest_table == "S02" ? "mw5-gallery" : "mw5";
    echo "<div class=\"item\">".latest($skin, $latest_table, 5, 50, 0)."</div>";

    if (($i++)%2==0) echo "</div><div class=\"latest\">";
}
?>
</div>



위에 적은 에러와 동일하게 나오네요. 
C:/AutoSet9/public_html/mw5/skin/latest/mw5-gallery/latest.skin.php 파일은 정상적으로 있습니다.

문법적으로는 이상이 없어 보이는데...왜 에러가 나는지 도무지 모르겠네요.



아래처럼 하면 나오긴 하지만,  갤러리형 최신글 위치가 엉뚱한 데 뿌려지고 말이죠.


<div class="latest">
<?php
$i = 1;
while ($latest_table = array_shift($list)) {
    $mw_skin_config = mw_skin_config($latest_table);
    if ($mw_skin_config['cf_attribute'] == "1:1") continue;
if ($latest_table == 'S02')
    echo "<div class=\"item\">".latest("theme/mw5-gallery", $latest_table, 2, 50, 0)."</div>";
else
    echo "<div class=\"item\">".latest("theme/mw5", $latest_table, 5, 50, 0)."</div>";

    if (($i++)%2==0) echo "</div><div class=\"latest\">";
}
?>
</div>

Warning: include(C:/AutoSet9/public_html/mw5/skin/latest/mw5/latest.skin.php): failed to open stream: No such file or directory in ~~
이 경고를 가장 먼저 없애야 할 듯하네요.
파일 한 줄씩 echo로 찍어 보면서 디버깅 하시는 게 제일 나을 겁니다.

echo 문 자체에 조건을 걸면 안되는건가요?

 

if(테이블 == A){

  echo "<div class=\"item\">".latest("AAA", $latest_table, 5, 50, 0)."</div>";  

}

 

아래처럼 넣으면 나오긴 하는데, 정렬이 이상하게 돼요. 아마도, php문의 조건문 흐름탓 같아요.

그래서, 일부러 3항 연산문을 사용하려는건데, echo문 안에 3항 연산문이 들어가면 에러나네요.

 

<div class="latest">

<?php

$i = 1;

while ($latest_table = array_shift($list)) {

    $mw_skin_config = mw_skin_config($latest_table);

    if ($mw_skin_config['cf_attribute'] == "1:1") continue;

if ($latest_table == 'S02')

    echo "<div class=\"item\">".latest("theme/mw5-gallery", $latest_table, 5, 50, 0)."</div>";

else

    echo "<div class=\"item\">".latest("theme/mw5", $latest_table, 5, 50, 0)."</div>";

 

    if (($i++)%2==0) echo "</div><div class=\"latest\">";

}

?>

</div>

 

 

 

bef6adccaa9163ce92efb02cb7348669_1448963025_379.gif
 

답변을 작성하시기 전에 로그인 해주세요.
전체 119
QA 내용 검색

회원로그인

(주)에스아이알소프트 / 대표:홍석명 / (06211) 서울특별시 강남구 역삼동 707-34 한신인터밸리24 서관 1404호 / E-Mail: admin@sir.kr
사업자등록번호: 217-81-36347 / 통신판매업신고번호:2014-서울강남-02098호 / 개인정보보호책임자:김민섭(minsup@sir.kr)
© SIRSOFT