Problem: Date fields in Access are returned as dd/mm/yyyy hh:mi:ss and printed as such on my HTML output.
Reasons for problem: I have a generic function that parses db output and converts into an arrayref that is passed as arguments to the TMPL_LOOP attribute of HTML::Template. This function does not know about the data types it receives. The way the SELECT query is framed takes care that I dont have to do individual field assignments to map attributes in the template to the SELECTed fields. Code attached:
sub get_tmpl_loop_arrayref_bind
#Given an SQL query this will return an arrayref compatible with TMPL_
+LOOP. Works where the query does not contain a , before the 'from' an
+d where there is no subquery.
{
my ($dbh, $sql, @binds, $sessID) = @_;
$dbh->{RaiseError}=0;
$dbh->{PrintError}=0;
$sql=~/select\s+(.*?)from\s+.*/ig;
my $onlyfields = $1;
my @fields = split /,/, $onlyfields;
foreach my $field (@fields)
{
$field=$1 if ($field=~/.*?\s+as\s+\[(.*)\]/ig); #Fields taken with the
+ as identifier are parsed here. Eg. "select a/b as ratio"
$field=~s/\s+//ig;
}
my $sth = $dbh->prepare($sql) || die "Could not prepare query $sql in
+get_tmpl_loop_arrayref";
$sth->execute || die "Could not execute query $sql in get_tmpl_loop_ar
+rayref";
my @loop_data = ();
while (my $hash_op = $sth->fetchrow_hashref('NAME_lc'))
{
my %row_data;
foreach my $field (@fields)
{
$row_data{$field}=$hash_op->{$field}? $hash_op->{$fiel
+d}:'-' ;
}
$row_data{id}=$sessID if $sessID;
push (@loop_data, \%row_data);
}
if ($dbh->errstr()) {
return $dbh->errstr();
}
else
{return \@loop_data;}
}
The SELECT statement would be as follows:
"SELECT username, name, role, email, phone, mobile, creation_date from
+ user where creator='".$session->param('username')."'";
and the HTML:Template would be like so:
<TMPL_LOOP NAME=all_users>
<!-- Data display -->
<TR>
<TD WIDTH="25%" class="tblvlu"><TMPL_VAR NAME=username></TD>
<TD WIDTH="25%" class="tblvlu"><TMPL_VAR NAME=name></TD>
<TD WIDTH="25%" class="tblvlu"><TMPL_VAR NAME=role></TD>
<TD WIDTH="25%" class="tblvlu"><TMPL_VAR NAME=email></TD>
<TD WIDTH="25%" class="tblvlu"><TMPL_VAR NAME=phone></TD>
<TD WIDTH="25%" class="tblvlu"><TMPL_VAR NAME=mobile></TD>
<TD WIDTH="25%" class="tblvlu"><TMPL_VAR NAME=creation_date></TD>
Works like a charm except the display of the creation date which is usually something like
10/06/2005 00:00:00
Regards,
Rajesh
PS: Was that too much detail??