Monks good day! I have a coding problem... and I need your advice.
I am using HTML::Template module. In my CGI script, I am calling a subloop inside the main loop. I wanted an output that's something like this:
Topic 1
- Item A
- Item B
- Item C
Topic 2
- Item X
- Item Y
- Item Z
Topic 3
- Item a
- Item b
- Item c
but instead, my code resulted to:
Topic 1
- Item a
- Item b
- Item c
Topic 2
- Item a
- Item b
- Item c
Topic 3
- Item a
- Item b
- Item c
Here's a part of my code:
$y = 0; @loop = ();
while($y < $x) {
my %items;
$items{_TOPIC} = $topic[$y];
$sth = $dbh->prepare("SELECT item1, item2, item3 FROM table WHERE
+topic='$topic[$y]' ORDER BY id ASC");
$sth->execute();
@subloop = ();
while(@ary = $sth->fetchrow_array()) {
my %subitems;
$subitems{_first_item} = $ary[0];
$subitems{_second_item} = $ary[1];
$subitems{_third_item} = $ary[2];
push(@subloop, \%subitems);
}
$items{_ITEMS} = \@subloop;
push(@loop, \%items);
$y++;
}
$html->param(_TOPICS => \@loop);
$html->output;
Can you help me point out the error?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.