표를 만들고 그 안에다가 출력시키는 쿼리는 요????
본문
이리저리 해봐도 안되어서 좀 힌트좀 얻고자 질문 드립니다.
예를 들어...
------------------------------------------------------
| 년수 | 1월 | 2월 | 3월 | 4월 | 5월 | 6월 |
------------------------------------------------------
| 2014 | | | | | | |
------------------------------------------------------
| 2015 | | | | | | |
------------------------------------------------------
이런 표를 그려 놓고서요... 표는 아래 처럼 그렸 봤습니다...
########################################################################
<table align="center" cellpadding="0" cellspacing="1" width="700" bgcolor="#BCBCBC">
<tr>
<td width="100" height="45" align="center" bgcolor="#E4EAEC"><b>년도</b></td>
<td width="100" align="center" bgcolor="#E4EAEC"><b>1월</b></td>
<td width="100" align="center" bgcolor="#E4EAEC"><b>2월</b></td>
<td width="100" align="center" bgcolor="#E4EAEC"><b>3월</b></td>
<td width="100" align="center" bgcolor="#E4EAEC"><b>4월</b></td>
<td width="100" align="center" bgcolor="#E4EAEC"><b>5월</b></td>
<td width="100" align="center" bgcolor="#E4EAEC"><b>6월</b></td>
</tr>
<?
for ($i=0; $i<5; $i++) { $year = $i+2011;
?>
<tr>
<td width="100" height="45" align="center" bgcolor="#E4EAEC"><b><?=$year?>년도</b></td>
<?
for ($h=0; $h<6; $h++) { $month = $h+1;
$row = mysql_fetch(" select mb_name, r_year, r_month, t_score, tel from g4_score where r_year='$year'");
if($row[r_year]==$year && $row[r_month]==$month ) { $class_score=$row[t_score];}
else{ $class_score="-";}
?>
<td width="100" align="center" bgcolor="#ffffff">
<?=$year?>-<?=$month?><br><?=$class_score?><!-- 성적 넣는곳 -->
</td>
<?}?>
</tr>
<?}?>
</table>
########################################################################
for 문으로 그렸어요... for 문으로 몇번 그려라... 라고 만들었습니다.
그런데요...
디비는 score 라는 테이블에 있습니다. 그럼 그 디비를 쿼리 해와야 하는데요..
요기서부터가 감이 안와요...
r_year 필드에 연도가 있어요.. 그 년도와 같은걸 레코드를 불러와서 표안에 성적을 뿌려줘야 되거든요.
두번째 for문에서 td를 그릴때 많은횟수의 쿼리를 하게 되지만 테스트할겸 빨간색 처럼 쿼리를 했더니 역시나 한개만 가져오드라구요.
그래서 while 문으로 sql_fetch_array($row) 로 돌리면 있는 것만 가져오니 표가 깨지드라구요...
왜냐면 성적이 없는 경우는 공란으로 가야 되자나요.. 100프로 다 성적이 있어서 칸을 모두 채운다면 좋겠지만 비어있는 칸도 생기기 때문입니다.
for문안에다가 다시 while 문을 돌려보고... while문을 먼저 돌리고 for문을 돌려 봐도..ㅠㅠㅠㅠ
삼일째 머리싸메다가 고수님게 힌트 얻고자 글을 올립니다.
저 양식데로 있는 것만 그 양식안에다가 뿌려주는 반복문을 어떻게 해야 하나요?
꼭 부탁드립니다.
답변 1
값이 비어있다면 그부분을 체크해서 <td></td> 를 뿌려줌 되죠
예를들어 $score 라는 변수에 값이 읍다면
if(!$score) echo "<td></td>"; else echo "<td>{$score}</td>";
이렇게 하면.. 될듯한데요.