PHP Code:
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
);
}
else
{
$aRecords[$id]['attendance'][$date] = $attendance;
}
}
$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);
printf('
<table width="443" border="1">
<tr>
<th rowspan="2" scope="col">Id</th>
<th rowspan="2" scope="col">StudentName</th>
<th rowspan="2" scope="col">StudRoll</th>
<th colspan="%d" scope="col">Attendance</th>
</tr>
<tr>
', $diDiff->d);
foreach ($dp AS $attDate)
{
printf('<td>%s</td>' . PHP_EOL, $attDate->format('d-m-Y'));
}
print('</tr>');
foreach ($aRecords AS $record)
{
$att = array_pop($record);
print('<tr>');
vprintf('<td>%s</td><td>%s</td><td>%s</td>', $record);
printf('<td>%s</td>', implode('</td><td>', $att));
print('</tr>');
}
?>
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.