Forum Moderators: coopster

Message Too Old, No Replies

PHP Newbie.

Displaying MySQL Row in form select

         

web_young

4:16 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



I'm just beginning PHP and I have a question. I have a MySQL table that has two rows. One row is quotes and the other is authors. What I'd like to do is display the authors in a form select field and allow the user to select an author. When the user selects an author all of the quotes from that author would be displayed. I think I have the first part working...

<form method=post>
<select>
<?
$sql="SELECT author FROM quotes GROUP BY author ASC";
$result = mysql_query($sql);
//display authors in select box
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {;
$author = $row["author"];
echo "<option name='$author'>$author</option>";
}
?>
</select>
</form>

Does that look right?

How do I display the quotes that match the author they select? Some authors have several quotes in the database.

Any help would be appreciated. Thanks.

Salsa

4:54 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



That looks okay, except that I think you want to make the option, name='author', not name='$author'.

To get your second select box dynamically populated with the quotes by a given author, however, is going to require some JavaScript. Either create arrays of all quotes for each author when you query the database, then write a JS function that's triggered by an onchange event in the author select box to repopulate the quote select box with the appropriate author's array of quotes, OR use an onchange="this.form.submit()" event in the author select box, which would send the form data back to the server whenever an author change is made, and where you'd do a query for the given author's quotes to repopulate the quote select box.

I hope this helps.

web_young

5:01 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



OR use an onchange="this.form.submit()" event in the author select box, which would send the form data back to the server whenever an author change is made, and where you'd do a query for the given author's quotes to repopulate the quote select box.

I think that's what I want to do, except the quotes won't actually need to display in a select box, they'll just display on the screen (sorry I wasn't very clear). The trouble I'm having is figuring out how to query the database to get the authors quotes. How would I get started on that?

Salsa

5:13 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



No, you said it clearly enough. I just didn't read it clearly
;)

One more point, though: You'll also need to add a form action, pointing to the script that will process the data. Like:

<form action="target_script.php" method="post">

You can use

$_SERVER['PHP_SELF']
as the target script if the form data will be processed by the same script as generates the form (probably the best idea).

I wish you well.

Salsa

5:20 pm on Jan 19, 2005 (gmt 0)

10+ Year Member



Ooops, I didn't see your new question. I think that all you'll need to do is something like:

if (isset($_POST['author'])) { 
$author = $_POST['author'];
$sql="SELECT quotes FROM quotes WHERE author='$author'";
... //rest of query processing and html formatting....
}