Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I would look at two things:
First, if you are using HTML:Template, your TMPL_VAR tags don't look right... they should have a "name" as well:
<TMPL_VAR name="fname">
Second, it looks like you are doing your assignments the hard way. If you return an AoH (Array of Hashes) from your call to DBI, you can assign that entire array to a loop within your template. For instance:

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; }
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 :-)
Note the call to fetchall_arrayref({}) within dbselect. Read up on the documentation for DBI, in this format it returns AoH.

The template that uses this looks like this:
<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>
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.

In reply to Re: Unable to display data from hash array by benrwebb
in thread Unable to display data from hash array by bar10der

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (4)
    As of 2020-02-20 04:44 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (86 votes). Check out past polls.