2013-03-17 51 views
1

我有一些小错误,想知道是否有人可以帮忙!显示复选框数据mysql

我正在为大学创建考勤系统。

这样的场景:

学生成功登录,然后要查看他/她出席一个特别课程。

问题:我希望它给我托运和从MySQL uncheked数据,它目前显示一个空的复选框(当它的意思进行检查)也以它显示大量的重复数据的那一刻,有没有可能我可以限制它,例如每周显示一条记录,它会显示所有数据并复制它。

<?php 

$q3= "SELECT attendance.week_number_id, attendance.week_number, courses.course_id, students.student_id, course_attendance.present, course_attendance.notes 
FROM courses, course_attendance, attendance, students 
WHERE course_attendance.student_id= ".$_SESSION['student_id']." AND course_attendance.course_id= courses.course_id AND course_attendance.week_id= attendance.week_number_id AND courses.course_id='101' 
"; 


$result = mysql_query($q3) or die(mysql_error()); 

echo "<table border='1' align='center'><tr> <th><strong>Week Number</strong></th> <th><strong>Present</strong></th> <th><strong>Notes</strong></th> </tr> "; 
while($row = mysql_fetch_assoc($result)) 
{ 
    extract($row); 
echo 

"</td><td width='200' align='center'>" .$row['week_number']. 
"</td><td width='400' align='center'><input type='checkbox' name='present'" .$row['present']. 
"</td><td width='400' align='center'>" .$row['notes']. 
"</td><tr>"; 
} 
echo "</table>"; 
?> 

注:我成功地连接到数据库,MySQL的启动和运行,我使用的会议,目前它的数据显示为学生,但不显示现有检查或uncheked值,该复选框是空的。

任何人都可以帮忙

+0

$ checked变量在哪里定义? – Anonymous 2013-03-17 17:59:51

+0

对不起,它的$ row ['present'],mySql中的'present'字段是一个int,它定义1表示存在,0表示不存在,我希望它显示所有数据是1或0,但是在复选框中 – 2013-03-17 18:02:33

回答

0

在你的代码中,你没有正确定义要检查的复选框。制作,增加了checked="true"如果 '现在' 字段是1

<?php 

    $q3 = " SELECT attendance.week_number_id, attendance.week_number, courses.course_id, students.student_id, course_attendance.present, course_attendance.notes 
      FROM courses, course_attendance, attendance, students 
      WHERE course_attendance.student_id= ".$_SESSION['student_id']." AND course_attendance.course_id= courses.course_id AND course_attendance.week_id= attendance.week_number_id AND courses.course_id='101'"; 


    $result = mysql_query($q3) or die(mysql_error()); 

    echo " 
     <table border='1' align='center'> 
      <tr> 
       <th><strong>Week Number</strong></th> 
       <th><strong>Present</strong></th> 
       <th><strong>Notes</strong></th> 
      </tr> 
     "; 

    while($row = mysql_fetch_assoc($result)) { 
     $checked = ''; 
     if($row['present'] == 1) { 
      $checked = ' checked="true"'; 
     } 

     echo " 
      <tr> 
       <td width='200' align='center'>" . $row['week_number'] . "</td> 
       <td width='400' align='center'> 
        <input type='checkbox' name='present'" .$checked . "/> 
       </td> 
       <td width='400' align='center'>" . $row['notes'] . "</td> 
      </tr> 
     "; 
    } 

    echo "</table>"; 

?> 
+0

我现在要试试这个代码谢谢你会让你知道它是否有效 – 2013-03-17 18:13:09

+0

现在没有数据显示:S – 2013-03-17 18:19:21

+0

是否有错误? – Anonymous 2013-03-17 18:20:52

0

代码你

echo 
    "</td><td width='200' align='center'>" .$row['week_number']. 
    "</td><td width='400' align='center'><input type='checkbox' name='present'" .$row['present']. 
    "</td><td width='400' align='center'>" .$row['notes']. 
    "</td><tr>"; 

的说法应该是

$present = ""; 
if(.$row['present']==1) 
{ 
    $present = "checked =checked/>";  
} 
else 
{ 
    $present = "/>";  
} 

呼应 “”。$行['WEEK_NUMBER “]。 “ ”“。$ row ['notes']。 “”;

希望这可以帮助你。谢谢

+0

为了不使用更多的行,为什么不制作一小段代码来决定何时只用一段HTML代码添加checked =“checked”? – Anonymous 2013-03-17 18:08:39

+0

是的......也可以这样做......好吧,我在编辑我的答案...... – 2013-03-17 18:09:36

+0

谢谢我现在要试试这个 – 2013-03-17 18:13:30

0
$checked = ''; 
if($present == 1) { 
    $checked = 'checked="checked"'; 
} 

echo "</td><td width='200' align='center'>" .$row['week_number']. 
"</td><td width='400' align='center'><input type='checkbox' name='present'" .$checked . "/>" . 
"</td><td width='400' align='center'>" .$row['notes']. 
"</td><tr>"; 
} 
echo "</table>"; 

尝试。