RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Developing PHP Applications with NetBeans and Eclipse : Page 2

Discover the convenience of developing PHP applications using two of the most popular and powerful IDEs on the market, NetBeans and Eclipse.


Implementing the Project

With the project in place, you're ready to begin implementing the booksPHP functionality. You'll need some HTML for the presentation layer, some JavaScript to implement AJAX, and some PHP and database code for the business logic and persistence tasks. Because the application requires all the technologies in symbiosis, you'll get to explore how well NetBeans handles each (with a focus on its PHP support).

Building the HTML Interface

Most of the HTML for this project resides in the index.php page, which NetBeans automatically created in the project stub. NetBeans lets you add HTML elegantly through the Palette tool. If this tool is not visible, you can activate it from the Window → Palette menu. The Palette offers shortcuts to all the main HTML tabs, which you use by simply dragging-and-dropping the elements you need.

Figure 3. NetBeans Code Assistant: The Assistant provides help when inserting tags manually.
NetBeans require you to provide specific configurations for some tags before it adds them. For example, when you drag a <table> tag, you'll be prompted for the number of rows, columns, and so on. I won't go into further detail here—creating HTML code in NetBeans is pretty much a walk in the park. Therefore, use the Palette and drag and drop elements to modify the index.php as shown in Listing 1.

Some tags (such as <script> and <div>) aren't available through the Palette, so you'll need to add them manually. You'll find the NetBeans code assistant helpful here (see Figure 3).

Adding the JavaScript AJAX Code

As Listing 1 shows, index.php uses a JavaScript module named AJAXjs.js. This module contains the AJAX code for the booksPHP application. In NetBeans, you create a JavaScript file like this:

  1. In the Projects view, expand the booksPHP node, and right-click on the Source Files node.
  2. Select the New → Other option from the context menu.
  3. In the New File wizard, select the "Other" item from the Categories panel.
  4. In the same wizard, select the "JavaScript File" item from the File Types panel.
  5. Click the Next button.
  6. Provide a name for the new JavaScript file (for this example, the name is AJAXjs)
  7. Click the Finish button.
At this point, you'll see an empty JavaScript file in the NetBeans editor. Using the JavaScript code assistant, insert the self-explanatory code in Listing 2.

Author's Note: You'll find the bookanim.gif file mentioned in Listing 2 in the downloadable code for this article.

Implementing the PHP Code

Before implementing the PHP code for the booksPHP project, take a look at the complete list of NetBeans features for PHP developers.

You can explore every feature in that list while developing booksPHP! First, create a new PHP file, named search.php. This file will contain the PHP code to query a database containing a set of books stored by name, ISBN, author and price. Here's how to create the file:

  1. In the Projects view, expand the booksPHP node and right-click on the Source Files node.
  2. Select the New → PHP File option from the context menu.
  3. Provide a name for the new PHP file (call it search).
  4. Click Finish.
Author's Note: NetBeans offers wizards for creating PHP web pages, classes, and interfaces—all available through the same context menu used above to create the PHP file.

Now you'll see an empty PHP file in the NetBeans editor. Use the code assistant, syntactic and semantic code highlighting, pop-up documentation, code formatting and folding, instant rename, code templates, and automatic code completion (including bracket completion), to create the code in Listing 3.

The code in Listing 3 uses a database.php file. This is a simple PHP file that provides a connection to the books database. You can create the code using the same procedure listed above to create search.php:

      or die ("<error>Fatal server error!</error>");
   $select = mysql_select_db("books", $id)
      or die ("<error>Fatal server error!</error>");
Figure 4. Connecting to MySQL Server: From the Services tab in NetBeans, you can find and connect to databases.

Creating the Database

Obviously, the database connection code shown in the previous section requires a books database. You can use NetBeans to create the database as well:

  1. Start MySQL Server.
  2. In NetBeans, switch to Services view (if this view is not visible, then make it visible using the Window menu).
  3. Expand the Database node, navigate to the MySQL Server node, and from the context menu choose Connect (see Figure 4).
  4. Author's Note If you need to configure your MySQL server properties, choose Properties from the same context menu shown in Figure 4.

  5. NetBeans establishes the connection. Now, under the MySQL Server node, you'll see a list containing the available databases.
    Figure 5. Creating a MySQL Database: Enter a database name and choose which user(s) or groups you want to provide access to.
  7. You can create new databases as well. To create the books database, right-click on the MySQL Server node and select the Create Database option.
  8. In the Create MySQL Database dialog box, provide the database name (books) and grant full access to the desired user (for all users use *@localhost as shown in Figure 5).

Populating the Database

With the books database created, follow this procedure to connect to it and create some sample data:

Figure 6. Making the Database Connection: Insert the username and password you want to use to connect to the books database.
  1. Connect to the new books database by right-clicking on the books node (which should appear now under the MySQL Server node) and select Connect from the context menu.
  2. In the Connect window, type root for the User name, and leave the Password field blank. Click OK (see Figure 6).
  3. NetBeans adds a new connection node named jdbc:mysql://localhost:3306/books to the Databases tree.
  4. Expand the jdbc:mysql://localhost:3306/books node, and right-click on Tables. Select the Create Table item from the context menu.
  5. In the Create Table wizard, type bookstore as the table name, and specify its structure as shown in Figure 7.
    Figure 7. Create Table: Use the Create Table dialog to define the sample bookstore table structure.

  7. Click OK.
  8. Right-click on the jdbc:mysql://localhost:3306/books node, and choose Execute Command from the context menu.
  9. An SQL Command window opens. You can use this window to populate the bookstore table with a few records for test purposes. To do that, enter and execute the following SQL statements. To execute an SQL statement, click on the Run SQL button (first button from the left), on the SQL Command toolbar:
   insert into bookstore values (1, 
      'Introduction to PHP', 'Mark User', 
      '3334-4424-334-3433', 500)
   insert into bookstore values (2, 
      'DHTML and CSS', 'Teague Sanders', 
      '4545-23-23-23-23232', 1500)
   insert into bookstore values (3, 
      'Introduction to PHP', 'Weeling Tom', 
      '4334-2323-23233-434', 300)
   insert into bookstore values (4, 
      'Web design', 'Weeling Tom', 
      '4334-2323-23233-434', 600)
   insert into bookstore values (5, 
      'PHP 5', 'Weeling Tom', 
      '444-87-67665-678678', 600)
   insert into bookstore values (6, 
      'JavaServer Pages', 'Tick Own', 
      '897-9898-987-099', 800)
The database is now ready, so switch back to Projects view.

Running booksPHP from NetBeans

Finally, you can run and test the booksPHP application. Make sure MySQL and Apache HTTP Server are running, and then select Run → Run Project. A web browser launches, and you should see the application interface as shown in Figure 8.

Figure 8. The booksPHP program In Action: Here's the booksPHP sample application running in a browser.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date