Hi Warren, 

Thanks for the very valuable tips! I didn't understand a lot about what I was 
reading in the examples and tutorials and you have helped clear up a lot of 
things! I'll try out a few things and report back!

-----Original Message-----
From: dancer-users [mailto:dancer-users-boun...@dancer.pm] On Behalf Of Warren 
Young
Sent: Tuesday, 4 July 2017 12:58 AM
To: Perl Dancer users mailing list <dancer-users@dancer.pm>
Subject: Re: [dancer-users] Database template not working (and also my Dancer2 
code)

On Jul 1, 2017, at 8:41 PM, CHONG Yu Meng <yumeng.ch...@invictus.com.sg> wrote:
> 
> subscribers => $sth -> fetchrow_hashref('id’)

Do you really want just one row here?

If so, your SELECT statement should have a WHERE clause that matches only one 
row, and you should be checking for an empty result set.

If you’re actually intending to pass all subscribers to the template, you 
probably want fetchall_arrayref({});  That is, you want a reference to an array 
of hashrefs.

Alternately, since you’re specifying the columns by name in the SELECT 
statement, you could probably get DBI to give you a reference to an array of 
arrayrefs, and then index by column ID, but that’s a bit more brittle in the 
face of future changes.

> <% FOREACH id IN subscribers.keys.nsort %> <tr> <td><% 
> subscribers.$id.displayname %></td> <td><% subscribers.$id.emailaddr 
> %></td> </tr> <% END %>

I don’t use Template Toolkit, so I’m just going on what I can see by Googling 
things, but give the array-of-hashes option above, I think you want something 
more like this:

<% FOREACH s IN subscribers %>
<tr> <td><% s.displayname %></td> <td><% s.emailaddr %></td> </tr> <% END %>

That is, you’re taking $subscribers as an arrayref, getting each subscriber in 
whatever order the DB returns it, and then indexing into each row by column 
name.

If you want a different row ordering, you’d add an ORDER BY clause to the 
SELECT statement.  This is also why I think you probably don’t want 
fetchall_hashref(): ordering for display will be harder than letting the DBMS 
sort for you.
_______________________________________________
dancer-users mailing list
dancer-users@dancer.pm
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
_______________________________________________
dancer-users mailing list
dancer-users@dancer.pm
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users

Reply via email to