I have a table attendance which contains id(int), attendance(char), and date(date datatype) columns. Values get stored in this table through a php file. In the below code I have given two textbox for the user to select date range. eg. from 2012-10-15 to 2012-10-17.. when the user selects date range I am populating the whole class students attendance. So in my html table im populating name, roll no, attendance and date. But I do not know how to populate the date in different columns. suppose the user selects 15th october to 17th october as mentioned above, the attendance for these three dates should appear. I have tried something but I guess it's wrong.
$report = mysql_query("SELECT id, studname, studroll, attendance, date FROM student a, samp b WHERE a.id = b.stud_id AND b.date BETWEEN '".$_POST['from']."' AND '".$_POST['to']."'") or die(mysql_error());
echo '<form action="" method="post">
Please select date :<input name="from" id="from" type="text" />
Please select date :<input name="to" id="to" type="text" />
<table width="600" border="2">
<tr>
<th width="83" scope="col">ID</th>
<th width="83" scope="col">Student Name</th>
<th width="55" scope="col">Student Roll.No</th>
<th width="51" scope="col">Attendance</th>
</tr>';
while(list($id, $studname, $studroll, $attendance, $date) = mysql_fetch_row($report))
{
echo '<tr>
<td>'.$id.'</td>
<td>'.$studname.'</td>
<td>'.$studroll.'</td>
<td>'.$date.'</td>
</tr>';
}
echo '</table><input type="submit" name ="submit" id="submit2" value ="submit"></input>
</form>';
Student is my master table from which Iam fetching student name, student rollno.. and from table samp, iam fetching attendance and the date on which attendance was taken.. Is there any mistake in my table design. Kindly guide..
Easily doable, its all about control.
I'm going out in a few minutes so I don't have the time to right this currently; I'll look at it when I get back. What version of PHP are you currently using (major and minor)?
Easily doable, its all about control.
I'm going out in a few minutes so I don't have the time to right this currently; I'll look at it when I get back. What version of PHP are you currently using (major and minor)?
A dumb question.. as my I am new to PHP. What is major and minor version? I am currently using 5.3.8
That's what I needed to know: MAJOR.MINOR.REV, so 5.3 is what I needed to know. That means you can use the DateInterval and DatePeriod which will be handy in looping. Although you can do this from just the while loop for the fetch, I personally think that would get rather complex rather quickly. So I'd suggest dumping everything to a controlled array first, then iterating it. This way you don't even need to order the records if you don't want (although I'd still order by id then date).
PHP Code:
$aRecords = array();
while(list($id, $studname, $studroll, $attendance, $date) = mysql_fetch_row($report))
{
if (!isset($aRecords[$id]))
{
$aRecords[$id] = array(
'id' => $id,
'studname' => $studname,
'studroll' => $studroll,
//'attendance' => $attendance, // I don't know what this is, so I'm leaving it out
'ondate' => array($date)
);
}
else
{
$aRecords[$id]['ondate'][] = $date;
}
}
$dtStart = new DateTime($_POST['from']);
$dtEnd = new DateTime($_POST['to']);
$diDiff = $dtEnd->diff($dtStart, true);
$di = new DateInterval('P1D');
$dp = new DatePeriod($dtStart, $di, $dtEnd);
That's what I needed to know: MAJOR.MINOR.REV, so 5.3 is what I needed to know. That means you can use the DateInterval and DatePeriod which will be handy in looping. Although you can do this from just the while loop for the fetch, I personally think that would get rather complex rather quickly. So I'd suggest dumping everything to a controlled array first, then iterating it. This way you don't even need to order the records if you don't want (although I'd still order by id then date).
PHP Code:
$aRecords = array(); while(list($id, $studname, $studroll, $attendance, $date) = mysql_fetch_row($report)) { if (!isset($aRecords[$id])) { $aRecords[$id] = array( 'id' => $id, 'studname' => $studname, 'studroll' => $studroll, //'attendance' => $attendance, // I don't know what this is, so I'm leaving it out 'ondate' => array($date) ); } else { $aRecords[$id]['ondate'][] = $date; } }
$dtStart = new DateTime($_POST['from']); $dtEnd = new DateTime($_POST['to']); $diDiff = $dtEnd->diff($dtStart, true); $di = new DateInterval('P1D'); $dp = new DatePeriod($dtStart, $di, $dtEnd);
Thanks for this snippet.. I am trying to understand your code.. It worked out exactly as I wanted.. But I am not understanding how to populate the attendance field.. The attendance field is of varchar(1) datatype and it contains 'P' AND 'A'. P stands for present and A stands for absent.. And also I see that you have given difference of the two dates if I am not mistaken in understanding the code. Actually I wanted it to be less than or equal to the 'to date'. eg: if i select from date as 2012-10-15 and to date as 2012-10-17, it populates 15th october and 16th october, but I need all the three dates. ie 15th, 16th and 17th..
I don't understand why you need this field at all. The impression I have of the table is if a record exists, that it is assumed that attendance would exist.
Does a record exist for every date and applied either an A or P in the attendance field for every student? It's a bit of a waste in dataspace IMO to do that, but you can modify it easily as well by changing the array creation in the while:
PHP Code:
if (!isset($aRecords[$id])) { $aRecords[$id] = array( 'id' => $id, 'studname' => $studname, 'studroll' => $studroll, 'attendance' => array($date => $attendance), // I don't know what this is, so I'm leaving it out ); else { $aRecords[$id]['attendance'][$date] = $attendance; }
To that, and modifying the foreach at the end to this:
I don't understand why you need this field at all. The impression I have of the table is if a record exists, that it is assumed that attendance would exist.
Does a record exist for every date and applied either an A or P in the attendance field for every student? It's a bit of a waste in dataspace IMO to do that, but you can modify it easily as well by changing the array creation in the while:
PHP Code:
if (!isset($aRecords[$id]))
{
$aRecords[$id] = array(
'id' => $id,
'studname' => $studname,
'studroll' => $studroll,
'attendance' => array($date => $attendance), // I don't know what this is, so I'm leaving it out
);
else
{
$aRecords[$id]['attendance'][$date] = $attendance;
}
To that, and modifying the foreach at the end to this:
What I am doing is, like I told earlier, I am marking attendance for every student as A or P through a checkbox. So if a checkbox is ticked the student is present and if not ticket the student is absent. And to mark the attendance, I have given a text field where the staff selects the date and this date gets stored in date column of database. Am i doing anything wrong?
echo '<form action="" method="post">
from: <input type="text" id="from" name="from"/>
to: <input type="text" id="to" name="to"/>
<input type="submit" name="submit" value="submit"/>
</form>';
$aRecords = array();
$report = mysql_query("SELECT id, studname, studroll, attendance, date FROM student a, samp b WHERE a.id=b.stud_id AND date BETWEEN '".$_POST['from']."' AND '".$_POST['to']."'");
while(list($id, $studname, $studroll, $attendance, $date) = mysql_fetch_row($report))
{
if (!isset($aRecords[$id]))
{
$aRecords[$id] = array(
'id' => $id,
'studname' => $studname,
'studroll' => $studroll,
'attendance' => array($date => $attendance), // I don't know what this is, so I'm leaving it out
Your code works as I wanted. But the last date ie the to date does not appear on the row. I have attached the screenshot. I have selected date ranges from 15th to 18th.
$dtEnd must be exclusive on dateperiod.
Add this directly before the datePeriod creation: $dtEnd->add($di);.
As for your data, I myself would probably track the absentee. Assuming that there is specific guidelines to follow that dictate when a day should be in attendance, then recording only those absent would reduce the amount of dataspace required. This of course assumes that on average the majority of the student population is present and not absent.
$dtEnd must be exclusive on dateperiod.
Add this directly before the datePeriod creation: $dtEnd->add($di);.
As for your data, I myself would probably track the absentee. Assuming that there is specific guidelines to follow that dictate when a day should be in attendance, then recording only those absent would reduce the amount of dataspace required. This of course assumes that on average the majority of the student population is present and not absent.
Ok Fou-Lu I tried it out but facing some problems still. Will try my best to rectify it else will come back