[dancer-users] list order from a hash
Attila Bárdi
attila.bardi at gmail.com
Wed Aug 26 17:38:18 BST 2015
Hey,
use the manual Luke!:)
http://www.template-toolkit.org/docs/manual/Directives.html <http://www.template-toolkit.org/docs/manual/Directives.html>
Search for sort.
<% FOREACH ID IN Jobs.keys.sort %>
<% FOREACH ID IN Jobs.keys.nsort %>
sorted alphabetically ( sort ) or numerically ( nsort )
Best regards,
Attila
> On 26 Aug 2015, at 15:51, Richard Reina <gatorreina at gmail.com> wrote:
>
> I have this table data that I load into a template as follows.
>
> $q = "SELECT ID, NAME
> FROM jobs
> ORDER BY NAME";
> $sth = $dbh->prepare($q);
> $sth->execute();
> my $Jobs_ref = $sth->fetchall_hashref('ID');
>
> template 'sport_add.tt <http://sport_add.tt/>', {
>
> 'Jobs' => $Jobs_ref,
> 'Testing' => "This is text to test to make sure this will come through",
> # to test, use this like <% Testing %> on your template page
>
> }, {};
>
> To display it in the template as a drop-down select I do:
>
> <select class="selectpicker" name="jobselect" id="jobselect" style="width: 500px;">
> <% FOREACH ID IN Jobs.keys %>
> <option value="<% Jobs.$ID.ID <http://id.id/> %>"><% Jobs.$ID.NAME <http://id.name/> %></option>
> <% END %>
>
> And it works great. Except for one thing, the list is not in order by name. I have read more than a few posts about how once you save a query as a hashref that the order is lost. Most of those posts suggested fetching as a arrayref instead. However, I have no idea how to do that and then display it in a select like the above code does. So I wanted to see if somebody perhaps knows of a not too complicated of a solution to get the select list into alphabetical order by name.
>
> Thanks
> _______________________________________________
> dancer-users mailing list
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20150826/95dfd156/attachment.html>
More information about the dancer-users
mailing list