Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In data entry forms that collect personal information, it's very common to present a list of available states or other geographical regions. In a static HTML page, these sorts of lists have options that are filled in manually. For example, in this file, join3.php, there's a pulldown list, or a select control. The options in this control are listed manually. If you select the control in Dreamweaver and then go down to the Properties panel, then click the List Values button, you'll see the list of items that have been manually added to the list.
In many situations though, it's a good thing to populate these sorts of lists from a database on the server. I'll show you how to fill this sort of controlling with dynamic data from a database table. First, you'll need to make sure that your database connection is set up correctly. Go to the Databases panel, to the explorecalifornia database connection, and double-click it to open the MySQL connection dialog. The files that are included with this course have been configured for Windows and WAMP server.
The password is set to a blank string. If you're working on Mac with MAMP, type in a password of root, r-o-o-t. Then, either way, click the Test button. If you get this dialog, indicating you haven't specified a password, click OK, and click Test again. Then regardless of whether you're working on Mac or Windows, you should see the message that the connection was made successfully. Click OK, and click OK.
Open the explorecalifornia database connection, and open its Tables list, and from there, go to the states table. You'll see that the states table has two columns: stateId and stateName. These match the values that are going to be used in the pulldown list. Right-click on the table and choose View Data, and you'll see that the table is populated with all the data you need for the list. Click OK to close the panel. Click on the List Values button again and click the minus button a whole bunch of times.
You're going to remove all of the static data in the dynamic list, and then click OK. The next step will be to add a recordset to this page that retrieves the state's data from the states table on the database. Go to the Bindings panel, click the Plus button and choose Recordset (Query). Name your new recordset rsStates. Change the table to states. Leave the column is set to All.
Notice that you'll be retrieving the data for both the stateId and the stateName column. And set the Sort parameter to the stateName. Click Test and you should see the data returned in alphabetical order, by stateName. Click OK, and click OK again. Now your recordset is a part of the page. Peak at Code view, and you'll see that the code for implementing the recordset is at the top of the page, including some boilerplate PHP code, and the actual SQL statement that retrieves the data from the server.
Go back to Design View and once again click on the pulldown list which now shows that it's empty. Click Dynamic. You'll add data from the recordset. Under Options from Recordset, select rsStates. Set the values to stateId, and the labels to stateName. You can also, if you like, add a static option. The static option would be an item such as choose a state. For this example, we'll leave that blank. Click OK, save your changes, and then test the page in an external browser.
When the page opens, the data is retrieved from the server and the pulldown list is populated with the dynamic data. Let's take a look at the generated code for the select control. Close the browser, and in Dreamweaver go to Code View, and you'll see that the select control now loops over the recordset, and creates one option element for each item in the recordset. Here's the actual option element, and all the rest of this code is executing the loop over the recordset so that you get one item in the recordset for each item in the database.
So that's a look at how to retrieve data from the database server and implement a dynamic pulldown list or list control without having to do any hand coding on your own.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 86703 Viewers
80 Video lessons · 136004 Viewers
59 Video lessons · 54733 Viewers
52 Video lessons · 68628 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.