473,383 Members | 1,742 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,383 software developers and data experts.

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 5246
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($table,$field,$type,$data="",$sort="yes ",$title="")
{
//connect to DB;
$query=mysql_query("SHOW COLUMNS FROM ".$table." LIKE
'".$field."'") or die (mysql_error());
if(mysql_num_rows($query)>0)
{
$row=mysql_fetch_row($query);

$options=explode("','",preg_replace("/(enum|set)\('(.+?)'\)/","\\2",$row[1]));
if ($sort =="yes")
sort ($options);
$ARay = explode(",",$data);
}
switch ($type)
{
case "select":
$text="\n\n<select 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=\"".$options[$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($options[$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
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...
3
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...
1
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...
25
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...
1
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...
2
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...
2
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. ...
7
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...
2
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.