neilwatson has asked for the wisdom of the Perl Monks concerning the following question:
My idea was to have three separate components. One to view, one to edit, and one that gets the data (Mysql/DBI) that gives it back to the first two components.
Consider these simple, untested, examples:
View data:
Edit or add data:<%perl> # views currency data my $data_structure = $m->comp('/comp/get_currency.mas'); print" <h3>Currencies:</h3> <table cellpadding=2> <tr id=tableheadcolor> <th>Currency</th> <th>Cid</th> </tr>\n"; $row=0; foreach ??? #what type of data structure should I have { $row++; if ($row/3 == int($row/3)){ print "<tr id=shadetable>\n"; }else{ print "<tr>\n"; } print "\t<td>".# data?."</a></td>\n"; print "\t<td>".# data?."</td>\n"; print "</tr>\n"; } print "</table>\n"; </%perl>
Get data from database<%args> $cid => 0 </%args> <%perl> # if a currency to edit was passed to this form then # get the data and place in the form. # otherwise present a blank form my $data_structure = $m->comp('/comp/get_currency', cid => $cid); </%perl> <h3>Make a new currency entry</h3> <form action=comp/insert.mas?form=add_currency method=post> <table cellpadding=2> <tr> <td align=right>Currency:</td> <td><input name=currency value=<& $some_structure &> type=text + size=5></td> </tr> </table> <input type=submit> </form> <& /view_currency.html &>
%perl> # get data requested and return my ($statement, $sth, $ref); $statement = 'SELECT cid, currency FROM currency ORDER BY currency;'; $sth = $dbh->prepare($statement) or die "Couldn't prepare statement: $ +dbh->errstr"; $sth->execute or die "Couldn't execute statement: $dbh->errstr"; while ($ref = $sth->fetchrow_hashref){ # store one or more rows of data in a structure } return $data_structure </%perl>
Am I on the right track? If so, what kind of data structure should I create to pass the table data back and forth?
An even better idea would be to pass the SELECT statement to a generic get_data component that would return the table in some structure. Is that possible?
Neil Watson
watson-wilson.ca
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Reusing code for Mysql and Mason Application
by Arunbear (Prior) on Aug 06, 2004 at 21:30 UTC |