묻고답하기

아래문장은 내용보기에서 아랫글 윗글 을 보여주는 db문인데요...
글쓰기를 하면 reply_flag=0 을 넣고 답변을 하게 되면 reply_flag=1 로 주어서 원글과 답변
글을 나누어습니다.
그런데 문제는 아랫글과 윗글이 있을경우
아랫글만 있을경우
윗글만 있을경우
잘 표현을 못하겠네요...
밑에 처럼 하니까 답변글이 없을때는
원본글을 누러고 들어가면 잘 나오는데
답변글이 있을때는 답변글을 누러고 들어가면
엉뚱하게 나옵니다.....
next글 나오고 next의 글제목은 prev 의 글제목이 나오네요.. 에구 미쵸 ㅜ,ㅜ
제로보드 이전글,다음글 소스를 봐도 초보라서 뭐가 뭔지 모르겠습니다.
php 잘하는분 해결책좀 쉽게 가르쳐주세요...
이것 가지고 벌서 일주일째 생각하고 검색하고 있습니다.
help me~~~~~~~~~~~~~~~~~~~~~~~~~~~~



$query1 = mysql_fetch_array(mysql_query("select * from $code where uid < $number and reply_flag='0' order by uid desc limit 0,1"));  // 아랫글 퀴리
$my_uid1 = $query1[uid];
$my_subject1 = $query1[subject];
$my_name1 = $query1[name];
$my_signdate1 = date("Y-m-d",$query1[signdate]);

$query2 = mysql_fetch_array(mysql_query("select * from $code where uid > $number and reply_flag='0' order by uid asc limit 0,1"));  // 윗글 퀴리
$my_uid2 = $query2[uid];
$my_subject2 = $query2[subject];
$my_name2 = $query2[name];
$my_signdate2 = date("Y-m-d",$query2[signdate]);


if($query1 && $query2) {
echo ("<table width=600 border=0 cellspacing=0 cellpadding=0>
        <tr onmouseover="this.style.backgroundColor='#fafafa'"

onmouseout="this.style.backgroundColor='+ '+ ''">
          <td width=50 height=22><font size=2 color=a9a9a9> Prev</font></td>
          <td width=350 height=22><font size=2 color=555555> <a

href=view.php?code=$code&number=$my_uid2>$my_subject2</a></font></td>
          <td width=100 height=22 align=center><font size=2

color=555555> $my_name2</font></td>
          <td width=100 height=22 align=right><font size=2

color=555555> $my_signdate2 </font></td>
        </tr>
       </tr>
        <tr>
          <td bgcolor=f5f5f5 height=1 colspan=4></td>
        </tr>
        <tr onmouseover="this.style.backgroundColor='#fafafa'"

onmouseout="this.style.backgroundColor=''">
          <td width=50 height=22><font size=2 color=a9a9a9> Next</font></td>
          <td width=350 height=22><font size=2 color=555555> <a

href=view.php?code=$code&number=$my_uid1>$my_subject1</a></font></td>
          <td width=100 height=22 align=center><font size=2

color=555555> $my_name1</font></td>
          <td width=100 height=22 align=right><font size=2

color=555555> $my_signdate1 </font></td>
        </tr>
        <tr>
          <td bgcolor=c9c9c9 height=1 colspan=4></td>
        </tr>
      </table>");
} else if($query2) {   // 윗글만 나타내기
echo ("<table width=600 border=0 cellspacing=0 cellpadding=0>
        <tr onmouseover="this.style.backgroundColor='#fafafa'"

onmouseout="this.style.backgroundColor=''">
          <td width=50 height=22><font size=2 color=a9a9a9> Prev</font></td>
          <td width=350 height=22><font size=2 color=555555> <a

href=view.php?code=$code&number=$my_uid2>$my_subject2</a></font></td>
          <td width=100 height=22 align=center><font size=2

color=555555> $my_name2</font></td>
          <td width=100 height=22 align=right><font size=2

color=555555> $my_signdate2 </font></td>
        </tr>
        <tr bgcolor=c9c9c9>
          <td colspan=4 height=1></td>
        </tr>
      </table>");
} else if($query1) {            // 아랫글만 나타내기
echo ("<table width=600 border=0 cellspacing=0 cellpadding=0>
        <tr onmouseover="this.style.backgroundColor='#fafafa'"

onmouseout="this.style.backgroundColor=''">
          <td width=50 height=22><font size=2 color=a9a9a9> Next</font></td>
          <td width=350 height=22><font size=2 color=555555> <a

href=view.php?code=$code&number=$my_uid1>$my_subject1</a></font></td>
          <td width=100 height=22 align=center><font size=2

color=555555> $my_name1</font></td>
          <td width=100 height=22 align=right><font size=2

color=555555> $my_signdate1 </font></td>
        </tr>
        <tr bgcolor=c9c9c9>
          <td colspan=4 height=1></td>
        </tr>
      </table>");
}