Re: [htmltmpl] Select/option How to set "selected"?
Brought to you by:
samtregar
From: Jason P. <ja...@jo...> - 2004-05-04 02:38:00
|
> That said, my philosophy is to dump as much work on the database as > possible. Why? -- because db programs have been developed over years and > years (not that Perl has not been, but your or my code may not have the > same advantage), db servers are typically fast machines, db software > have been honed to do set processing, evaluations, calculations, etc. > There is a lot of science behind db theory. Definitely use it to your > advantage. I definitely agree here -- I didn't think about using SQL to do the selected part ... kinda neat. :) What's really neat if you do it like that is you can use DBI's fetchall_arrayref method and be really snazzy: my $template = HTML::Template->new( 'filename' => 'file.TMPL' ); my $sth = $dbh->prepare( <<_SQL_ ); SELECT val, text, (if then else to return 'selected' or '') AS selected FROM table WHERE whatever... _SQL_ $sth->execute(); $template->param( 'OCCUPATION_LOOP' => $sth->fetchall_arrayref( {} ) ); ## Note that the fetchall_arrayref doesn't save you computing time -- ## it's only a shortcut and does the same thing as your loop to go ## through the results --- What's neat about this, too is that it simplifies the template code, as Puneet laid out. Cheers, Jason |