For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > February 2006 > Creating a table with HTML:Template









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Creating a table with HTML:Template
Christian Stalp

2006-02-22, 6:55 pm

Hello out there,
i try to create a table with HTML:Template

for this I fill a array with hashes:
while ( $zeile = $result->fetchrow_arrayref )
{
$uebergabe{ name => $row->[0], link => $row->[1] };
push ( @suchprofile, \%uebergabe );
}

and give it to the template-object:

$tmpl->param ( vorname => $vorname,
nachname => $nachname,
suchprofile => \@suchprofile );

On the tempalte i tried this:

</HEAD>

<BODY BGCOLOR="yellow">
<BR>
<HR>
<H3> Herzlich Willkommen <TMPL_VAR NAME="vorname"> <TMPL_VAR
NAME="nachname"> </H3>
<BR>
<TABLE BORDER="1">
<TR>
<TH>Name</TH>
<TH>Link</TH>
</TR>
<TMPL_LOOP NAME="suchprofile">
<TR>
<TD><TMPL_VAR NAME="name"></TD>
<TD><TMPL_VAR NAME="link"></TD>
</TR>
</TMPL_LOOP>
</TABLE>


</BODY>
....
But I got nothing but an empty table. What is wrong?

Thank you.

Gruss Christian

--
Telefonieren Sie schon oder sparen Sie noch?
NEU: GMX Phone_Flat http://www.gmx.net/de/go/telefonie
Paul Lalli

2006-02-22, 6:55 pm

Christian Stalp wrote:

> i try to create a table with HTML:Template
>
> for this I fill a array with hashes:
> while ( $zeile = $result->fetchrow_arrayref )
> {
> $uebergabe{ name => $row->[0], link => $row->[1] };


What exactly do you think this line is doing?

Are you coding with strict and warnings enabled? If not, start.

I *think* you're trying to set to elements of a hash called %uebergabe.
If that's correct, the above syntax is massively not correct.

my %uebergabe = ( name =>$row->[0], link => $row->[1] );

> push ( @suchprofile, \%uebergabe );


With the above change, this line would make sense.

However, why go through this mess, when the DBI functions will give you
your hash just the way you want it anyway? Instead of fetching the
rows as an array, fetch them as a hash:

while (my $uebergabe_ref = $result->fetchrow_hashref){
push @suchprofile, $uebergabe_ref;
}

Or maybe even use one of the fetchall_* methods to get your array in
just one call, instead of a loop...

Paul Lalli

David Kaufman

2006-02-24, 3:55 am

Hi Christian,

"Christian Stalp" <christian.stalp@gmx.de> wrote:
> i try to create a table with HTML:Template
> for this I fill a array with hashes:
> while ( $zeile = $result->fetchrow_arrayref ) {
> $uebergabe{ name => $row->[0], link => $row->[1] };
> push ( @suchprofile, \%uebergabe );
> }
>
> But I got nothing but an empty table. What is wrong?


try adding the following to the top of your script:

use strict;
use warnings;

The uninitialized variable warnings that you see will alert you that, in
your while statement, you assigned each row's hash reference to a
variable named $zeile, but then attempted to retrieve the values from a
hashref named $row.

while ( my $row = $result->fetchrow_arrayref ) {
$uebergabe{ name => $row->[0], link => $row->[1] };
push ( @suchprofile, \%uebergabe );
}

should work much better for you.

hth,

-dave


Christian Stalp

2006-02-25, 7:55 am

Hi David,
I allready solfe this problem.
> try adding the following to the top of your script:
>
> use strict;
> use warnings;


I allways do ;-)

> The uninitialized variable warnings that you see will alert you that, in
> your while statement, you assigned each row's hash reference to a
> variable named $zeile, but then attempted to retrieve the values from a
> hashref named $row.
>
> while ( my $row = $result->fetchrow_arrayref ) {
> $uebergabe{ name => $row->[0], link => $row->[1] };
> push ( @suchprofile, \%uebergabe );
> }
>
> should work much better for you.
>
> hth,
>
> -dave
>
>
>
> --
> To unsubscribe, e-mail: beginners-cgi-unsubscribe@perl.org
> For additional commands, e-mail: beginners-cgi-help@perl.org
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>


I don't know any more which version I showed you. But now I reallized it
with an array:

while ( $zeile = $result->fetchrow_arrayref )
{
push ( @suchprofile, $zeile->[0] );
}

$result->finish();

# Bediene jetzt Template mit Daten
$tmpl = new HTML::Template::Compiled ( filename => TMPL_FILE );

print $cgi_obj->header ( "text/html" ),
$cgi_obj->start_html ( -title => "Startseite" );


$tmpl->param ( vorname => $vorname,
nachname => $nachname,
suchprofile => \@suchprofile );

print $tmpl->output;

$dbh->disconnect();
exit;

And this works.

Thank you...

Gruss Christian

--
Telefonieren Sie schon oder sparen Sie noch?
NEU: GMX Phone_Flat http://www.gmx.net/de/go/telefonie
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com