in reply to Q & A

Ok, Sorry all for the vagueness. Basically, I need to figure out how the best way to store the data after I retrieve it from the database. Im using DBI and CGI to display the data. After fetching the data, I don't know wheter to store it in an array, hash, etc. and how to do it the best way. After that, I need to display the questions in a list as links and when clicked on will display the corresponding answer.

Replies are listed 'Best First'.
Re: Re: Q & A
by $code or die (Deacon) on Dec 15, 2000 at 00:48 UTC
    I would store them as a hash. Possibly a nested data structure if I needed more info. This would be good:
    %q_and_a = ( '105' => 'How do I use Netscape', '111' => 'How do I listen to MP3s', '119' => 'How can I send email with attachments' );
    You can cycle through each question like this:
    foreach (keys(%q_and_a)) { print "Q: <a href=answers.cgi?q=$_>$q_and_a{$_}</a>\n"; }
    This is a simple hash, and the keys are the unique identifiers for each question in the database. So when someone clicks on the link, your script would capture the id number and then pull out the answer and display it.

    Sample output:
    Q: <a href=answers.cgi?q=119>How can I send email with attachments</a> Q: <a href=answers.cgi?q=111>How do I listen to MP3s</a> Q: <a href=answers.cgi?q=105>How do I use Netscape</a>
Re: Re: Q & A
by boo_radley (Parson) on Dec 15, 2000 at 00:40 UTC
    If you're retrieving the data with DBI, you should be able to iterate through doing something like :
    while ( @row = $sth->fetchrow_array ) { print "<A HREF=$row[1]>$row[0]</A><P>\n"; }
    This snippet assumes you've already retrieved the Q&As, and have the Qs in row 0, and the link to the A in row 1. See also DBI's pod.