Building Web Sites All-in-One For Dummies® (85 page)

BOOK: Building Web Sites All-in-One For Dummies®
11.43Mb size Format: txt, pdf, ePub

5.
Press F12.

Dreamweaver prompts you to save the document. After you save the document, the page launches in your default browser. Click the Refresh button a few times, and you'll see the script do its magic. (See Figure 2-12.)

Figure 2-12:
Generate a random image on the home page.

Create a rotating ad banner

If you have a client who wants to generate extra revenue, you can create ASP code to generate an ad banner every time a page is visited. The ad banner, when clicked (of course), redirects the visitor to another site.

To add a rotating ad banner to a Web page, complete these steps:

1.
Open the ASP document to which you want to add the ad banner.

2.
Add a table where you want the ad banner to appear.

We recommend putting the ad banner at the top of the document. If you're adding the ad banner to an existing ASP page, position your cursor at the top of the document and then choose Insert⇒Table. The table should be 1 row and 1 column.

3.
Position your cursor inside the table and then switch to Code view.

4.
Enter the following code:

<%

set adrotator=Server.CreateObject(“MSWC.AdRotator”)

adrotator.Border=”1”

Response.Write(adrotator.GetAdvertisement(“ads.txt”))

%>

The first line of code tells IIS to compile a script. The second line of code creates the
AdRotator
object. The third line of code puts a 1-pixel border around the ad. The fourth line of code uses the
Write
method of the
Response
object to read an advertisement from a text file. The fifth line of code ends the script.

5.
Create a new document in Notepad (Windows) or TextEdit (Mac).

This is the text file that contains three things: the path to the images, the URL, and the frequency with which the ad should be displayed.

6.
Enter the information for your ads. The following is a sample:

REDIRECT banners.asp

*

banners/adp.gif

http://www.accessdigitalphotography.com/

Visit Access Digital Photography

50

banners/si.gif

http://www.superbimages.net

Visit Superb Images

30

banners/pL.gif

http://www.pixelicious.info

Pixelicious – The Digital Photography Podcast

20

The first line in the document specifies the document that contains the code to redirect the visitor to the ad site URL. The asterisk tells IIS that the ads follow. The three ads in this document will be rotated by the
AdRotator
component.

Here's a rundown of what each line in the ads designates:

•
Path:
The first line of each ad is the path to the image file. In this case, all of the image files are in a folder called Banners.

•
URL:
The second line of each ad is the URL to which the visitor will be redirected.

•
ALT text:
The third line of each ad is the ALT text that will be generated for the image.

•
Frequency:
The fourth line of each ad is the frequency in which it will appear. The first ad will appear 50 percent of the time, the second ad 30 percent, and the third ad 20 percent.

7.
Save the document as
ads.txt
.

Save the document in the root folder of your Web site.

8.
In Dreamweaver, create a new ASP document.

Choose the ASP VBScript option from the Page Type column of the New Document dialog box.

9.
Switch to Code view.

10.
Create a new line after the

tag and enter the following code:

<%url=Request.QueryString(“url”)If url<>”” then Response.Redirect(url)%>

In a nutshell, the script reads the URL associated with the image from the
ads.txt
document and redirects the visitor to the page.

11.
Save the document as
banners.asp
.

You also need to save this file in the root folder of your Web site.

12.
In Dreamweaver, click the tab for the document in which you inserted the
AdRotator
component.

13.
Press F12.

Dreamweaver prompts you to save the document. After you save the document, Dreamweaver opens it in your default browser and displays a banner ad. (See Figure 2-13.) Refresh the browser a few times to see the ads rotate.

14.
Click an ad.

The
banners.asp
page is summoned, the script runs, and the visitor is redirected to the URL associated with the ad. (See Figure 2-14.)

Figure 2-13:
Banner ads, love 'em or ignore 'em.

Figure 2-14:
The
adbanner.asp page
redirects the visitor to the URL associated with the ad.

Chapter 3: Introduction to PHP

In This Chapter

Understanding PHP

Discovering PHP server side requirements and PHP syntax

Adding variables to the code

Creating conditional statements

Getting loopy:
while
and
for

If your client needs an interactive Web site, PHP is yet another language you can use. Like ASP (the subject of the previous chapter), PHP scripts within PHP pages are interpreted by the server. PHP supports databases. In fact, many bulletin board and blog applications are created with PHP code. These applications use a MySQL database to store bulletin board and blog data. You can also create your own PHP pages by using an HTML editing application like Dreamweaver. If you need interactive pages that update when they're visited, read on, for the pages that follow introduce you to PHP.

Defining PHP

PHP originally stood for Personal Home Page. Now, the acronym means PHP HyperText PreProcessor. PHP pages contain HTML tags and PHP scripts to create a page that can change depending on user interactivity. The PHP page, when visited, is converted into HTML by the server. You can't view the original PHP code by viewing the source code from the browser. All you see if you do this is the HTML code generated by the server after parsing the PHP code.

PHP pages can have all the other goodies that HTML pages have, such as text, hyperlinks, and so on. Then you can kick them up a notch or two with PHP code. Many applications, such as interactive calendars, blogs, and bulletin boards, are powered by PHP. Unlike ASP, PHP is
cross-platform,
which means it can be used on servers running Unix, Windows, Macintosh, and other operating systems.

Fulfilling PHP Server Side Requirements

If you're creating PHP pages for a client's Web site, you have to choose a server that supports PHP. Originally, PHP required using an Apache server. However, many Linux servers can also parse PHP code. In the same regard, you should choose a server that supports MySQL databases. Your customer might not need a database in the beginning. However, as his site grows, he might decide to add a blog or create a mailing list from user e-mail addresses he's collected. When this occurs, having a server with MySQL database capabilities is a definite plus.

You also need to make sure that the server has the version of PHP and MySQL to support the application you intend to use or the pages you intend to create. As of this writing, the current version of PHP is 5.2.6, and the current version of MySQL is 5.0. Check with your Web server's technical staff to see which version they have installed on their system.

You can download an Apache server, PHP, and MySQL for installation on your local machine. However, if you already have an IIS server installed, these items will be in conflict. Doug creates his PHP documents on his local machine and then uploads them to his Web server for testing.

Creating PHP Pages

When you create PHP code, you must create the code with the correct syntax. Otherwise, the code won't execute properly when parsed by the server. If you've created ActionScript code from scratch, you're familiar with the need for proper syntax. All PHP code begins with
and ends with
?>
. A semicolon (
;
) is required at the end of each line of code. In the following steps, you can see how to create a PHP page with a simple script:

1.
In Dreamweaver, choose File
⇒
New.

The New Document dialog box appears.

2.
Click Blank Page and then choose PHP.

3.
Click Create.

Dreamweaver creates a new PHP page.

4.
Switch to Code view.

Your cursor is positioned after the

tag.

5.
Press Enter (Windows) or Return (Mac) to create a new line.

6.
Enter the following code:

echo “Hello World”;

?>

The
echo
command tells PHP to display the text between the quotation marks. Dreamweaver displays the beginning and ending PHP tags (
and
?>
) as red, boldfaced text. If desired, you can use
print
in lieu of
echo
.

7.
Save the document and upload it to your server.

When you save the document, choose PHP from the Files of Type drop-down menu.

8.
When you view the page in a Web browser, you see the text
Hello World
. (See Figure 3-1.)

Figure 3-1:
Using PHP to say hello to the world.

Declaring and Using Variables

Variables are a very important part of any code language.
Variables
are code objects that are used to store and dispense information. For example, you can have a variable with no content — a
null variable
— that's filled with the content a user enters into a form. You use variables when sending information to a database or retrieving information from a database. Variables in PHP are preceded by a dollar sign (
$
). Listing 3-1 shows a simple PHP script that contains variables.

Listing 3-1: Using Variables in PHP

$fname=”Doug”;

$lname=”Sahlin”;

print “Hello $fname $lname”;

?>

If this were added to a PHP page, the resulting page that would be delivered to the user's Web browser would display as shown in Figure 3-2.

Figure 3-2:
Using variables in a PHP page to display information.

Variables are much more powerful than that, though. As we mention previously, you can use them to transfer data from a form to a database, or from a form to a page. Listing 3-2 shows a simple HTML form that asks the user for first name and last name.

Listing 3-2: Retrieving Data from a Form











First name
Last Name





The form has two fields, named
fname
and
lname
. The form method is
post
, which means the data is transferred transparently and isn't visible in the visitor's browser. When the visitor clicks the Submit button, the form action calls a page named
welcome.php
. The PHP code for the
welcome.php
page is shown in Listing 3-3.

Listing 3-3: PHP Code That Retrieves Variables from a Form

$fname=$_POST[‘fname'];

$lname=$_POST[‘lname'];

print “Hello $fname $lname”;

?>

The first line of code signifies that the script is PHP. The second line of code defines a variable called
fname
and sets it equal to the value of the
fname
form field by using the
POST
method. The third line of code defines a variable called
lname
and sets it equal to the value of the
lname
form field using the
POST
method. The fourth line of code prints the result with the text
Hello
. The fifth line of code ends the script. Figure 3-3 shows the result when the script is parsed by the server.

Figure 3-3:
Populating variables with data from a form.

Working with Conditional Statements

Conditional statements are like a fork in the road. When you create a dynamic Web site with PHP, you want your Web page to be as smart as possible, with code that can make decisions based on the information visitors input, the type of browser they're using, and so on. With a conditional statement, you can achieve this. If the condition is true, one thing happens; if not, another thing happens. For example, a conditional statement can verify a user's e-mail address by seeing whether it has a valid format
(
somebody
@
somewhere.com
).
If the user didn't enter a valid e-mail address, the code you created asks the techno-phobic user to enter a valid e-mail address.

Putting conditional statements into action

The easiest conditional statement is to verify whether a condition is true. To do this, you use an
if
statement. The syntax for the statement is shown in Listing 3-4.

Listing 3-4: Proper Syntax for an if Statement

if (condition is true) {

do this

}

more code

If the condition verifies as true, the code within the curly brackets is executed; otherwise, the next line of code is executed. For example, say you have a form on your Web site that asks for the visitor's first name and last name. You can use an
if
statement to validate the form. Listing 3-5 shows the HTML code for the form.

Listing 3-5: A User Information Form











First name
Last Name





The form is pretty straightforward, asking for the user's information. As soon as the user clicks the Submit button, the
validate.php
page loads. This page contains the code shown in Listing 3-6, which tests whether the user entered her first name and last name.

Other books

Earth Enchanted by Brynna Curry
Daughter of the Drow by Cunningham, Elaine
Bash by Briana Gaitan
Amplified by Tara Kelly
Vital Force by Trevor Scott
Reese by Terri Anne Browning
Inside Steve's Brain by Leander Kahney
Prince of Fire by Linda Winstead Jones