View Single Post
Old 10-17-2012, 06:13 PM   PM User | #7
havish
New Coder

 
Join Date: Jul 2012
Posts: 26
Thanks: 1
Thanked 0 Times in 0 Posts
havish is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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($dtStarttrue);
$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);
    foreach(
$dp AS $attDate)
    {
        if (
in_array($attDate$att))
        {
            print(
'<td>x</td>');
        }
        else
        {
            print(
'<td>&nbsp;</td>');
        }
    }
    print(
'</tr>');

Try that out. Works okay in my head.
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..

Last edited by havish; 10-17-2012 at 06:17 PM..
havish is offline   Reply With Quote