I'm not overly familiar with PostgresSQL but wouldn't you do the sorting inside your SQL query? like this:
$all_cats = $dbh->selectall_arrayref("SELECT category_id,name FROM $sq +l{catego +ries} WHERE parent=$parent_cat_id ORDER BY category_id");

That's a minor point however ;-).

Now, onto HTML::Template and how you could possibly produce a 'variable' (array actually) that could later be fed to HTML::Template and used reasonably therein.

I know that HTML::Template allows for nested TMPL_LOOP statements, however, I hardly see them being useful in your case. Instead, you might try doing the following. Create an array structure like this:
@categories = ( { id => <i>insert category_id value here</i>, name => <i>insert name value here</i> level => <i>level (inside a category etc.) </i> }, { ... }, ... );
Notice introduction of the 'level' field? You may use this one in your HTML template to determine how much 'indentation' is required. To set 'level' value properly, you might modify your code to increment a $level variable instead of adding string filled with a bunch of 'spaces' to the $indent variable as it's no longer going to be needed. Basically, try to introduce $level variable in place of your $indent and also change this line:
$indent .= "&nbsp; ...";
to
$level++;
and later in your foreach loop, just push this hash onto your @categories array (this array may be global to start with.. however, it does feel yucky and you might have to change it later :)
foreach my $current_cat (sort { $a->[1] cmp $b->[1] } @{$all_c +ats}) { ($cat{id}, $cat{name}) = @$current_cat; push @main::categories {level=>$level,name=>$cat{name},i +d=>$cat{id}}; &ListCategories($indent, $cat{id}, 'y'); }
This way your Template code is left to decide which way to format the output based on the 'level' value ;-).

See if it works for you and let me know. (I didn't test the code, though).

"There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith

In reply to Re: Collecting data in a recursive routine. by vladb
in thread Collecting data in a recursive routine. by Anonymous Monk

Title:
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, details, 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, summary, 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.