Re: [htmltmpl] "dump"-ing a loop
Brought to you by:
samtregar
From: Puneet K. <pk...@ei...> - 2003-08-12 15:16:53
|
ok... that set me thinking, and I found the solution. Here is a complete example using XBase, so that some other poor sod in the future might quickly get going instead of fretting... In the script -- my $dbftable = new XBase $table or die XBase->errstr; # get the fieldname my @fns; for ($dbftable->field_names) { my %f = ('fn' => "$_"); push(@fns, \%f); } $template->param(fns => \@fns); # get the first ten rows my @res; for my $row (0..9) { my @col; for (0..$dbftable->last_field) { my @c = $dbftable->get_record_nf($row, $_); my %c = ('cv' => "$c[1]"); push(@col, \%c); } my %r = ('rv' => \@col); push(@res, \%r); } $template->param(res => \@res); And then in the template -- <table border="1"> <tr><tmpl_loop fns><th><tmpl_var fn></th></tmpl_loop></tr> <tmpl_loop res> <tr><tmpl_loop rv><td><tmpl_var cv></td></tmpl_loop></tr> </tmpl_loop> </table> done. Hope this helps someone in the future. On Tuesday, August 12, 2003, at 07:53 AM, Joel wrote: > Morning folks, > > I've done this before, and it's a bit tricky, but it's certainly > possible. > > What you need is a pair of nested loops. One to iterate through the > rows of > your data and one to iterate through the columns of each row. Your > template > would look like the following: (let's pretend like we are putting > everything > into a table) > > <table> > <TMPL_LOOP NAME=ROWS_LOOP> > <tr> > <TMPL_LOOP NAME=COLS_LOOP> > <td><TMPL_VAR NAME=VALUE></td> > </TMPL_LOOP> > </tr> > </TMPL_LOOP> > </table> > > Now it's just a matter of building your array of hashrefs to send to > HTML > template, which I'll leave as homework. :) > > If anyone can come up with a better solution, I'm open to it. > > --Joel > >> >> On Tuesday, August 12, 2003, at 12:42 AM, Kenneth Gonsalves wrote: >> >>> On Tuesday 12 August 2003 09:45, you wrote: >>> >>>> did a few "feeble" searches in the archive, but I really don't know >>>> how >>>> to word this exactly... >>>> >>>> I want to query a database table, and then print the results. The >>>> problem is, I don't know what the names of the fields are... >>>> >>>> so, while I can build an array of hash refs from the returned >>>> results, >>>> and assign them to a tmpl_loop, I don't know the names of the >>>> tmpl_vars... so in my template, I don't really know what to print >>>> out. >>> >>> you have an anonymous array/hash. you dont need to know the names of >>> the >>> variables. assign something like: >>> $var1 = @$return[0]. $var2 = @$return[1] etc (do this in the code and >>> not in the template) >>> kg >>> >> >> won't really work, I think. See, I query the database table like so (I >> have to, not that I want to)... >> >> SELECT * FROM mytable WHERE somecondition >> >> so in advance I have no idea how many fields are returned, and what >> their names are. While I can find that out in my code using the >> capabilities of my database driver module, I can't really rewrite my >> template on the fly (unless that is one way of doing it). Hence, I am >> stuck... >> >> |