answersLogoWhite

0


Best Answer

This can be done with any Database system as the clever part is within the PHP.

There are two ways of doing this. The first method is having each record as a new selection. The second is having one field of the selection.

I'm going to use 'DATABASE {' and '} DATABASE' in order to show you where to put your 'Database {' and your closing '}' for the query.

Each Record

Assuming that you have at least two fields. One called value one called text then the following would work. (If your value is always the same as the Text shown to the user then you can change the set of text to value)

Echo '<select name="myselect">';

#DATABASE {

$current_value = $DATABASE['value'];

$current_text = $DATABASE['text'];

$myarray[] = '<option value="' . $current_value . '">' . $current_text . '</option>'; #A*

} DATABASE

Foreach($myarray as $key => $value){

Echo $value;

}

Echo '</select>';

Of course in this method you do not require an array as such. Due to the fact that on line A* could echo the value there and then.

One Field

This would be used more in the cases in which the table is built for forms and works like a Content Management System (CMS).

Firstly set out your record contents as: (I'm assuming here that the field is called dropdown)

I am a value[nowtext]I am some text[nextopt]I am a second Value[nowtext]I am some more text

Now let me explain. If you split this up in to [nextopt] there are two options. If you then split each again by [nowtext] you'll find 2 in between the [nextopt].

In this case, the first item set is the value of the Dropdown. The second is then the text and is split using [nowtext].

There are two functions in PHP which will help us. The first is called explode(); and this works like myfeild.split(); in Javascript. The second is implode();. Explode() is making an array. Think of it as, Explode the information into smaller chunks. Implode is the opposite, this takes an Array and turns it in to a String. (I've added two links to W3schools for reference on these tags)

Fairly simple when you understand how they work. Here's an example:

Echo '<select name="myselect">';

#Database {

$current_dropdown = $DATABASE['dropdown'];

$dropdown_array_a = explode('[nextopt]',$current_dropdown); #Makes an array

Foreach($dropdown_array_a as $key => $value){ #Runs for each item in array and loads in the key and the value of the array.

$dropdown_array_b = explode('[nowtext]',$value

Echo '<option value="' . $dropdown_array_b[0] . '">' . $dropdown_array_b[1] . '</option>';

}

# } DATABASE

Echo '</select>';

That was two of the many ways to perform this task. A link to the reference of Explode, Implode, Arrays and Foreach has been added to the Question.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can you populate a Dropdown with an Array from a Database using PHP?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is it possible to call a stored procedure from java script function and populate an aspnet dropdown list HTML control using the value returned by the SP?

No, a stored procedure can not be called from Javascript. A dropdown list however can be populated using a stored procedure.


How do you insert value to a dropdownlist from textbox input php?

Store the textbox input in a database using a html form prefarably. Using a loop get all the textbox input from database and use print/echo to show them in a dropdown.


What are Advantages of using gate array in digital design?

The advantages of using an array are that lists of the same data types can be stored easily without the need for a connection to a database. For example, a list of names can be stored in an array and only one variable need be declared. - Mike Hoerger


How do you define tab dropdown on java script?

It would be better if you do tabs with dropdown using CSS. Javascript can be easily disabled.


Queue ADT Using Array?

implement the queue ADT using an array


How do I put a dropdown list inside a prompt box using Javascript?

not posiible


How do you retrieve an image from a database using PHP?

It depends what Database you are using. I personally use MySQL so will explain how you would do it using MySQL. You would have to create the MySQL database. I would store the image URL rather than the actual image. Then call it using PHP and a MySQL query. For Example: $sql = "SELECT * FROM Images WHERE Img_Name = 'img1'"; $query = mysql_query($sql); $array = mysql_fetch_array(query); then you would call it into HTML like so:


Why and is not using ' and ' in reading a string using scanf?

I guess you wanted to ask, why is it scanf ("%s", array)and not scanf ("%s", &array).Well, array is by definition a pointer to the first element: array = &array[0]


What can be achieved from the database using forms?

what can be achieved from the database using reports


Can you create a database with only a form?

Yes , you can create a database using a form . But you have to do two things First make a form in html or jsp whatever you like. Then Connect the form to database using java database connectivity.Yes, we can create database with a form. First create a form using html or jsp then we can connect it to database using JDBC.


Is the relational database an example of oracle database?

A relational database is a type of database design. Oracle is a brand of database. You can create a relational database using Oracle. You can also create relational databases using other database applications.


Database Design?

form_title=Database Design form_header=Transform your company by incorporating a new database design. What program will the database be used with?=_ Please explain what you will be using the database for?="" Do you have a database that you're already using?= () Yes () No