Dreamweaver MX can aid you in your PHP development, just as it helps with the other web- development languages. Although there isn't as much one-click support in Dreamweaver MX's PHP toolbars as, say, ColdFusion, Dreamweaver MX can still save you time in code entry and database calls.
Dreamweaver MX currently provides support for only the MySQL database when you're creating PHP pages. PHP can connect to other flavors of databases (see Chapter 10), but at this time, Dreamweaver MX doesn't support those other flavors. However, it does make MySQL database calls fast and easy. Let's take a look at Dreamweaver's handling of PHP.
When you choose to create PHP pages, Dreamweaver MX adds a PHP tab to the Insert panel, as shown in Figure 16.9. The PHP options are as follows:
What It Inserts at the Cursor Location
<?php $HTTP_POST_VARS; ?>
<?php $HTTP_GET_VARS; ?>
<?php $HTTP_SESSION_VARS; ?>
<?php $HTTP_COOKIE_VARS; ?>
<?php include(); ?>
<?php require(); ?>
<?php echo ?>
<?php if ?>
<?php else ?>
Figure 16.9: Dreamweaver MX's PHP tab
Two PHP functions on the Dreamweaver MX Insert bar are worth mentioning here: Include()and Require(). You use these two functions to include the contents of another .php code file within your code. Suppose you have a suite of database functions you've written to simplify repetitive database tasks such as opening and closing a connection. Instead of typing these functions into every page (or cutting and pasting the code), you can basically suck the function code into every file by using one of these two functions. For example, the code snippet below includes the file database_functions.php, which we'll assume contains a function called Open_Connection():
<?php include 'datase_functions.php'; Open_Connection(); ?>
Now that the file is included, we can simply refer to Open_Connection() just as if we had typed it in the code page ourselves. This is a great method by which you can save time and reuse code.
Include() and Require() are identical in every way except that if the file that you're including doesn't exist, Include() will simply give you a warning, whereas Require() will return a fatal error and stop processing the page. Basically, Include() won't bomb your code, but Require() will.As you can see, Dreamweaver MX icon support saves you typing, but that's about all at this point. But Dreamweaver MX's PHP support comes in handy when you start dealing with databases. Dreamweaver MX can handle all the cryptic MySQL calls for you.
PHP 4.1.0 deprecates the $http_type_vars arrays that Dreamweaver MX uses on its toolbar. Those variable arrays are replaced with newer, automatically global arrays such as $_get, $_post, $_cookie, $_server, $_env, $_request, and $_session. So you might want to start using these instead of Dreamweaver MX's default version just to make sure your code is compatible with future versions of PHP. You use these new variable arrays just as you use the older versions: instead of $http_post_vars['name'], you use $_post['name'].
Making connections to the various flavors of databases can be quite frustrating, especially when you're learning a new language. Every database and every language has its own specific manner in
Form variables Include CommentSession variablesCode Block ElseURL variablesCookie variablesEcho MoreTagsRequire If which to communicate, and PHP and MySQL are no different from the rest. Although you can certainly take the time to learn the connection strings that are required to talk to a database (see Chapter 10 for more information), you don't necessarily need to since Dreamweaver MX can handle it all for you. To illustrate this point, let's create a screen that shows the list of authors from our Books database. Follow these steps:
Create a new PHP page and add it to your PHP site. We're calling our PHP site PB_PHP.
For information on how to create a new site, see Chapter 2.
To create a new Dynamic PHP page, choose File ® New, and select the appropriate options, as shown in Figure 16.10.
Figure 16.10: Create a new PHP Dynamic Page.
To create a link to the database, click the plus sign (+) on the Databases tab of the Application panel. You'll see that Dreamweaver MX offers only the MySQL connection since that is the only database support currently offered by Dreamweaver MX when using PHP.
Assuming you've created a MySQL database named PubBooks or are using the sample included on the CD, enter something similar to what you see in Figure 16.11.Give the connection a name, tell Dreamweaver MX what MySQL server to use, give the username and password, and then tell Dreamweaver MX which database to use.
Figure 16.11: e're building a connection to our MySQL table.
Click the Test button to test your connection. (If your copy of MySQL and PHP are on the same computer, you can enter localhost as the server name.) If Dreamweaver MX can communicate with the database, it will give you a connection successful message like that in Figure 16.12.
Figure 16.12: Our connection was successful.
Now, we're ready to add the list of authors recordset to the Bindings tab of our Application panel.
Click the plus sign (+) on the Bindings tab, and choose Recordset to open the Recordset dialog box, which is shown in Figure 16.13.
Figure 16.13: We're going to select the data, sorted by last name, from the tblauthors table.
In the Name box, enter AuthorList, from the Connection drop-down list, select PubBooks, and from the Table drop-down list box, select tblauthors.
We want to sort by last name, so from the Sort drop-down list box, select LastName.
Click Test, and you should see a Test SQL Statement window similar to that in Figure 16.14, showing all the data from the table.
Figure 16.14: We can test to make sure our query works before we leave the query configuration.
Click OK, and Dreamweaver MX will add some code to your pages that extracts the query.
In our example, Dreamweaver MX added the following to the top of the page:
<?php require_once('file:///J|/Inetpub/wwwroot/helpmedia/PB_php/ Connections/PubBooks.php'); ?> <?php mysql_select_db($database_PubBooks, $PubBooks); $query_AuthorList = "SELECT * FROM tblauthors ORDER BY LastName ASC"; $AuthorList = mysql_query($query_AuthorList, $PubBooks) or die(mysql_error()); $row_AuthorList = mysql_fetch_assoc($AuthorList); $totalRows_AuthorList = mysql_num_rows($AuthorList); ?>
and added the following to the bottom:
<?php mysql_free_result($AuthorList); ?>
This code tells PHP to load the connections setup file PubBooks.php. It then sets up the MySQL connection, builds the SQL string we want to execute, and gets the data. The last line frees up the connection so that PHP can return the used memory and other resources to the server. Isn't it nice that Dreamweaver MX handles this for us?
Now, we're ready to actually display the data. Currently our page is blank, so let's add a Dreamweaver MX Dynamic Table to the page. Follow these steps:
Click in your PHP page.
Choose Insert ® Application Objects ® Dynamic Table to open the Dynamic Table dialog box, which is shown in Figure 16.15.
Figure 16.15: Dreamweaver MX guides us through creating our dynamic table.
In the Recordset drop-down list box, select AuthorList-it should already be selected since this is the only recordset on the page-and keep the rest of the defaults at this point.
Click OK, and Dreamweaver MX will add a table with a repeating region in your page. You should now see something similar to Figure 16.16.
Figure 16.16: We now have a repeating region within our table.
Add a header to your page, save it, and load it in your browser. You should see something similar to Figure 16.17, which shows our data list, sorted by last name, in an HTML table. And all we had to do was enter just a little bit of information. Dreamweaver MX makes it that easy.
Figure 16.17: We created our dynamic PHP/MySQL data page with just a few clicks and a little information.