2015-05-21 77 views
3

目前我正在試圖讓預訂系統看起來像這樣日曆系統預訂錯誤,PHP循環出了錯

enter image description here

(在HTML &有點PHP的簡易完成)應用後腳本和功能,我得到這個:

enter image description here

代碼:

function displayCalendar(){ 
    global $database; 
    global $smarty; 
    $sID = $_GET['serverid']; 
    $database->query('SELECT * FROM bookings WHERE sID = :server'); 
    $database->bind(':server',$sID); 
    $getServer = $database->fetchAll(); 
    $week = $_GET['week']; 

    $times = array(); 
    for ($h = 6; $h < 18; $h++){ 
     for ($m = 0; $m < 60 ; $m += 60){ 
      $time = sprintf('%02d:%02d', $h, $m); 
      $times["'$time'"] = "$time"; 
     } 
    } 

    $days = array(
     'Monday', 
     'Tuesday', 
     'Wednesday', 
     'Thursday', 
     'Friday', 
     'Saturday', 
     'Sunday', 
    ); 

    echo '<tr>'; 
    for ($i = 0; $i <= 6; $i++) { 
     echo '<th>'.$days[$i].'</th>'; 
    } 
    echo '</tr>'; 

    foreach ($times as $time) 
    { 
     echo '<tr>'; 
     for($i = 0; $i <= 6; $i++) { 
      foreach($getServer as $test => $row){ 
       if($row['time'] == $time && $row['day'] == $days[$i] && $row['week'] == $week) 
       { 
        echo '<td><button type="submit" class="btn btn-danger" aria-label="Left Align">'.$row['time'].'</button></td>'; 
       } 
       else 
       { 
        echo '<td><button type="submit" class="btn btn-success" aria-label="Left Align"><a href="?page=booking&serverid='.$sID.'&week='.$week.'&day='.$days[$i].'&time='.$time.'">'.$time.'</a></button></td>'; 
       } 
      } 
     } 
     echo '</tr>'; 
    } 
} 
+0

您嘗試使用不同的變量內循環,可以說'$ j' – hoss

+0

作爲一個建議,你應該訓練自己獨立的代碼邏輯和表示。首先構建你的數組以獲得結構化的結果,然後將其顯示在html中。 – goto

+0

嗨,內循環不受上一個循環的影響。 關於建議,我這樣做。的print_r();但現在這個問題是由於某種原因,當從數據庫中包含一個foreach循環時,它會這樣做。 –

回答

1

我對代碼做了一些改動,但是讓它起作用。

關鍵是hasData()函數循環,如果找到數據,它會執行一個返回循環。

你的代碼繼續循環,每次迭代都會回顯。

function getData(){ 
    global $database; 
    $sID = $_GET['serverid']; 
    $database->query('SELECT * FROM bookings WHERE sID = :server'); 
    $database->bind(':server',$sID); 
    $getServer = $database->fetchAll(); 

    return $getServer; 
} 


function hasData($getServer, $time, $day, $week, $sID){ 
    foreach($getServer as $row){ 
     if($row['time'] == $time && $row['day'] == $day && $row['week'] == $week) 
     { 
      return '<td><button type="submit" class="btn btn-danger" aria-label="Left Align">'.$time.'</button></td>'; 
      break; 
     } 
    } 
    return '<td><button type="submit" class="btn btn-success" aria-label="Left Align"><a href="?page=booking&serverid='.$sID.'&week='.$week.'&day='.$day.'&time='.$time.'">'.$time.'</a></button></td>'; 
} 

function displayCalendar(){ 
    global $database, $smarty; 
    $sID = $_GET['serverid']; 
    $getServer = getData(); 

    $week = $_GET['week']; 

    $times = array(); 
    for ($h = 6; $h < 18; $h++){ 
     for ($m = 0; $m < 60 ; $m += 60){ 
      $time = sprintf('%02d:%02d', $h, $m); 
      $times["'$time'"] = "$time"; 
     } 
    } 

    $days = array(
     'Monday', 
     'Tuesday', 
     'Wednesday', 
     'Thursday', 
     'Friday', 
     'Saturday', 
     'Sunday', 
    ); 

    echo '<tr>'; 
    for ($i = 0; $i <= 6; $i++) { 
     echo '<th>'.$days[$i].'</th>'; 
    } 
    echo '</tr>'; 

    foreach ($times as $time) 
    { 
     echo '<tr class="time">'; 
     for($i = 0; $i <= 6; $i++) { 
      echo hasData($getServer, $time, $days[$i], $week, $sID); 
     } 
     echo '</tr>'; 
    } 
} 
+0

需要更乾淨一點。 –

+0

Works thaaaank uuuuuuuuu! –