Hi Grozanc,
I don't like the html form because it doesn't have names and because so many tiny checkmarks can easily be error prone and <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> off a student if you check the wrong one. And you don't say anything about the output that it is going to give you, are you going to have to print out a report every day? I recommend that you start by just having a roster in the database which I will give you the code for now. Then tomorrow or the next day, I will give you the code to mark each student as present or absent as you call their name. It will then have functionality to later change to excused absence or tardy and will also allow you to enter points for homework, quizes, midterms, and the final exam.
First, use phpadmin to make a database called 'students', table called 'names', and two varchar 30 fields called 'Last' and 'First'. Notice the capitalization. I made a user 'teacher' with password 'pencil' and gave that user full permissions.
Your database settings include file is as follows and goes in a file called 'studentsSettings.php' in a folder called 'Teacher' under your htdocs or document root folder:
Code: Select all
<?
$host = 'localhost';
$username = 'teacher';
$password = 'pencil';
$database = 'students';
?>
Your form to enter the roster names is called 'Roster.htm' and goes in the Teacher folder:
Code: Select all
<html>
<body onload="document.forms[0].reset(), document.forms[0].Last.focus()">
<form action="../Teacher/CreateRoster.php" method="post">
<table>
<tr><td>last name:</td><td>first name:</td></tr>
<tr><td><input type="text" size="30" name="Last"></td>
<td><input type="text" size="30" name="First"></td>
<tr></tr>
<tr><td></td><td></td><td></td><td></td><td></td><td align='right'><input type="Submit" value='enter'></td></tr>
</table>
</form>
</body>
</html>
Your script to place your entries into the database is called 'CreateRoster.php' and goes in the Teacher folder:
Code: Select all
<?
//open the database
include('../Teacher/studentsSettings.php');
$db = mysql_connect(localhost,$username,$password)
or
die("could not connect");
mysql_select_db($database) or die( "Unable to select database");
//take the form variables out of the post array and put into non-array variables
$last = $_POST['Last'];
$first = $_POST['First'];
//take the regular variables and put them into the corresponding table field names prior to inserting into SQL
$sql = "INSERT INTO names SET
Last= '$last',
First='$first'";
//insert posted entries into database table
$inserted = mysql_query($sql, $db) or die(mysql_error());
//print a heading for the output page
echo ("<b><center><font color='#330099' size='4'>Student Attendance<br>");
//output the table of entries
print("<table border='5' width = '100%' cellspacing='2' cellpadding='2'>");
print("<tr><th> Last Name </th> <th> First Name </th></tr>");
//order by last name ascending
$query = "SELECT * FROM names ORDER BY Last";
//take everything from the database which is in the form of a mysql array and it will go into a php array variable called result
$result = mysql_query($query) or die("Couldn't execute query");
//while there are rows in the array do the following:
while ( $row = mysql_fetch_array($result) ) {
//put the array variables back into regular variables
$last= $row["Last"];
$first= $row["First"];
//print what you got before looping to the while statement for the next row
print("<tr><td width='50%'>$last </td>
<td width='50%'>$first </td></tr>");
}
//close the connection
mysql_close();
//close the table and the rest of the html but include a link to get back to data entry
echo ('<body onload="document.forms[0].submit.focus()">');
echo ('<form action="Roster.htm" method="post">');
echo ("<INPUT TYPE=submit NAME=submit VALUE= 'Back to input form'></form>");
print("</table>");
print("</html>");
?>
Try it out if you already have your class roster, otherwise I am using the roster of the Baltimore Ravens to test it.