I'll break down what just happened there. I'm using an object called Page (I use it for web pages in general), it has a method called dbselect that will query the database for me (the connection is set up elsewhere). $newst is an HTML::Template that I have genereated, so I call the param method of $newst with the output from dbselect assigned to the TMPL_LOOP news. It then fills in my news page with rows of news. Some day I'll update it to limit the number of stories, but I'm basically boring so I don't have that much news to display :-)my $p = Page->new(); # now we get the info from the database and pass it to the template # in this case, the template needs a reference to an array of hashes # in order to handle the TMPL_LOOP properly. Which is exactly what # dbselect returns. hard to believe :-) $newst->param(news => $p->dbselect($sql,1)); sub dbselect{ my Page $self = shift; my $sql = shift if @_; my $hash = shift if @_; my $db = $self->db(); my $h = $db->prepare($sql) or $self->errorpage("DB problem in sele +ct prepare: " . $db->errstr()); $h->execute() or $self->errorpage("DB problem in select execute: " + . $db->errstr()); my $arrayref; if ($hash){ $arrayref = $h->fetchall_arrayref({}); } else { $arrayref = $h->fetchall_arrayref(); } return $arrayref; }
So you see, by using the AoH with a TMPL_LOOP, I was able to loop through everything with almost no code. Very easy once you understand it.<table border=0 width='98%' cellspacing=3 cellpadding=3> <TMPL_LOOP NAME="news"> <tr><td> <table width='100%' border=1 cellspacing=1 cellpadding=0> <tr> <th align='left'><TMPL_VAR NAME='summary'></th> <th align='right'><TMPL_VAR NAME='pubdate'></th> </tr> <tr> <td align='left' colspan=255><i>By: <TMPL_VAR NAME +='realname'></i></td> </tr> <tr> <td align='left' colspan=255><TMPL_VAR NAME='conte +nt'></td> </tr> </table> </td></tr> </TMPL_LOOP> </table>
In reply to Re: Unable to display data from hash array
by benrwebb
in thread Unable to display data from hash array
by bar10der
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |