2012-05-22 59 views
1

我似乎無法完成這項工作。我從表中獲取數據,如果列中的值= y,那麼必須檢查複選框,如果列中的值= n,則不能選中複選框,所有複選框都保持選中狀態,即使只有第一行的列= Y

<?php 
session_start(); 
    include 'datalogin.php'; 
    $sel_course_id = 2; 
    $sel_date_1 = '2012-05-21'; 
    $sel_date_2 = '2012-05-22'; 
    echo "<form action='' method='post'>"; 
    echo "<strong>Completed the course</strong>"; 

    $sql1 = "SELECT * FROM trn WHERE course_id = '$sel_course_id' AND date_1 = '$sel_date_1' and date_2 = '$sel_date_2'"; 
    $res1 = mysql_query($sql1); 
    while ($row1 = mysql_fetch_assoc($res1)) { 
     $rowid1   = $row1['id']; 
     $uid1    = $row1['usr_id']; 
     $certificate_name1 = $row1['certificate_name']; 
     $course_completed1 = $row1['course_completed']; 

      $sql2 = "SELECT * FROM ex_usrs WHERE id = '$uid1'"; 
      $res2 = mysql_query($sql2); 
      while ($row2 = mysql_fetch_assoc($res2)) { 
       $fn2 = $row2['firstname']; 
       $ln2 = $row2['lastname']; 
      } 
      $checked2 = ""; 
      if ($course_completed1 == y) { 
       $checked2 = "checked"; 
      } 
      if ($course_completed1 == n) { 
       $checked2 = ""; 
      } 
      echo "<input name='question[$rowid1][]' type='checkbox' checked='$checked2' value='1' />$fn2 $ln2"; 
echo "</br>"; 
    } 
    echo "</form>"; 
?> 

回答

0

像上面提到的那樣,您需要引用yn來比較字符串值。但即使只有checked=""也會導致您的複選框被選中。嘗試使用類似下面的語句:

if ($course_completed1 == "y") { 
    $checked2 = "checked='checked'"; 
} 
elseif ($course_completed1 == "n") { 
    $checked2 = ""; 
} 

echo "<input name='question[$rowid1][]' type='checkbox' $checked2 value='1' />$fn2 $ln2"; 
2

字符串使用引號:

if ($course_completed1 == "y") { 
    $checked2 = "checked"; 
} 
if ($course_completed1 == "n") { 
    $checked2 = ""; 
} 
1

checked='$checked2'應該是純$checked2,它是一個布爾屬性。

(除非你正在寫的XHTML在這種情況下,您測試if ($course_completed1 == "y") {後,也應給予價值​​3210)。

您的測試還應該檢查字符串("y")而不是常量(y)。