By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,830 Members | 2,276 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

Populating <options> tag with SQL data?

P: n/a
Hello everyone,

I have a problem that I cannot seem to figure out. If any of you have
an answer or suggestion, I would appreciate it.

My HTML/PHP code needs to pull options from a database to populate the
<optionstag for a drop down menu and I cannot get it to work.

Basically, there is a form on the website with a drop down box. I need
the options in this box to be pulled from a preexisting table in a
database. That's all. Sounds very simple but I'm totally lost on how
to accomplish it.

Any help would be greatly appreciated.

Thank you,

Cresh

May 3 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On May 3, 10:37 am, cresh <davebow...@gmail.comwrote:
Hello everyone,

I have a problem that I cannot seem to figure out. If any of you have
an answer or suggestion, I would appreciate it.

My HTML/PHP code needs to pull options from a database to populate the
<optionstag for a drop down menu and I cannot get it to work.

Basically, there is a form on the website with a drop down box. I need
the options in this box to be pulled from a preexisting table in a
database. That's all. Sounds very simple but I'm totally lost on how
to accomplish it.

Any help would be greatly appreciated.

Thank you,

Cresh
Difficult to say. You haven't told us anything about the database
(like the tables you are using or even the server software). You
haven't told us what you've tried or done so far, and you haven't
shown us any code you've written. We can't help you without some
additional details.

May 3 '07 #2

P: n/a
On May 3, 9:51 am, ZeldorBlat <zeldorb...@gmail.comwrote:
Difficult to say. You haven't told us anything about the database
(like the tables you are using or even the server software). You
haven't told us what you've tried or done so far, and you haven't
shown us any code you've written. We can't help you without some
additional details

I am using PHP and HTML to make the form. The server software is MySQL
database. I can pull information from the database fine, I just don't
know how to put the information into an HTML form as options in a drop
down box.

For example:

The table contains five records: OPTION1, OPTION2, OPTION3, OPTION4,
OPTION5.

I can pull the data from the table using an array, no problems there.
I can display the data, etc., just can't figure out how to put the
data into a form <optiontag.

Maybe something like this:
<form action="process.php" method="post">
<p>Select An Option:
<select name="program" style="color:#FF0000">
<option value="<?echo $program[ ] ; ?>" selected="selected"><?echo
$program[ ] ; ?></option>
<option value="<?echo $program[ ] ; ?>"><?echo $program[ ] ; ?></
option>
<option value="<?echo $program[ ] ; ?>"><?echo $program[ ] ; ?></
option>
<option value="<?echo $program[ ] ; ?>"><?echo $program[ ] ; ?></
option>
<option value="<?echo $program[ ] ; ?>"><?echo $program[ ] ; ?></
option>
</select>

Do you see what I'm trying to do now? I'm not sure if I'm making
myself clear enough. $program[ ] is the variable to hold the
information pulled from the database table.

Thanks again,

Cresh

May 3 '07 #3

P: n/a
cresh wrote:
I can pull the data from the table using an array, no problems there.
I can display the data, etc., just can't figure out how to put the
data into a form <optiontag.
[...]
Do you see what I'm trying to do now? I'm not sure if I'm making
myself clear enough. $program[ ] is the variable to hold the
information pulled from the database table.
In order to iterate through an array, do this:

<?php

foreach($program as $item)
{
echo "<option value='$item'>$item</option>";
}

?>
Or maybe you'd prefer something like this:
<?php

$r = mysql_query("select foo from baaz where foobar");

while($row = mysql_fetch_assoc($r))
{
echo "<option value='{$row['foo']}'>{$row['foo']}</option>";
}

?>
I hope you get the idea.

By the way, try to make your PHP blocks larger, and fewer in number - code
is more readable that way.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

El clavo que sobresale siempre recibe un martillazo.
--Proverbio Chino
May 3 '07 #4

P: n/a
<?php
>
$r = mysql_query("select foo from baaz where foobar");

while($row = mysql_fetch_assoc($r))
{
echo "<option value='{$row['foo']}'>{$row['foo']}</option>";

}

?>

I hope you get the idea.

By the way, try to make your PHP blocks larger, and fewer in number - code
is more readable that way.
Thank you! This is -exactly- what I was trying to do.
I'm sorry, next post I'll try to get the PHP blocks larger and easier
to read.

Thanks again, this worked perfectly.

Cresh

May 3 '07 #5

P: n/a
Just in case anyone else needs this type of code, here is my small
example, thanks to the help received here. The reference to
"require.php" is where I store my database and server information,
such as dbase name, user, password, etc.

Thanks again for the assistance, it works great.

Cresh

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>Retrieve Information</title>
</head>
<body>
<? require('require.php');
$link = mysql_connect($dbasehost, $dbaseuser, $dbasepass);
if (!$link) {
die('Could not connect to the database. Error is: ' .
mysql_error());
}
mysql_select_db($dbasename);
?>
<form name"myform" action="file.php" method="post">
<p>Make a selection: <select name="program">

<?
$r = mysql_query("select information from info");
while($row = mysql_fetch_assoc($r))
{
echo "<option
value='{$row['information']}'>{$row['information']}</option>";
}
?>
</select></form>
</body>
</html>

May 3 '07 #6

P: n/a
If you are getting results back from your database (e.g. MySQL), you can use a
"while" loop to collect the information. This link at the PHP site would cover
it in more detail

http://www.php.net/manual/en/functio...l-db-query.php

but once you have the query setup you can built that into a variable such as...

$option = '';
while ($row = mysql_fetch_assoc($result))
{
$option .= "<option>" . $row['ColumnName'] . "</option>";

}

Tom

Thank you very much. It's great to find out information like this. I
appreciate it much.

Cresh

May 3 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.