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

Hello, Fellow monks...

I've tried looking for the answer to this in the Super search, by searching for the above title, but could not find the correct way to do this...

I've asked on a MySQL list, but have not recieved any type of reply. I know this is mostly a MySQL question, but it also has to do with Perl, since it's Perl and CGI.pm handling this. So if you know how I can do this, can you please point me in the right direction.

I have a database with Categories, which I could just do this:
while (my $something = $sth->fetchrow_hashref()) { $option_list .= qq~<option value="~ . $something->{id} . qq~">~ . + $something->{cat_name} . qq~</option>\n~; }
Then $option_list would have my option list. However, I am going to use it for anywhere from 10 to 100 different forms on each page, depending on the setting, and each one will have a different default, so how can I put it in a hash or array, where I can use CGI.pm's popup_menu? Here is what I want to be able to do, something like this:
popup_menu(-name=>"somename1", -values=>[%options], -labels=>[\%options], -default=>$somename1_value);
Something like that. Of course, it would be customized to my scripting style, but that is way I'm trying to pull the data out of the database.

here is what I have thus far:
$sth1 = $dbh->prepare (qq{ SELECT * FROM categories }); $sth1->execute(); %cat_options = (); while (my $oc111 = $sth1->fetchrow_hashref()) { $cat_options{$oc111->{id}} = $oc111->{c_name}; } $sth1->finish(); # And for the popup_menu: popup_menu(-name=>"cid", -values=> [%cat_options], -labels=>{\%cat_options}, -default=>$row->{cid}, -class=>"formfield_left")
That does not work. ;(

I'd really appreciate any advice.

thx,
Richard

Replies are listed 'Best First'.
Re: popup_menu from database
by dws (Chancellor) on Apr 06, 2003 at 04:34 UTC
    I'd really appreciate any advice.

    Here's some high-level advice: Life gets easier when you can break a problem in independent sub-problems. Building a pop-up menu from a database is a great example of a problem that divides into parts that can be developed and tested independently. The only trick in combining the two is deciding on the data structure that communicates the results of the first sub-problem (getting the data out of database) with the second sub-problem (turning it into a pop-up menu).

    Dividing the problem increases the likelihood of getting help. Instead of dumping a large problem on the table, you pose a smaller, more focused one. In this case, "How do I fetch a set of rows from MySQL and turn them into (some data structure)?"

    Or, if your question is "what datastructure is best if I want to create a pop-up menu?", ask that. Notice how MySQL drops away entirely.

    Asking focused questions improves your chances of getting focused answers.

      This question comes from the good advise of dws...

      How do I fetch a set of rows from MySQL and turn them into a Perl hash?

      thx,
      Richard
        Here is a snippet to help you on your way:
        while ( my $ref = $sth->fetchrow_hashref() ) { print $q->popup_menu( -name => "some_random_name", -values => [keys %$ref], -labels => $ref, -default => 'default_key_from_ref', ); print $q->br; }

        -enlil