Implementing the Project
With the project in place, you're ready to begin implementing the booksPHP
Building the HTML Interface
Most of the HTML for this project resides in the index
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
as shown in Listing 1
Some tags (such as <script>
) 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
As Listing 1
. This module contains the AJAX code for the booksPHP
- In the Projects view, expand the booksPHP node, and right-click on the Source Files node.
- Select the New → Other option from the context menu.
- In the New File wizard, select the "Other" item from the Categories panel.
- Click the Next button.
- Click the Finish button.
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
. 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:
- In the Projects view, expand the booksPHP node and right-click on the Source Files node.
- Select the New → PHP File option from the context menu.
- Provide a name for the new PHP file (call it search).
- 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
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:
- Start MySQL Server.
- In NetBeans, switch to Services view (if this view is not visible, then make it visible using the Window menu).
- Expand the Database node, navigate to the MySQL Server node, and from the context menu choose Connect (see Figure 4).
|Author's Note If you need to configure your MySQL server properties, choose Properties from the same context menu shown in Figure 4.
- 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.|
- 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.
- 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.|
- 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.
- In the Connect window, type root for the User name, and leave the Password field blank. Click OK (see Figure 6).
- NetBeans adds a new connection node named jdbc:mysql://localhost:3306/books to the Databases tree.
- Expand the jdbc:mysql://localhost:3306/books node, and right-click on Tables. Select the Create Table item from the context menu.
- 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.|
- Click OK.
- Right-click on the jdbc:mysql://localhost:3306/books node, and choose Execute Command from the context menu.
- 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',
insert into bookstore values (2,
'DHTML and CSS', 'Teague Sanders',
insert into bookstore values (3,
'Introduction to PHP', 'Weeling Tom',
insert into bookstore values (4,
'Web design', 'Weeling Tom',
insert into bookstore values (5,
'PHP 5', 'Weeling Tom',
insert into bookstore values (6,
'JavaServer Pages', 'Tick Own',
The database is now ready, so switch back to Projects
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.|