?
Solved

Formatting tables using CGI.pm - html shortcuts

Posted on 2005-04-07
6
Medium Priority
?
286 Views
Last Modified: 2013-12-25
I am trying to create a table in Perl/CGI using the CGI.pm html shortcuts.  

The problem is that I cannot work out how to control each column with CSS using this method.

For example, if I wanted the left column to be one colour and the other column another colour.  

I have included a part of the script that I have so far:

sub showNewCustomerPage {
      my $self = shift;
      
      my $q = $self->query();
      my $content = "";
      $content .= $q->p("<br><center><h3>New Customer</h3></center>");
      $content .= $q->start_form();
      $content .= $q->table({-border=>0,-cellpadding=>3,-cellspacing=>2,-align=>'center'},
            $q->Tr([
                  $q->td(["Company",
                        $q->textfield(-name => 'company',-size => '40')]),
                  $q->td(["First Name",
                        $q->textfield(-name => 'first',-size => '40')]),
                  $q->td(["Last Name",
                        $q->textfield(-name => 'last',-size => '40')]),
                  $q->td(["Title",
                        $q->textfield(-name => 'title',-size => '25')]),
                  $q->td(["Phone"])
                        $q->textfield(-name => 'phone',-size => '25')]),
                  $q->td(["Phone 2",
                        $q->textfield(-name => 'phone2',-size => '25')]),
                  $q->td(["Fax",
                        $q->textfield(-name => 'fax',-size => '25')]),
                  $q->td(["Mobile",
                        $q->textfield(-name => 'mobile',-size => '25')]),
                  $q->td(["Email",
                        $q->textfield(-name => 'email',-size => '40')]),
                  $q->td(["&nbsp;",
                        $q->submit(-value => 'Add New Customer')]),
            ]));
      $content .= $q->end_form();
      return $content;
}

Can you please advise.   Thanks.
0
Comment
Question by:falthorn
  • 4
  • 2
6 Comments
 
LVL 18

Expert Comment

by:kandura
ID: 13727293
I think you can simply add a style attribute like so:

    $q->td({ -style => 'border:solid 1px black'} , ...)
0
 

Author Comment

by:falthorn
ID: 13727363
I have tried that, but what that seems to do is alter the whole row within the table.

I was wondering how you could insert a unique CSS class to each row.

For example, doing standard html coding I would do the following:

<table border="0" cellpadding="3" cellspacing="2">
<tr>
  <td class="left">Name</td><td class="right">Fred Bloogs</td>
</tr>
...



0
 
LVL 18

Expert Comment

by:kandura
ID: 13727545
   use CGI qw/:standard/;
    print Tr( td({-class=>'left'}, 'Blah!'), td({-class=>'right'}, 'Bazz'));
   
prints

    <tr><td class="left">Blah!</td> <td class="right">Bazz</td></tr>


Your use of arrayrefs in the Tr creates a new tr for every element. It doesn't look you can distribute and specify at the same time, unfortunately, so you'll have to specify each TR explicitly.

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 18

Expert Comment

by:kandura
ID: 13727621
In other words, you'll have to write it like this:

          $q->Tr(
               $q->td({ -class=>'left'},  "Company"),
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'company',-size => '40')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},   "First Name"),
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'first',-size => '40')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "Last Name"),
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'last',-size => '40')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "Title"),
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'title',-size => '25')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "Phone"),
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'phone',-size => '25')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "Phone 2"),
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'phone2',-size => '25')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "Fax",
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'fax',-size => '25')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "Mobile",
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'mobile',-size => '25')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "Email",
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'email',-size => '40')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "&nbsp;",
               $q->td({ -class=>'right'},
                    $q->submit(-value => 'Add New Customer')),
            );
0
 
LVL 18

Accepted Solution

by:
kandura earned 500 total points
ID: 13727657
oops, missed a couple of closing braces:

          $q->Tr(
               $q->td({ -class=>'left'},  "Company"),
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'company',-size => '40')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},   "First Name"),
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'first',-size => '40')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "Last Name"),
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'last',-size => '40')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "Title"),
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'title',-size => '25')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "Phone"),
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'phone',-size => '25')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "Phone 2"),
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'phone2',-size => '25')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "Fax"),
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'fax',-size => '25')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "Mobile"),
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'mobile',-size => '25')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "Email"),
               $q->td({ -class=>'right'},
                    $q->textfield(-name => 'email',-size => '40')),
            ),
          $q->Tr(
               $q->td({ -class=>'left'},  "&nbsp;"),
               $q->td({ -class=>'right'},
                    $q->submit(-value => 'Add New Customer')),
            );
0
 

Author Comment

by:falthorn
ID: 13727817
You've done it again Kandura.

It worked.

Thanks a million.  
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses
Course of the Month16 days, 20 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question