코드 질문입니다
본문
안녕하세요 긴 코드를 줄여볼려고 하는데 어떻게 해야할지 몰라서 질문을 남깁니다!
<?php if($row['wr_9'] || $row['wr_10']) { ?>
<div class="n_ifo_ct_wrap">
<div class="n_ifo_ct_tit">
<p><?php echo $row['wr_9'] ?></p>
</div>
<div class="rg-n_fi">
<p><?php echo $row['wr_10']?></p>
</div>
</div>
<?php } ?>
<?php if($row['wr_11'] || $row['wr_12']) { ?>
<div class="n_ifo_ct_wrap">
<div class="n_ifo_ct_tit">
<p><?php echo $row['wr_11'] ?></p>
</div>
<div class="rg-n_fi">
<p><?php echo $row['wr_12'] ?></p>
</div>
</div>
<?php } ?>
<?php if($row['wr_13'] || $row['wr_14']) { ?>
<div class="n_ifo_ct_wrap">
<div class="n_ifo_ct_tit">
<p><?php echo $row['wr_13'] ?></p>
</div>
<div class="rg-n_fi">
<p><?php echo $row['wr_14'] ?></p>
</div>
</div>
<?php } ?>
<?php if($row['wr_15'] || $row['wr_16']) { ?>
<div class="n_ifo_ct_wrap">
<div class="n_ifo_ct_tit">
<p><?php echo $row['wr_15'] ?></p>
</div>
<div class="rg-n_fi">
<p><?php echo $row['wr_16'] ?></p>
</div>
</div>
<?php } ?>
<?php if($row['wr_17'] || $row['wr_18']) { ?>
<div class="n_ifo_ct_wrap">
<div class="n_ifo_ct_tit">
<p><?php echo $row['wr_17'] ?></p>
</div>
<div class="rg-n_fi">
<p><?php echo $row['wr_18'] ?></p>
</div>
</div>
<?php } ?>
<?php if($row['wr_19'] || $row['wr_20']) { ?>
<div class="n_ifo_ct_wrap">
<div class="n_ifo_ct_tit">
<p><?php echo $row['wr_19'] ?></p>
</div>
<div class="rg-n_fi">
<p><?php echo $row['wr_20'] ?></p>
</div>
</div>
<?php } ?>
<?php if($row['wr_21'] || $row['wr_22']) { ?>
<div class="n_ifo_ct_wrap">
<div class="n_ifo_ct_tit">
<p><?php echo $row['wr_21'] ?></p>
</div>
<div class="rg-n_fi">
<p><?php echo $row['wr_22'] ?></p>
</div>
</div>
<?php } ?>
<?php if($row['wr_23'] || $row['wr_24']) { ?>
<div class="n_ifo_ct_wrap">
<div class="n_ifo_ct_tit">
<p><?php echo $row['wr_23'] ?></p>
</div>
<div class="rg-n_fi">
<p><?php echo $row['wr_24'] ?></p>
</div>
</div>
<?php } ?>
<?php if($row['wr_25'] || $row['wr_26']) { ?>
<div class="n_ifo_ct_wrap">
<div class="n_ifo_ct_tit">
<p><?php echo $row['wr_25'] ?></p>
</div>
<div class="rg-n_fi">
<p><?php echo $row['wr_26'] ?></p>
</div>
</div>
<?php } ?>
<?php if($row['wr_27'] || $row['wr_28']) { ?>
<div class="n_ifo_ct_wrap">
<div class="n_ifo_ct_tit">
<p><?php echo $row['wr_27'] ?></p>
</div>
<div class="rg-n_fi">
<p><?php echo $row['wr_28'] ?></p>
</div>
</div>
<?php } ?>
<?php if($row['wr_29'] || $row['wr_30']) { ?>
<div class="n_ifo_ct_wrap">
<div class="n_ifo_ct_tit">
<p><?php echo $row['wr_29'] ?></p>
</div>
<div class="rg-n_fi">
<p><?php echo $row['wr_30'] ?></p>
</div>
</div>
<?php } ?>
wr_9 ~ wr_30에서 값이 있는 여분필드는 출력 값이 없는 여분필드는 미출력되게 코드를 짰는데 코드가 효율적이지 못한것 같아서 이것보다 더욱 깔끔하게 코드를 만들려고 하는데 조언좀 구합니다! 도와주세요!
!-->
답변 3
동일한 결과물을 갖는 코드입니다.
<?php
for ($i=9; $i<=30; $i+=2) {
$field1 = 'wr_'.$i;
$field2 = 'wr_'.($i+1);
if ( $row[$field1] || $row[$field2] ) {
?>
<div class="n_ifo_ct_wrap">
<div class="n_ifo_ct_tit">
<p><?=$row[$field1]?></p>
</div>
<div class="rg-n_fi">
<p><?=$row[$field2]?></p>
</div>
</div>
<?php
}
}
예시로 이렇게 한번 해보세요
조건문은 여분필드 9가 비워있지 않으면.. 값이 표시되고 반대로 값이 없으면 p 태그로 표시되요
삼항연산자로 처리 해봤어요
<div class="n_ifo_ct_wrap">
<div class="n_ifo_ct_tit">
<?php echo !empty($row['wr_9']) ? "<p>".$row['wr_9']."</p>" : "<p><p>"; ?>
</div>
<div class="rg-n_fi">
<?php echo !empty($row['wr_10']) ? "<p>".$row['wr_10']."</p>" : "<p><p>"; ?>
</div>
</div>
<?php } ?>
<?php
for($i = 9; $i < 31; $i++) {
if($i % 2 == 1) echo "<div class='n_ifo_ct_wrap'><div class='n_ifo_ct_tit'><p>".$row['wr_'.$i]."</p></div>";
if($i % 2 == 0) echo "<div class='rg-n_fi'><p>".$row['wr_'.$i]."</p></div></div>";
}
?>
이건 혹시 되나요?
!-->
답변을 작성하시기 전에 로그인 해주세요.