473,626 Members | 3,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

creating form 'select lists' using PHP

Hi everyone

Is it by any chance possible to use mysql and php to auto create the
content for drop-down lists on forms by retrieving the values from a
database? The reason i ask is that on a site i am making i am asking
users to select from list (e.g. nationality) however i would like to
avoid typing out every possible value of which there are about 40
(which change). I would much prefer to keep a seperate table in my
database for the site.

Kind regards

Mcyi2mr3

Sep 25 '05 #1
2 5266
phpuser32423 wrote:
Hi everyone

Is it by any chance possible to use mysql and php to auto create the
content for drop-down lists on forms by retrieving the values from a
database? The reason i ask is that on a site i am making i am asking
users to select from list (e.g. nationality) however i would like to
avoid typing out every possible value of which there are about 40
(which change). I would much prefer to keep a seperate table in my
database for the site.

Kind regards

Mcyi2mr3

Sure. I assume the database fields have a numeric ID, so you can get the
ID as an answer while the user only sees the description.
Just fetch all the IDs and Descriptions from the database table (if you
order it by description, the user can type the first letter to select
one). Print the HTML for a dropdown list (<SELECT NAME=...>) and the
options. If you are using the ID, print something like:
'<OPTION VALUE=\'' . $ID . '\'>' . $description
for each record in the table
Make it part of a form and it is submitted with it.

Best regards
Sep 25 '05 #2
On or about 9/25/2005 4:03 PM, it came to pass that phpuser32423 wrote:
Hi everyone

Is it by any chance possible to use mysql and php to auto create the
content for drop-down lists on forms by retrieving the values from a
database? The reason i ask is that on a site i am making i am asking
users to select from list (e.g. nationality) however i would like to
avoid typing out every possible value of which there are about 40
(which change). I would much prefer to keep a seperate table in my
database for the site.

Kind regards

Mcyi2mr3

You did not say it but this code assumes you are using MySql Enum or Set
fields.

The following php script I wrote will take an Enum or Set field from a
table,creates a Select box or Check Boxes, and could be easily expanded
to have a Select Multiple or whatever you need. It will optionally sort
the field names and set selected elements from the corresponding
database data field. I'm fairly new at PHP and cobbled this together
from various other scripts I found on the net for use on my website. I
am certain someone can find ways to do it better, more elegantly or find
fault with the code, but this works for me. However if you do find a
serious fault please post it in this group.

Apologies for the messy indentation it's caused by the very poor
interaction of my text editor and e-mail program.

$table = Table name
$field = Enum or Set field in $table
$type = select or whatever (defaults to check box)
$data = field value (optional the data from field in table)
$sort = yes or no (sort options ascending)
$title = optional; null first field for a select
<?php
function DrawFromDB($tab le,$field,$type ,$data="",$sort ="yes",$title=" ")
{
//connect to DB;
$query=mysql_qu ery("SHOW COLUMNS FROM ".$table." LIKE
'".$field."' ") or die (mysql_error()) ;
if(mysql_num_ro ws($query)>0)
{
$row=mysql_fetc h_row($query);

$options=explod e("','",preg_re place("/(enum|set)\('(. +?)'\)/","\\2",$ro w[1]));
if ($sort =="yes")
sort ($options);
$ARay = explode(",",$da ta);
}
switch ($type)
{
case "select":
$text="\n\n<sel ect name='". $field ."'>\n";
if ($title > "")
$text.="<option value=\"\">" . $title . "</option>\n";
for ($i=0;$i<count( $options);$i++)
{
$selected = NULL;
if ($data == $options[$i])
$selected ="SELECTED ";
$text.="<option " . $selected .
"value=\"".$opt ions[$i]."\">".ucfirst( $options[$i])."</option>\n";
}
$text.="</select>\n\n";
break;

default:
$text="\n";
for ($i=0;$i<count( $options);$i++)
{
$checked=NULL;
for ($j=0;$j<count( $ARay);$j++)
{
if ($ARay[$j] == $options[$i])
$checked=" CHECKED ";
}
$text.="<INPUT TYPE='checkbox' " . $checked . " NAME='" . $field."[]'
VALUE='". $options[$i] . "'>".ucfirst($o ptions[$i])." \n";
}
$text.="\n";
break;
}
return $text;
}
?>
Sep 26 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2722
by: gonzalo briceno | last post by:
I have been using phplib for a while and I really like the framework except for form creation. Maybe it is me but I in my opinion there isn't a good way to create forms or should I say, everything else is so well done that the way you create forms seems to be too cumbersome, in particular making it so that a pull down menu selects a value that you assign it. In any case, does anyone know of any php based (or other readily accepted web...
3
10038
by: Craig Keightley | last post by:
I have the following form <FORM NAME="form1" METHOD="POST"> <?php do { ?> <input name="approve" type="checkbox" id="approve" value="<?php echo $row_rs; ?>"> <select name="select"> <option value="1">option 1</option> <option value="2">option 2</option> <option value="3">option 3</option>
1
2289
by: James | last post by:
I am creating a system whereby equipment is inspected. Data is inputted into an inspection form. However, any equipment that is not satisfactory needs to have spare parts ordered for that piece of equipment from a list of parts. I kind of need a form which lists parts and then check boxes next to a part can be created so the user can select which parts are needed and then a button is clicked which sends these parts to a report. I am not...
25
10212
by: Lyn | last post by:
Hi, I am working on a genealogy form. The only table (so far) lists everybody in the family, one record per person. Each record has an autonum ID. The parent form (frmMainForm) displays the data in each record, which includes the ID of the father and the mother (who also have records in the table). One record per form. I have a Tab Control in the form, and in one of the tabs I have a subform (sfmSiblings) in which I wish to list...
1
2908
by: longtim | last post by:
I have been having endless difficulty creating reports/queries that set any relevent parameters from controls in forms. I am creating an application under access 2003 but will target access 2000. The access file is in access 2000 format. I have a form that will hold the relevent parameters for the query/report that reports the statistics for all job records that match a certain criteria. These are: - A Customer Name.
2
12609
by: Yoshitha | last post by:
hi I have 2 drop down lists in my application.1st list ontains itmes like java,jsp,swings,vb.net etc.2nd list contains percentage i.e it conatains the items like 50,60,70,80,90,100. i will select any skill in 1st drop down list then i'll select % of this skill in the 2nd list box , based on the percentage i've selected in the 2nd list box it has to display 2 sets of drop down list boxes at run time one for selecting skill and
2
2106
by: justplain.kzn | last post by:
Hi, I have a table with dynamic html that contains drop down select lists and readonly text boxes. Dynamic calculations are done on change of a value in one of the drop down select lists. Using Safari,my first iteration the script works fine ( indicating that there are 33 form variables ). When trying another dropdown select value, the
7
1685
by: Roarke | last post by:
Forgive me if this is too long/too simple - I am new to this! I have an htm page with a "post" form. This has 2 droplists, the variables of which are posted to a php page. This page queries a mySQL database and echoes in rows any matching records. At present each result row lists the info from the database fields name, location and type. I had put an "a href=" around these results so if a row is clicked, it opened a seperate htm page...
2
1468
by: Craig | last post by:
I am trying to create a form that shows quote info, but also has a subform that lists all keywords and allows you to select multiple keywords to associate with that quote. I have never done a subform before and am struggling a little with it as there is an element of indirection involved. Any pointers would be greatly appreciated. A summary of my DB so far is:
0
8259
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8696
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8358
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5571
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1504
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.