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

I Pull info from a database (SQL). And I use this to create cells for each row of data.
while (@row = $sth->fetchrow()){ ## Make our HTML look better if no data. $oid = "&nbsp;" if($oid eq ""); $date = "&nbsp;" if($date eq ""); $username = "&nbsp;" if($username eq ""); print <<HTML; <TR BGCOLOR="#FFFFFF"> <TD><FONT SIZE=2 FACE=ARIAL>$row[0]</FONT></TD> <TD><FONT SIZE=2 FACE=ARIAL>$row[1]</FONT></TD> <TD><FONT SIZE=2 FACE=ARIAL><B>$row[2]</B></FONT></TD> <TD><FONT SIZE=1 FACE=ARIAL> [<A HREF="out_form.cgi?$row[0]">Clock Out</A>] </FONT></TD> </TR> HTML } # End of while.

What i want to do is replace this with a Form. Input that row into a dropdown <select><option> where each <option> is the row of data. Any clues? More details needed? Sounds easy but I tried replacing the code with:
<FORM action=""><select size=10> HTML while (@row = $sth->fetchrow()){ ## Make our HTML look better if no data. $oid = "&nbsp;" if($oid eq ""); $date = "&nbsp;" if($date eq ""); $username = "&nbsp;" if($username eq ""); print <<HTML; <OPTION>$row[0] $row[1] $row[2]</option> HTML } # End of while.

That didn't seem to work. My only conclusion is that i don't use (@row = $sth->fetchrow()

Replies are listed 'Best First'.
Re: Trying to add objects in dropdown
by andreychek (Parson) on Oct 05, 2001 at 22:01 UTC
    Well, there's a few things I noticed that are missing from your example, which may or may not be present in your actual code. And I hate to assume :-)

    Are there end tags for your SELECT and FORM tags after your while loop that you have in there?

    As for things that may certainly be breaking it -- the OPTION tags within your SELECT statement do not require end tags.. and I'm not sure, but end tags may actually break them. Unless thats part of the new XHTML thing...

    Additionally, you may find that not printing out HTML code by hand is a good thing. While perhaps a few small lines may be acceptable, you appear to be using enough HTML to warrant using CGI, which can assist you with any of those HTML coding issues anyway. However, I sometimes find that certain projects are better off using an HTML Templating scheme such as HTML::Template and Template::Toolkit.

    Lastly, you may want to consider trying fetchrow_arrayref(), which won't yank all the results into a single array. You can then use it like this:
    while (my $row = $sth->fetchrow_arrayref()) { ## Make our HTML look better if no data. $oid = "&nbsp;" if($oid eq ""); $date = "&nbsp;" if($date eq ""); $username = "&nbsp;" if($username eq ""); print <<HTML; <OPTION>$row->[0] $row->[1] $row->[2] HTML }
    This example, of course, isn't incorporating CGI, which would again probably be a good idea. And you did define earlier in your code, using "my", your $oid, $date, and $username variables, right? :-)

    But if none of that does the trick, perhaps you could be more specific about how things aren't working for you, and what it is that is happening. Good luck!
    -Eric


    Updated various stuff
Re: Trying to add objects in dropdown
by perrin (Chancellor) on Oct 05, 2001 at 21:59 UTC
    This reminds me of a joke. A scientist is conducting tests on some frogs. He removes a frog's left leg, shouts "jump!", and the frog jumps to the left. He writes down "remove frog's left leg, frog jumps left." He removes a frog's right leg, shouts "jump!", and the frog jumps to the right. He writes down "remove frog's right leg, frog jumps to the right." He removes both legs on a frog, shouts "jump!", and the frog doesn't jump. He writes down "Remove both legs, frog goes deaf."

    Your form isn't working because your HTML is incorrect.

    UPDATE: Oh, never mind, I misread what you were trying to do. Still kind of a funny joke though.

      Very good joke. I needed that. Thanks :)
      I'll look at my HTML. I'm pretty sure it all checked out ok
Re: Trying to add objects in dropdown
by higle (Chaplain) on Oct 05, 2001 at 21:57 UTC