I'm working on a Mason, Mysql inventory application. For any item, whose information may be in one or more tables, the user will have an option to view, edit or add a new item. I want avoid having three fully separate pieces of code that each fetch and then display. I think it would be easier to share some common code.

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:

<%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>
Edit or add data:
<%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 &>
Get data from database
%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


In reply to Reusing code for Mysql and Mason Application by neilwatson

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.