2010-06-24 2 views
0

여기에 내 코드가 있습니다.PHP while 루프 및 테이블 헤더는 db 값이 설정된 경우에만 표시됩니다.

<table border="1" style="width:800px"> 
       <?php $schedule_db = mysql_query("SELECT * FROM schedule WHERE '00:00' is not null and '01:00' is not null and '02:00' is not null and '03:00' is not null and '04:00' is not null and '05:00' is not null and '06:00' is not null and '07:00' is not null and '08:00' is not null and '09:00' is not null and '10:00' is not null and '11:00' is not null and '12:00' is not null and '13:00' is not null and '14:00' is not null and '15:00' is not null and '16:00' is not null and '17:00' is not null and '18:00' is not null and '19:00' is not null and '20:00' is not null and '21:00' is not null and '22:00' is not null and '23:00' is not null") 
       or die(mysql_error()); ?> 
       <form method="post" action="config_action.php"> 
       <?php while($schedule = mysql_fetch_array($schedule_db)) { ?> 
        <tr><?php if(isset($schedule['00:00'])) { ?><td style="width:32px"><?php echo $schedule['00:00'] ?></td><?php } if(isset($schedule['02:00'])) { ?><td style="width:32px"><?php echo $schedule['02:00'] ?></td><?php } if(isset($schedule['03:00'])) { ?><td style="width:32px"><?php echo $schedule['03:00'] ?></td><?php } if(isset($schedule['04:00'])) { ?><td style="width:32px"><?php echo $schedule['04:00'] ?></td><?php } if(isset($schedule['05:00'])) { ?><td style="width:32px"><?php echo $schedule['05:00'] ?></td><?php } if(isset($schedule['06:00'])) { ?><td style="width:32px"><?php echo $schedule['06:00'] ?></td><?php } if(isset($schedule['07:00'])) { ?><td style="width:32px"><?php echo $schedule['07:00'] ?></td><?php } if(isset($schedule['08:00'])) { ?><td style="width:32px"><?php echo $schedule['08:00'] ?></td><?php } if(isset($schedule['09:00'])) { ?><td style="width:32px"><?php echo $schedule['09:00'] ?></td><?php } if(isset($schedule['10:00'])) { ?><td style="width:32px"><?php echo $schedule['10:00'] ?></td><?php } if(isset($schedule['11:00'])) { ?><td style="width:32px"><?php echo $schedule['11:00'] ?></td><?php } if(isset($schedule['12:00'])) { ?><td style="width:32px"><?php echo $schedule['12:00'] ?></td><?php } if(isset($schedule['13:00'])) { ?><td style="width:32px"><?php echo $schedule['13:00'] ?></td><?php } if(isset($schedule['14:00'])) { ?><td style="width:32px"><?php echo $schedule['14:00'] ?></td><?php } if(isset($schedule['15:00'])) { ?><td style="width:32px"><?php echo $schedule['15:00'] ?></td><?php } if(isset($schedule['16:00'])) { ?><td style="width:32px"><?php echo $schedule['16:00'] ?></td><?php } if(isset($schedule['17:00'])) { ?><td style="width:32px"><?php echo $schedule['18:00'] ?></td><?php } if(isset($schedule['19:00'])) { ?><td style="width:32px"><?php echo $schedule['19:00'] ?></td><?php } if(isset($schedule['20:00'])) { ?><td style="width:32px"><?php echo $schedule['20:00'] ?></td><?php } if(isset($schedule['21:00'])) { ?><td style="width:32px"><?php echo $schedule['21:00'] ?></td><?php } if(isset($schedule['22:00'])) { ?><td style="width:32px"><?php echo $schedule['22:00'] ?></td><?php } if(isset($schedule['23:00'])) { ?><td style="width:32px"><?php echo $schedule['23:00'] ?></td><?php } ?></tr> 
       <?php } ?> 
       </form> 
</table> 

내가하고 싶은 것은 컬럼에 값이있는 경우 각 열이 있지만 무엇인지 시간을 표시하는 테이블 헤더가 (다른 모든 행 위의 단지 행)입니다.

내가 좋아하는 뭔가 내가 코드를 생각 : 그것은 잠시 루프이기 때문에, 그것이 내가 발생하는 것이 아닌 모든 실제 행하기 전에 일어날

<tr><?php if(isset($schedule['00:00'])) { ?><td style="width:32px">00:00</td><?php } if(isset($schedule['01:00'])) { ?><td style="width:32px">01:00</td><?php } ?> etc. 

, 난 그냥 원하는 맨 위에 한 번 일어난다.

이 문제를 어떻게 해결할 수 있습니까? 한 번만 루프를 만들 수 있도록 추가 할 수있는 것이 있습니까?

감사합니다.

답변

1

그런 다음 while 루프 내에서 행을 출력하지 마십시오. 모든 데이터를 var에 저장하십시오. 그리고 var에 내용이 포함되어 있으면 머리글을 내 보낸 다음 var에 모든 내용 행을 포함시킵니다.

편집

좋아 그게 내가 쓴 가장 추한 코드, 당신은 아마도 코드를 훨씬 작게 만들 배열과 물건을 사용할 수 있습니다. 그러나 이것은 아이디어를 얻기위한 코드 스 니펫입니다. 모든 행을 캡처하십시오. 그렇게하는 동안 각 필드가 채워져 있는지 확인하십시오. 그렇다면 머리글에있는 열이 필요합니다. 예를 들어 data0true으로 설정하면됩니다. 전체 데이터를 처리 한 후에는 각 var를 확인하고 사실인지 확인합니다. 그렇다면, 머리말에 열을 인쇄하는 것보다. 그래서 결국에는 테이블 태그를 풀고 headrow-var를 입력 한 다음 서식이 지정된 모든 데이터와 닫는 테이블 태그가있는 문자열을 보게됩니다. 기본 아이디어를 얻습니다. 헤드로와 다른 행을 별개의 병에 보관하고 올바른 순서로 배치하십시오 ...

// Get data from database-table... 
$schedule_db = mysql_query('...your query...') or die(mysql_error()); 

$data0 = false; 
$data1 = false; 
$data2 = false; 
// reapeat this for all hours till 23... 

while($schedule = mysql_fetch_array($schedule_db)) 
{ 
    if(isset($schedule['00:00'])) 
    { 
     $data0 = true; 
     $row = '<td style="width: 32px;">' . $schedule['00:00'] . '</td>'; 
    } 

    // Reapeat this for every hour you need... 
    if(isset($schedule['01:00'])) 
    { 
     $data1 = true; 
     $row .= '<td style="width: 32px;">' . $schedule['01:00'] . '</td>'; 
    } 

    //... 

    $rows .= $row; 
} 

$headrow = '<tr>'; 
if($data0 === true) 
    $headrow .= '<th>00:00</th>' 

if($data1 === true) 
    $headrow .= '<th>01:00</th>'; 

// Repeat for all hours... 


$headrow = '</tr>'; 

echo '<table>'; 
echo $headrow; 
echo $rows; 
echo '</table>'; 
+0

이 문장을 다시 작성해주십시오. 매우 명확하지 않습니다. – Sam

+0

내 게시물을 편집했습니다. 이것은 추한 것보다 낫지 만 기본적인 생각을 얻길 바랍니다. –

+0

지금, 고맙습니다. :). – Sam