php 1~12월 내용나오게 하려면 어떻게 해야 할까요?
본문
안녕하세요 도움좀 얻고자 합니다.
1~12월을 입력받아
if($wr_1_1) $wr_1=$wr_1_1."|";
if($wr_1_2) $wr_1.=$wr_1_2."|";
if($wr_1_3) $wr_1.=$wr_1_3."|";
if($wr_1_4) $wr_1.=$wr_1_4."|";
if($wr_1_5) $wr_1.=$wr_1_5."|";
if($wr_1_6) $wr_1.=$wr_1_6."|";
if($wr_1_7) $wr_1.=$wr_1_7."|";
if($wr_1_8) $wr_1.=$wr_1_8."|";
if($wr_1_9) $wr_1.=$wr_1_9."|";
if($wr_1_10) $wr_1.=$wr_1_10."|";
if($wr_1_11) $wr_1.=$wr_1_11."|";
if($wr_1_12) $wr_1.=$wr_1_12."|";
$wr_1 = march|april||may|june;
리스트 페이지에서 $wr_1 변수를 $wr_1 = explode("|", $wr_1);
배열로 나누어서 $wr_1[0] = march; 결과값은 이건데
1월에 매칭하려면 밸류값은 $wr_1[0] = march 이므로 이부분을
1~12월에 어떻게 매칭을 하면 될까요 많은 도움 부탁드립니다.
<table>
<caption>과정</caption>
<tr>
<th>프로그램 제목</th>
<td>1월</td>
<td>2월</td>
<td>3월</td>
<td>4월</td>
<td>5월</td>
<td>6월</td>
<td>7월</td>
<td>8월</td>
<td>9월</td>
<td>10월</td>
<td>11월</td>
<td>12월</td>
</tr>
<?php
for ($i=0; $i<count($list); $i++) {
$wr_1 = explode("|", $wr_1);
?>
<tr>
<th><?php echo $list[$i]['subject'] ?></th>
<td><?php if($wr_1 == "january") echo cut_str(strip_tags($list[$i]['content']),100)?> </td>
<td><?php if($wr_1 == "february") echo cut_str(strip_tags($list[$i]['content']),100)?></td>
<td><?php if($wr_1 == "march") echo cut_str(strip_tags($list[$i]['content']),100)?></td>
<td><?php if($wr_1 == "april") echo cut_str(strip_tags($list[$i]['content']),100)?></td>
<td class="bg_red"><?php if($wr_1 == "may") echo cut_str(strip_tags($list[$i]['content']),100)?></td>
<td><?php if($wr_1 == "june") echo cut_str(strip_tags($list[$i]['content']),100)?></td>
<td><?php if($wr_1 == "july") echo cut_str(strip_tags($list[$i]['content']),100)?></td>
<td><?php if($wr_1 == "august") echo cut_str(strip_tags($list[$i]['content']),100)?></td>
<td><?php if($wr_1 == "september") echo cut_str(strip_tags($list[$i]['content']),100)?></td>
<td><?php if($wr_1 == "october") echo cut_str(strip_tags($list[$i]['content']),100)?></td>
<td><?php if($wr_1 == "november") echo cut_str(strip_tags($list[$i]['content']),100)?></td>
<td><?php if($wr_1 == "december") echo cut_str(strip_tags($list[$i]['content']),100)?></td>
</tr>
<?php } ?>
<?php if (count($list) == 0) { echo '<tr><td colspan="'.$colspan.'" class="empty_table">게시물이 없습니다.</td></tr>'; } ?>
</table>
답변 3
<tr>
<th><?php echo $list[$i]['subject'] ?></th>
<?php
$wr1 = explode("|", $wr_1);
$months = array("january", "february", "march");
foreach($months as $v) {
echo "<td>";
echo in_array($v, $wr1) ? cut_str(strip_tags($list[$i]['content']), 100) : "";
echo "</td>";
}
?>
</tr>
$months = array("january", "february", "march");
여기는 뒤에 4월부터 채워 넣으세요. 너무 길어져서 안 넣었습니다.
!-->$list[$i]['wr_1']에 있는 월에 맞는
<td>에 $list[$i]['content']를 뿌려준다.
(content <--이게 맞나 확인도 필요합니다.
아니라면 $list[$i]['wr_content'] 입니다.)
제가 이해한게 맞다면, 이렇게 하면 됩니다.
<table>
<caption>과정</caption>
<tr>
<th>프로그램 제목</th>
<?php
for ($j=1; $j<=12; $j++) {
echo "<td>".$j."월</td>";
}
?>
</tr>
<?php
for ($i=0; $i<count($list); $i++) {
?>
<tr>
<th><?php echo $list[$i]['subject'] ?></th>
<?php
$month_arr = array("january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december");
for ($j=0; $j<count($month_arr); $j++) {
echo "<td>";
if (strpos($list[$i]['wr_1'], $month_arr[$j]) !== false){
echo cut_str(strip_tags($list[$i]['content']),100);
}
echo "</td>";
}
?>
</tr>
<?php } ?>
<?php if (count($list) == 0) { echo '<tr><td colspan="'.$colspan.'" class="empty_table">게시물이 없습니다.</td></tr>'; } ?>
</table>
이런경우에는
배열을 가지고 월의 이름에 따른 배열을 선언합니다.
$monkind = array("january", "february", .........., "december");
그리과 작성하신 위의 로직에서 문법이 틀린부분은
if($wr_1 == "january")
이 문장과 동일한 구문은 전부 오류가 납니다.
왜냐면
$wr_1 = explode("|", $wr_1);
이렇게 작성한 내용에 따라서,
결과값은
$wr_1 은 배열로 구성됩니다. 몇개가 존재할지는 모릅니다만
원하시는 형태는
1월~12월까지 가로로 나열되고
각 월에 값이 있으면 해당 Row의 정보를 보이게 하고 싶은신것 같습니다.
for문 이하만 변경해서 작성해봅니다.
$monkind = array("january", "february", .........., "december"); // 내용은 직접채우세요
for ($i=0; $i<count($list); $i++) {
$list_wr1 = explode("|", $wr_1); //이름이 중첩되지 않게 합니다.
?>
<tr>
<th><?php echo $list[$i]['subject'] ?></th>\
<?php
for ($j = 0; $j < count($monkind); $j++ ) {?>
<td><?php if($list_wr1[$monkind[$j]) echo cut_str(strip_tags($list[$i]['content']),100)?> </td>
<?php
} ?>
</tr>
<?php } ?>
<?php if (count($list) == 0) { echo '<tr><td colspan="'.$colspan.'" class="empty_table">게시물이 없습니다.</td></tr>'; } ?>
</table>
이렇게 변경해보세요
!-->!-->