473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Populating <options> tag with SQL data?

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
6 5524
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
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
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
<?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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: dpj5754 | last post by:
Is there a simple and determinist way to make the difference between the 2 sequences: <XX></XX> and <XX/> The EndElement callback does not provide this information.
8
by: Daniel Hansen | last post by:
I know this must seem totally basic and stupid, but I cannot find any reference that describes how to control the spacing between <p>...</p> and <div>...</div> blocks. When I implement these on a...
4
by: Ferd Berfel | last post by:
given this code: <select name="mySelect" size="3" multiple> <option>one</option> <option>two</option> <option>three</option> <option>four</option> <option>five</option> <option...
4
by: joiv | last post by:
I'm making a <select></select> with lots of <option></option>. It contains all possible options. Because of the length of the list, I also have an <input type="text">. This is what I wish to do:...
10
by: BuddhaBuddy | last post by:
Platform is DB2/NT 7.2.9 The table was created like this: CREATE TABLE MYTEST ( MYTESTOID bigint not null primary key, FK_OTHEROID bigint not null references other, FK_ANOTHEROID bigint not...
11
by: Richard Maher | last post by:
Hi, I have read many of the copius entries on the subject of IE performance (or the lack thereof) when populating Select Lists. I don't mind the insert performance so much, (I get 100x120byte...
2
by: Johnny BeGood | last post by:
Hi All, I need to populate a list box and/or a dropdown list on a form. I have all the bits and pieces together, all bar the code which takes the result of a query and creates a list box. Any...
1
by: sosamv | last post by:
I'm trying to sort a select list using javascript...using : example: <option value="1">zebra</option> var a = document.getElementById('mylist').options.text; or
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.