2010-06-24 61 views
0

這是我的代碼。PHP while while循環和表頭只顯示數據庫值是否設置

<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. 

然而,因爲它是在一個while循環,它會在每次實際行,這不是我希望發生的事情發生之前,我只是希望它在頂部發生一次。

我怎樣才能解決這個問題?有什麼我可以附加到,使它只循環一次?

謝謝!

回答

1

然後不要在while循環內輸出你的行。將所有數據保存到var中。然後,如果var包含內容,則放出headrow,之後包含所有內容行的var。

編輯

好吧這是我寫過的最醜陋的代碼,你很可能使用數組和東西使代碼更小。但這是獲取想法的代碼片段...捕獲所有行。這樣做時會檢查每個字段是否填寫完整。如果是這樣,那麼您需要在head頭中有一列。我們通過將data0設置爲true來實現此目的。處理完整個數據後,我們檢查每個變量並查看它是否爲真。如果是這樣,那麼你在頭頂上打印柱子。所以最後你會看到,你輸出表格標籤,然後是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 2010-06-24 11:32:19

+0

我編輯了我的帖子。這是比醜陋,但我希望你能得到基本的想法... – 2010-06-24 12:27:41

+0

我明白了,謝謝:)。 – Sam 2010-06-24 13:04:04