[dancer-users] Iteration of hash in template.

John Stoffel john at stoffel.org
Wed Sep 30 22:14:42 BST 2015



Richard> Hello All, I ussually iterate a hash that is the result of
Richard> $rows_ref = $sth->fetchall_hashref('ID'); in my template
Richard> with  <% FOREACH ID Matched_pats.keys.sort %>. However, In
Richard> this case though the template only displays one row because
Richard> each resulting row in the SQL query all have the same ID
Richard> number. Is there any simple change I can make to get the
Richard> results to fully iterate?

What you generally need to do in this case is do multiple queries, or
you need to redo your structure so that you key off something else.  

For example, if each user can have one or more charities, you want to
print out something like:


   User: John
   Charity:   RedCross
	      NPR Station
	      Local Park
	      Big Park across county

   User: Richard
   Charity:        Dancer

   User: Bob

   User: Steve
   Charity:        RedCross


So youre temply would loop over the users, then over the charity field
for each user.  It's the One->Many relationship printing.  

The inefficient way is to do a select for all the users, then a query
per-user for each charity they have.  Or as you've shown, doing a big
join also works.  But when you want to paginate your results, doing a
limit as early as possible might make alot of sense to save crawls
through the DB.

So the template would do something like:

<% FOREACH NAME .... %>
   User: <% name %>
   <% FOREACH Charity NAME->.... %>
      Charity: <% Charity %>
   <% END %>
<% END %>

But my syntax is completly off since I don't know TT off the top of my
head.

John



More information about the dancer-users mailing list