in reply to popup_menu from database

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.

Replies are listed 'Best First'.
Re: Re: popup_menu from database
by powerhouse (Friar) on Apr 06, 2003 at 04:44 UTC
    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

        thank you to everyone, I got it to work. Here is how I did it...
        %cat_options = Admin_Get_Cat_options_hash(); # Here is Admin_Get_Cat_options_hash code: sub Admin_Get_Cat_options_hash { my %options_list; $sth = $dbh->prepare (qq{ SELECT id,c_name FROM categories }); $sth->execute(); while (my ($id,$c_name) = $sth->fetchrow_array()) { $options_list{$id} = $c_name; } $sth->finish(); return(%options_list); }
        That worked.
        Thanks for the idea to have it in a sub, dws

        thx,
        Richard