if문 질문 좀 할게요 ㅠㅠ

if문 질문 좀 할게요 ㅠㅠ

QA

if문 질문 좀 할게요 ㅠㅠ

답변 4

본문


<span><?php echo $list[$i]['wr_9'];?></span>

 

질문1. $list[$i]['wr_9'] 공란일 땐 무조건 "델루나"로 보여지고, span에 "c" 클래스 추가

 

질문2. $list[$i]['wr_9'] 값이 "홍길동" 일 땐 span에 "a" 클래스 추가

         $list[$i]['wr_9'] 값이 "김철수" 일 땐 span에 "b" 클래스 추가

 

if문을 어떻게 작성해야 할까요??

이 질문에 댓글 쓰기 :

답변 4

<?php

if ($list[$i]['wr_9'] == '') {

  $txt = "델루나";

  $class = "c";

} else if ($list[$i]['wr_9'] == '홍길동') {

  $txt = "";

  $class = "a";

} else if ($list[$i]['wr_9'] == '김철수') {

  $txt = "";

  $class = "b";

}

?>

<span class="<?php echo $class ?>"><?php echo $txt ?></span>

<?php

  $txt =$list[$i]['wr_9'];

if ($list[$i]['wr_9'] == '') {

  $txt = "델루나";

  $class = "c";

} else if ($list[$i]['wr_9'] == '홍길동') {

  $class = "a";

} else if ($list[$i]['wr_9'] == '김철수') {

  $class = "b";

}

?>

<span class="<?php echo $class ?>"><?php echo $txt ?></span>

if 한번으로 끝내기

<?php

$wr_9 = $list[$i]['wr_9'];

if(!$wr_9) $wr_9 ='델루나';

$classArr=array('델루나'=>'c, '홍길동'=>'a', '김철수'=>'b');

?>

<span class="<?php echo $classArr[$wr_9];?>"><?php echo $wr_9;?></span>

 

추가적으로 더 늘어날수도 있으니

스위치문으로 사용하시는것도 좋습니다.


<?php
switch ($list[$i]['wr_9']){
    case '홍길동':
        $txt = $list[$i]['wr_9']; // 원래 값이 홍길동이기에 홍길동이 들어감.
        $add_class = 'a';
        break;
    case '김철수':
        $txt = $list[$i]['wr_9']; // 원래 값이 김철수이기에 김철수가 들어감.
        $add_class = 'b';
        break;
    default:
        $txt = '델루나';
        $add_class = 'c';
}
?>
 
<span class="<?php echo $add_class ?>"><?php echo $txt ?></span>
답변을 작성하시기 전에 로그인 해주세요.
QA 내용 검색
질문등록
전체 37
© SIRSOFT
현재 페이지 제일 처음으로