CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Re: [Iconx] CGI::Application and MySQL: Edit Log



FishMonger
Veteran / Moderator

Mar 4, 2015, 11:16 AM


Views: 10591
Re: [Iconx] CGI::Application and MySQL

You don't send it to the template from within the while loop. The while loop builds an array of hashes and after the while loop you assign the array to a template var.

Here's an example from one of my retired apps.

Code
my @search_results = search_abook($form{searchfield}, $form{searchvalue}); 

$template->param(results => \@search_results);

print $template->output;

#--------------

sub search_abook {
my ($field, $search_pattern) = @_;

# stripped code that sets up the db connection and sql statement

if ($sth->rows > 0) {
while (my ($owner, $full, $first, $last, $email, $label) = $sth->fetchrow_array) {
my %account = (
owner => $owner,
fullname => $full,
firstname => $first,
lastname => $last,
email => $email,
label => $label,
);
push @accounts, \%account;
}
}
$dbh->disconnect;
return @accounts;
}


Here's the related code in the template file.

Code
    <tmpl_loop name="results"><tr> 
<td><tmpl_var name="owner"></td>
<td><tmpl_var name="fullname"></td>
<td><tmpl_var name="firstname"></td>
<td><tmpl_var name="lastname"></td>
<td><tmpl_var name="email"></td>
<td><tmpl_var name="label"></td>
<tmpl_if NAME="gic">
<td><a href="modify.pl?locate=1&amp;id=<tmpl_var name="owner">">mod</a></td>
<td><a href="delete.pl?locate=1&amp;id=<tmpl_var name="owner">">del</a></td>
</tmpl_if>
</tr>
</tmpl_loop>


I used fetchrow_array but you could use fetchrow_hashref instead which would allow to avoid manually assigning the hash data like I did.


(This post was edited by FishMonger on Mar 4, 2015, 11:18 AM)


Edit Log:
Post edited by FishMonger (Veteran) on Mar 4, 2015, 11:18 AM
Post edited by FishMonger (Veteran) on Mar 4, 2015, 11:18 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives