Hi guys, wonder if any of you could figure out what am doing wrong in regards to arrays & checkboxes with the following script:
the code in question is as follows:
<?php
function edit_st_te_event() {
include("./error.php");
//DB Connection
$db = new DB_Connection;
$db->connect();
$database = $db->get_link_id();
//Check if form has been submitted and update DB if it has been
if (isset($_POST["submitted"]) && $_POST["submitted"] == "Update") {
echo "<h5>FORM HAS BEEN SUBMITTED!</h5>";
$event_id = Utility::request('event_id');
$event = new teaching_event;
$event->load($event_id);
$mod_code = $event->get_module_code();
$class_type = $event->get_class_type();
//The form was submitted, now check $_POST['check_1']
if (!(empty($_POST['check_1']))) {
echo "<h5>CHECK_1 IS NOT EMPTY!</h5>";
foreach ($_POST['check_1'] AS $id) {
$query = "UPDATE st_te_event SET";
for ($i = 1; $i <= 12; $i++) {
$val = $_POST["check_$i"];
if (!$val) {
$val = 0;
} //close if
$query.= " Week$i = $val,";
} //close for
$query = substr($query, 0, -1); // Trim trailing comma
$query.= " WHERE student_id = '$id' AND module_code = '$mod_code' AND class_type = '$class_type'";
echo "Update query is : <p>$query</p>";
if (!mysql_query ($query,$database)) {
echo mysql_error();
} //close if
} //close foreach
} //close if
} //close if
$event_id = Utility::request('event_id');
$event = new teaching_event;
$event->load($event_id);
//capture form variables
$year = $event->get_year();
$group_no = $event->get_group_no();
$mod_code = $event->get_module_code();
$class_type = $event->get_class_type();
?>
<!---------------------Display the Table-------------------->
<table border=1 cellspacing="2" cellpadding="4" width="100%" style="background-color:#EEE">
<tr><th>Year</th><th>Group</th><th>Module</th><th>Class</th><th>Student ID</th><th>Name</th>
<?php
for ($i=1; $i <= 12; $i++) {
echo "<th>Wk{$i}</th>";
} //close for
echo "<th>Total</th>";
echo "</tr>";
//Load all of the student records from the st_te_event table
$st_te_event = new st_te_event;
$teaching_events = st_te_event::get_selected($year, $group_no, $mod_code, $class_type);
$num = count($teaching_events);
if ($num == 0) {
echo "<center><h5><font color='#000099'>The selected teaching event has not yet been setup!</font></h5></center>";
echo "<META HTTP-EQUIV='refresh' content='3;URL=./index.php?mode=view§ion=new_teaching_event'>";
} //close if
else {
for ($count = 0; $count < $num; $count++) {
$record = $teaching_events[$count];
$year = $record->get_year();
$group_no = $record->get_group_no();
$mod = $record->get_module_code();
$class = $record->get_class_type();
$id = $record->get_student_id();
$name = $record->get_name();
echo "<tr>";
echo "<form name='form1' method='post' action='./index.php?mode=view§ion=edit_st_te_event'>";
echo "<td>$year</td>";
echo "<td>$group_no</td>";
echo "<td>$mod</td>";
echo "<td>$class</td>";
echo "<td>$id</td>";
echo "<td>$name</td>";
$total = 0;
$percent = 0;
//Create the 12 checkboxes for each week of the student's attendance'
for ($i = 1; $i <= 12; $i++) {
$str1 = "get_week";
$str2 = "$i";
$str3 = "()";
$exp = $str1.$str2.$str3;
$val = $record->$exp;
echo "<td>";
$s1 = "check_";
$s2 = "$i";
$s3 = "[]";
$check = $s1.$s2.$s3;
echo "<input type='checkbox' name='$check' value='$id'";
if ($val) {
echo " CHECKED";
echo ">";
} //close if
echo "</td>";
//increment total
if ($val) {
$total++;
} //close if
} //close for
//Caclulate total as percentage
$percent = round(($total / 12) * 100);
echo "<td>$percent%</td>";
echo "<input type='hidden' name='event_id' value='$event_id'>";
echo "</tr>";
//Insert total as percentage to database
$sql = "UPDATE st_te_event SET Total = '$percent' WHERE student_id = '$id' AND module_code = '$mod_code' AND class_type = '$class_type'";
$sql_result = mysql_query($sql, $database) or die (mysql_error());
unset($record);
} //close for
echo "
<tr>
<td>
<input type='submit' name='submitted' value='Update'>
</td>
</tr>
</form>";
mysql_close($database);
?>
</table>
<br>
<?php
} //close else
} //close edit_st_te_event
Basically what I want to be able to do is check the checkboxs for each students attendance, and update each record in the st_te_event table just by hitting the one "Update" button at the bottom of the page.
The code above uses an array of checkboxes which record the student ID, however how can I record the student ID as well as their 12 weeks attendance records?
to see what I am trying to achieve with the script above goto HERE! and for login use:
user - ocuk
pass - ocuk
Once logged in if you goto the heading "Edit/Add Lecture" on the left hand nav bar and under that click "Edit Lecture" this will display a test lecture which ive setup, click on "Click to edit student attendances" and it will load the script I am having problems with above.
What I want to achieve is a way to check whether a student attended a particular weeks lecture and do this for all students on the table then hit the one update button at the bottom to update the database.
I would appreciate any help or advice any of you can give me on this, at the moment when I select the checkboxes for several students then hit "Update" I get the following error:
Update query is :
UPDATE st_te_event SET Week1 = Array, Week2 = Array, Week3 = Array, Week4 = 0, Week5 = 0, Week6 = 0, Week7 = 0, Week8 = 0, Week9 = 0, Week10 = 0, Week11 = 0, Week12 = 0 WHERE student_id = '10027700' AND module_code = 'ACF504J1' AND class_type = 'Lecture (2 hour)'Unknown column 'Array' in 'field list'
cheers!