Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Custom Loop - help needed

Quote Reply
Custom Loop - help needed
 
hello!

I've got the following code in my script which is working great returning a loop to my template.

The problem is that the loop variable is always positive in the template, even if there are no results in the loop - which means it always displays the first option below which is in my templates no matter if there actually are results in the loop or not. Any idea how I can fix that so that if there are no records this will work?

<%if questions_loop%>
DISPLAY 1111111111111111
<%else%>
DISPLAY 2222222222222222
<%endif%>

Code:
# Create a loop of questions
my $db = $DB->table('questions');
$db->select_options("ORDER BY question_id");
my $sth = $db->select( { poll_id_fk => $poll->{poll_id} } ) || die $GT::SQL::error;
my $hits = $db->hits ;
# loop through the results and create a question loop
my @questions;
if ( $hits ) {
while (my $hash = $sth->fetchrow_hashref) {
push @questions, $hash;
}
}

# Load the poll information
print $IN->header();
print Links::SiteHTML::display('easypoll_edit', { questions_loop => \@questions , %$poll } );
Quote Reply
Re: [ryel01] Custom Loop - help needed In reply to
what about

if ( $hits ) {
while (my $hash = $sth->fetchrow_hashref) {
push @questions, $hash;
}
# Load the poll information
print $IN->header();
print Links::SiteHTML::display('easypoll_edit', { questions_loop => \@questions , %$poll } );

}
else {
print $IN->header();
print Links::SiteHTML::display('easypoll_edit', { %$poll } );

}
Quote Reply
Re: [ryel01] Custom Loop - help needed In reply to
Generally, in the official code, we pass along a second variable containing the number of elements, so the last line would change to:
Code:
print Links::SiteHTML::display('easypoll_edit', { questions_loop => \@questions, questions_loop_count => scalar @questions, %$poll } );

At some point in the future, GT::Template may support something like '<%if questions_loop.count%>' - but currently we use the separate count variable approach.

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com