rxguil has asked for the wisdom of the Perl Monks concerning the following question:

Hi All, I have a cgi script that is pulling data out or a database, and in short, the results are a table with each record displayed(e.g. Item Color Quantity). I want to put a link at the end of each record that holds something specific about that record (e.g. Color). Thanks
# while($Data->FetchRow()) # { # %Data = $Data->DataHash(); # print "<tr>"; # foreach $key( keys( %Data )) # { # print "<td bgcolor = #9999CC>$Data{$key} </td>"; # }

Replies are listed 'Best First'.
Re: Extracting Data
by CountZero (Bishop) on Dec 12, 2002 at 07:27 UTC

    What kind of link where you thinking of?

    • To another record
    • another webpage
    • ...?

    Where does the info for the link come from?

    • From the database
    • right out of the record itself
    • is it computed "on the fly"
    • ...?

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      The link would go to another script. The data would come from the record itself. Basically the page loads and pulls records from a database. At the end of each record displayed, I want to have a link that people can click on. For example record 6 is a black hat and record 5 is a red hat. If someone clicks on the black hat I want to use that info not the info for the red hat, if that makes any sense.
        I didn't recognise the bit of your code that fetches records from the database, so I'm going to make a whole heap of assumptions in this answer...take from it what you will:
        use CGI; my $q = CGI->new; my %params = $q->Vars; my %links = ("red" => "redscript", "black" => "blackscript"); my $dbh = DBI->connect("DBD:$database:$sid","$user","$pass") or die "$0 connect " . DBI->errstr; my $sql = "select detail1, detail2 detail3 from detail_table where id = ?"; my $sth = $dbh->prepare($sql) or die "$0 prepare $sql " . $dbh->errstr +; $sth->execute($params{id}) or die "$0 execute $params{id} " . $dbh->er +rstr; print $q->header,$q->start_html,$q->start_table; while (my @row = $sth->fetchrow_array()) { print $q->Tr($q->td([$row[0], $row[1], $q->a({-href=>"/cgi-bin/$links[$row[0]].pl?id= +$params{id}"}, "link to $row[0]") ])); } print $q->end_table,$q->end_html;
        (this code isn't tested for obvious reasons...)

        rdfield

Re: Extracting Data
by hmerrill (Friar) on Dec 12, 2002 at 14:12 UTC
    Assuming hash %Data has key 'color', how about
    while($Data->FetchRow()) { %Data = $Data->DataHash(); print "<tr>"; foreach $key( keys( %Data )) { print qq{ <td bgcolor = #9999CC> <a href="http://some_new_site/some_script.cgi?color=$D +ata{'color'}">$Data{'color'}</a> </td> } ### end print qq }
    When printing html in perl, I like to use either here-documents or qq - if you don't then you have to worry about using quotes *in* your html.

    Beware - this is completely untested, but hopefully it's close. HTH.
      Thank you very much!!! The  $Data{'Color'} is what I needed.