Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Y'know, it's really, really nice to post actually parsable example code.

You could just seperate the first td call.

# corrected original my ($value1, $value2, $value3) = (1..3); print $cgi->Tr({ -align=>'LEFT', -valign=>'TOP'}, [ td( { -OnClick=>"javascript:dothis()" }, [ $cgi->a( {-href=>"http://example.com", -target=>"_new"}, $cg +i->img('blah.gif')) . $cgi->a( {-href => 'http://example.com' }, $value1 ). +"\n", $cgi->hidden( -name=>'name2', -value=>"$value2" ). $cgi->a( {-href => 'http://example.com' }, $value2 ) . + "\n", $cgi->hidden( -name=>'name3', -value=>"$value3" ). $cgi->a( {-href => 'http://example.com' }, $value3 ) . + "\n", ]), ]); print "\n"; # td call separated print $cgi->Tr({ -align=>'LEFT', -valign=>'TOP'}, td($cgi->a( {-href=>"http://example.com", -target=>"_new"}, $cgi-> +img('blah.gif')) . $cgi->a( {-href => 'http://example.com' }, $value1 )) +. "\n" . td( { -OnClick=>"javascript:dothis()" }, [ $cgi->hidden( -name=>'name2', -value=>"$value2" ). $cgi->a( {-href => 'http://example.com' }, $value2 ) . + "\n", $cgi->hidden( -name=>'name3', -value=>"$value3" ). $cgi->a( {-href => 'http://example.com' }, $value3 ) . + "\n", ]), );

But since this isn't obviously in a loop, you've got no reason to print your hidden fields within the table; that's just making the code messy. To make things a little saner, store things in advance in a list (for order) and a hash (for name-value association) and built it with a loop.

my @names = (qw(name1 name2 name3)); my %cells = (name1 => {href => 'http://example.com/1', value => $value +1}, name2 => {href => 'http://example.com/2', value => $value +2}, name3 => {href => 'http://example.com/3', value => $value +3}); sub row { my ($names, $cells) = @_; my $first = shift @$names; my @result; push @result, CGI::td(CGI::a({-href=> $cells->{$first}{href}, -targe +t => "_new"}, CGI::img('blah.gif')) . CGI::a({-href=> $cells->{$first}{href}}, $cel +ls->{$first}{value})); for my $name (@$names) { push @result, CGI::td({ -OnClick=>"javascript:dothis()" }, CGI::hidden(-name => '$name', -value => $cel +ls->{$name}{value}) . CGI::a({-href => $cells->{$name}{href}}, $ce +lls->{$name}{value})); } return join '', @result; } print $cgi->Tr({ -align=>'LEFT', -valign=>'TOP'}, row(\@names, \%cells +));

Then chuck that too and start using a template module.


In reply to Re: Creating a table using cgi.pm - a unique problem by Zed_Lopez
in thread Creating a table using cgi.pm - a unique problem by sara2005

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-24 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found