Tải bản đầy đủ

Adobe dreamweaver CS5 with PHP


Adobe® Dreamweaver® CS5 with PHP

Training from the Source

David Powers


Adobe® Dreamweaver® CS5 with PHP: Training from the Source
David Powers
Adobe Press books are published by:
Peachpit
1249 Eighth Street
Berkeley, CA 94710
510/524-2178
800/283-9444
For the latest on Adobe Press books, go to www.adobepress.com
To report errors, please send a note to errata@peachpit.com
Peachpit is a division of Pearson Education.
Copyright © 2011 David Powers
Acquisitions Editor: Victor Gavenda

Project Editor: Rebecca Freed
Development Editor and Copyeditor: Anne Marie Walker
Production Editor: Becky Winter
Technical Editor: Tom Muck
Compositor: Danielle Foster
Indexer: Rebecca Plunkett
Cover Design: Charlene Charles-Will

Notice of Rights
All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic,
mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For information on getting permission for reprints and excerpts, contact permissions@peachpit.com.

Notice of Liability
The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been
taken in the preparation of the book, neither the author nor Peachpit shall have any liability to any person or entity
with respect to any loss or damage caused or alleged to be caused directly or indirectly by the instructions contained
in this book or by the computer software and hardware products described in it.

Trademarks
Adobe, the Adobe logo, and Dreamweaver are registered trademarks of Adobe Systems in the United States and/or
other countries.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks.
Where those designations appear in this book, and Peachpit was aware of the trademark claim, the designations
appear as requested by the owner of the trademark. All other product names and services identified throughout the
book are used in an editorial fashion only and for the benefit of such companies with no intention of infringement
of the trademark. No such use, or the use of any trade name, is intended to convey endorsement or other affiliation
with this book.
ISBN-13: 978-0-321-71984-3
ISBN-10:
0-321-71984-0
987654321
Printed and bound in the United States of America


Bio
David Powers has been writing about Dreamweaver, PHP, CSS, and web development since
2003. This is his twelfth book on the subject. David started developing websites in 1994,
shortly after assuming the role of Editor, BBC Japanese TV. He needed a way of advertising the fledgling channel in Japan but had no budget. So, he begged the IT department for a
corner of server space and singlehandedly developed an 80-page bilingual website, which he
regularly maintained for the next five years.

After a career spanning three decades in radio and TV news, David left the BBC in 1999 to
work independently. He’s an Adobe Community Professional and Adobe Certified Instructor
for Dreamweaver. You’ll often find him giving help and advice in the Dreamweaver forums
and Adobe Developer Center—to which he has contributed many popular tutorials and training videos. He greatly enjoys traveling and taking photos—all the photos used in this book
were taken by him.
David has also translated a number of musical plays from Japanese into English, and he likes
nothing better than sushi with a glass or two of cold sake.


Acknowledgments
For several years I’ve bent the ears of the long-suffering Dreamweaver engineering team
to improve support for PHP. Wow! They certainly came up trumps. My thanks to Devin
Fernandez, Scott Fegette, Don Booth, Randy Edmunds, Chris Bank, Virgil Palanciuc, Jon
Michael Varese, and the many others who have helped me dig deeper into Dreamweaver over
the past few versions to understand the program’s strengths and weaknesses (yes, there are
some—and I’m sure the team is already working on eliminating them).
I’m particularly grateful to Scott, one of the Dreamweaver product managers, whose idea it
was to get me to write this book. I hope it’s not too far removed from what he was hoping for.
Scott passed my name to Victor Gavenda, the Executive Editor at Adobe Press, while attending Adobe MAX at Los Angeles in 2009. For those of you who don’t know, MAX is an annual
geekfest that’s a mixture of presentations by leading web professionals, sneak peeks into
Adobe’s future technology, and wild parties (Scott plays a mean guitar).
Victor welcomed me into the Adobe Press/Peachpit family with grace and courtesy. My
thanks go to him and to the two editors who worked directly with me on the book, Rebecca
Freed and Anne Marie Walker. Thanks also to the production team for going the extra mile to
make the code easier to read.
Finally, I mustn’t forget Tom Muck, a true Dreamweaver and PHP expert who checked the text
and code for technical accuracy. This is the third book we’ve worked on together. Tom always
manages to keep me on the straight and narrow, spotting important details I’ve missed and suggesting ways to improve the text. Any mistakes that remain are my responsibility alone.


Contents
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . viii

LESSON 1

Why PHP and Why Dreamweaver CS5? . . . . . . . . . . . . . . . . . . . . . 3
A Rich Mix of PHP Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
What Is PHP? What Does It Do? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
A Tour of the Main PHP Features in Dreamweaver CS5 . . . . . . . . . . . . . 9

LESSON 2

Getting Ready to Develop with PHP. . . . . . . . . . . . . . . . . . . . . . .27
Setting Up a Local Testing Environment . .
Checking Your PHP Installation . . . . . . . .
Using Virtual Hosts . . . . . . . . . . . . . . . .
Setting Up a PHP Site in Dreamweaver CS5

LESSON 3

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.28
.30
.40
.47

A Quick Crash Course in PHP. . . . . . . . . . . . . . . . . . . . . . . . . . . .60
How PHP Makes Pages Dynamic . . . . . . . . . . . . . .
Taming the Unknown with Variables . . . . . . . . . . .
Grouping Related Values in Arrays . . . . . . . . . . . . .
Using Conditions to Make Decisions. . . . . . . . . . . .
Using Functions to Perform Tasks . . . . . . . . . . . . .
Using Objects and Resources . . . . . . . . . . . . . . . .
Using Operators for Calculations and Joining Strings .
Automating Repetitive Tasks . . . . . . . . . . . . . . . .
Including External Files . . . . . . . . . . . . . . . . . . . .
Understanding Error Messages . . . . . . . . . . . . . . .

LESSON 4

.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.

. .61
. .63
. .70
. .74
. .79
. .88
. .91
. .96
. 101
. 108

Restyling a WordPress Site . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Understanding the Structure of a CMS . . . . . . . . . . . . . . . . . . . . . . 112
Installing WordPress . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Creating a WordPress Theme . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119


vi

Contents

LESSON 5

Designing and Building Your Own Database. . . . . . . . . . . . . . . . . 141
Working with MySQL . . . . . . .
Creating a Database and Tables.
Creating MySQL User Accounts .
Importing Existing Data. . . . . .

LESSON 6

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

. 142
. 155
. 161
. 165

Generating PHP Automatically with Server Behaviors. . . . . . . . . . . 170
What Server Behaviors Do . . . . . . . . . . . .
Connecting to the Database. . . . . . . . . . .
Inserting Records into a Table . . . . . . . . . .
Creating a Login System . . . . . . . . . . . . .
Displaying, Updating, and Deleting Records
Evaluating the Server Behaviors . . . . . . . .

LESSON 7

.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

. 171
. 171
. 177
. 183
. 192
. 207

Validating Input on the Server . . . . . . . . . . . . . . . . . . . . . . . . . 212
Introducing the Zend Framework . . . . . . . . . . . . . . . . . . . . . . . . . 213
Improving the Registration Form. . . . . . . . . . . . . . . . . . . . . . . . . . 218
Authenticating User Credentials with Zend_Auth . . . . . . . . . . . . . . . 247

LESSON 8

Zending Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
How PHP Handles Email . . . . . .
Stopping Spam with a CAPTCHA .
Processing User Feedback . . . . .
Processing Other Form Elements .
Resetting Forgotten Passwords . .
Unsubscribing Registered Users .

LESSON 9

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

. 259
. 263
. 266
. 277
. 286
. 299

Uploading Images and Other Files. . . . . . . . . . . . . . . . . . . . . . . 304
Understanding How PHP Uploads Files .
Creating an Upload Form. . . . . . . . . .
Using Zend_File for Uploads. . . . . . . .
Sending Email Attachments . . . . . . . .

LESSON 10

.
.
.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

. 305
. 306
. 308
. 334

Inserting Data into Multiple Tables . . . . . . . . . . . . . . . . . . . . . . 344
Assessing the Task . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Creating the Database Structure . . . . . . . . . . . . . . . . . . . . . . . . . . 346
Building the CMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349


Training from the Source

LESSON 11

Updating and Deleting Files in Related Tables . . . . . . . . . . . . . . . 378
Selecting Records with SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379
Completing the CMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383

LESSON 12

Using Ajax to Refresh Content . . . . . . . . . . . . . . . . . . . . . . . . . 418
Enhancing Pages with Ajax . . . . . . .
Introducing Adobe Widget Browser . .
Configuring a Widget . . . . . . . . . . .
Creating a Master/Detail Set . . . . . . .
Refreshing a Page Without Reloading .
Creating Clean URLs . . . . . . . . . . . .

LESSON 13

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

. 419
. 420
. 424
. 437
. 448
. 457

Deploying Your Site Online . . . . . . . . . . . . . . . . . . . . . . . . . . . 461
Transferring a Database. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Preparing Your PHP Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
Setting Up Your Remote Server in Dreamweaver. . . . . . . . . . . . . . . . 470

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476

vii


Introduction
My first encounter with PHP came about 10 years ago. By that time, I already had plenty of
experience developing websites. I had started out writing HTML in a text editor before settling
on Dreamweaver as my favorite authoring tool. A new project involved publishing more than
30 articles a day. It was a subscription service, so the site needed to be password-protected and
searchable. An ordinary website wouldn’t do. That’s when PHP came to the rescue.
PHP makes communication with a database a breeze, so content can be stored in the database,
making it searchable. Instead of creating a new page for every article, pages are populated
dynamically with the requested items. You can also password-protect the administrative or
members-only area of a site. PHP does a lot more: It can send email, upload files, and attach files
to emails—all of which you’ll learn how to do in this book. PHP is also the driving force behind
the three most popular content management systems: Drupal, Joomla!, and WordPress.
So, where does Dreamweaver come into the picture? Dreamweaver has supported PHP to some
degree since 2002, mainly through server behaviors, which automatically generate PHP code
for some basic tasks. But the level of support has taken a quantum leap forward in Dreamweaver
CS5. The server behaviors are still there (see Lesson 6), but they take a back seat.
The big changes lie in code hinting, embedded PHP documentation (including examples),
autocompletion of variables, automatic discovery of dynamically related files, and—perhaps best of all—the ability to view and navigate through PHP pages without leaving the
Document window. As a result, it’s now possible to style WordPress, Joomla!, and Drupal in
Dreamweaver CS5 without the need to generate static pages. These changes are described in
detail in Lesson 1, but in a nutshell they should appeal to designers and developers alike.
PHP’s popularity springs from being easy to learn. You can achieve practical results very
quickly. Of course, like any skill, becoming an expert takes time and practice. The new PHP
features in Dreamweaver CS5 not only help the learning process, but you’ll find them even
more useful as you gain experience. Dreamweaver is my preferred choice for designing the look
of a website and organizing files, but I was beginning to use dedicated PHP authoring tools for
the dynamic aspects of development. Dreamweaver CS5 has changed all that. I now have the
best of both worlds in the same program.


Training from the Source

Who This Book Is for
This is a “beyond the basics” book, so you should already have a solid understanding of how
a website is built. You should also have a good understanding of HTML, because PHP code
needs to be embedded in the underlying structure of a page to display the dynamic output. It’s
not necessary to know every tag and attribute, but if you don’t know the difference between a
3lc5 and an 3c`5 tag, you’ll be lost. All the example files and exercises are styled with CSS, but
design is not the focus of this book. You don’t need to understand CSS to work through the
lessons, but your web development skills would certainly be the better for it. You’ll also find it
makes it easier to follow Lesson 4, where you create a new WordPress theme.
You don’t need prior knowledge of PHP. This book doesn’t teach PHP in a formal manner,
but Lesson 3 provides a crash course in how to write PHP, and Lesson 5 teaches the basics of
database design using MySQL, the most popular open source database.
If you already know some PHP, all the better. This book moves at a fairly rapid pace.
Lessons 7–12 make extensive use of the Zend Framework, a powerful library of PHP
components that take a lot of hard work out of creating dynamic sites. Lesson 12 also uses
the jQuery JavaScript framework. Again, you don’t need prior knowledge of jQuery or
JavaScript, but it will certainly help.

How to Use This Book
Time is precious, so you probably want to jump straight to the solution for your current
problem. If you have considerable PHP experience, that approach might work. However,
the majority of readers should start with Lesson 1 and work through each one in sequence
because each lesson builds on the previous one. If you skip ahead, you’re likely to miss a vital
explanation and will need to backtrack anyway.
The “Approximate Time” at the beginning of each lesson is simply an estimate of the time it
will take to work through the exercises. Don’t regard it as a challenge, and don’t feel downcast
if you take much longer. Each lesson is packed with information. Take time to absorb it, and
break the lesson into smaller chunks to match your own pace.
Most lessons contain reference sections followed by hands-on exercises. Each step explains
not only what to do, but also why you’re doing it. The idea is to help you think about how you
could apply the same techniques to your own projects. This isn’t a point-and-click book, but
instead is one that aims to stimulate your problem-solving abilities. The more you think, the
more you’re likely to get out of it.

ix


x

Introduction

Accompanying files
The accompanying CD contains all the files necessary to complete the exercises in this book.
The only exceptions are the PHP/MySQL development environments described in Lesson 2
and the LightBox Gallery Widget in Lesson 12. PHP and MySQL are updated frequently, so it
makes more sense to get the most recent versions from the source. In the case of the LightBox
Gallery Widget, one object of the exercise is to show you how to install the Adobe Widget
Browser and download widgets from the Adobe Exchange.
Lesson 2 describes how to set up the Dreamweaver site to work through the exercises in this
book. The files for each lesson are in folders named lesson01, lesson02, and so on. There
are no files for Lesson 13. For each lesson that contains exercises, there are normally three
subfolders: completed, start, and workfiles. The workfiles folder is deliberately left empty; it’s
where you should create and save the files for the lesson’s exercises. If you follow this structure,
the exercise files will use the common style sheets that are stored in the styles folder.
To save time, many exercises have partially completed pages, which you should copy from the
start folder to the workfiles folder for that lesson. The completed folder contains copies of the
exercise files shown at various stages of completion.
In Lessons 10 and 11, you should create a folder called cms in the site root. The cms_complete
folder contains a full working copy of the completed project.
NOTE: The files were created on a Windows computer but are fully compatible with Mac OS X.
However, the path in library.php needs to be adjusted to match the location of the Zend
Framework files. See Lesson 7 for details.

Windows/Mac differences
The few Dreamweaver CS5 and PHP differences between Windows and Mac OS X have been
pointed out at relevant places in the book.
Keyboard shortcuts are given in the order Windows/Mac, but in the rare cases where there is
no Mac equivalent, this has been pointed out. On some Mac keyboards, the Opt(ion) key is
labeled Alt. On a UK Mac keyboard, use Alt+3 to type the hash symbol (#).
Using a multi-button mouse with a Mac is now so common that the instructions refer only to
right-click. If you prefer a single-button mouse, use Ctrl-click.


Training from the Source

Code portability
One of the pleasures of working with PHP is that it’s platform-neutral. All the PHP code in this
book works equally well on Windows, Mac OS X, and Linux. However, it’s important to realize
that different versions of PHP and MySQL have different functionality. Also, server administrators have the ability to turn off certain features. To use this book, your web server must be running PHP 5.2 and MySQL 4.1 or later. The code will not work with earlier versions.

Getting help
When you encounter a problem, the first person to look to for help is you. Did you skip a step
or mistype the name of a variable or function? One of the quickest ways of finding an error
is to use Dreamweaver’s File Compare feature (choose Help > Using Dreamweaver CS5 >
Creating and Managing Files > Comparing files for differences) to compare your file with the
version in the completed folder.
File Compare requires a third-party file comparison utility. If you don’t have one installed,
WinMerge (http://winmerge.org) for Windows and TextWrangler (www.barebones.com/
products/textwrangler/) for Mac OS X are both free.
If you can’t solve the problem on your own and a quick search on the Internet doesn’t produce the answer, post a question in the Adobe forums. The best one for PHP questions is
the Dreamweaver Application Development forum at http://forums.adobe.com/community/
dreamweaver/dreamweaver_development. I’m frequently there providing help, so you might
even get an answer from me.
I also post updates and tutorials on my website at http://foundationphp.com/, and you can
follow me on Twitter @foundationphp.
Every care has been taken to eliminate errors, but if you think you have found one, please
email errata@peachpit.com with the details.

Layout conventions
The following text conventions are used throughout this book:
r Boldface text. Words in bold text indicate input that you should type in a field or the
name of a file you should create.

xi


xii

Introduction

r Boldface code. Code that is added or changes is displayed in boldface.
`]VGFJK r
`]\dgkpVGFJKRlj\ieXd\T ss\dgkpVGFJKRgXjjnfi[T
]X`c\[4KIL<2
t\cj\r
i\hl`i\VfeZ\c`YiXip%g_g 2

r

r Long code. Sometimes, code won’t fit on a single line on the printed page. Where this
happens, an arrow indicates the continuation of a broken line like this:
i\jlck4i\ZXgkZ_X$5m\i`]pVGFJKRÊi\ZXgkZ_XVZ_Xcc\e^\V]`\c[ËT#
± VGFJKRÊi\ZXgkZ_XVi\jgfej\V]`\c[ËT 2

r Italics. Text in italics is for emphasis or to introduce important concepts.

Let the Journey Begin
Above all, enjoy the experience that lies ahead. Even if you find working with code uncomfortable to begin with, PHP is not hard. Welcome to the ever-growing PHP community.


This page intentionally left blank


LESSON 1

What You Will Learn
In this lesson, you will:
r See how PHP builds web pages dynamically and communicates with a database
r Explore the greatly improved PHP features in Dreamweaver CS5
r See how to change Dreamweaver’s default settings
r See how Live View can display pages from a PHP content management system,
such as WordPress
r Understand how to access and filter dynamically related pages

Approximate Time
This lesson should take approximately 1 hour to complete.

Lesson Files
Media Files:
None
Starting Files:
None
Completed Files:
lesson01/time.php


LESSON 

Why PHP and Why
Dreamweaver CS5?
“If you had one extra day in your week to learn a new technology, what would you choose?”
That’s one of the questions the Adobe Dreamweaver product team asked users before starting work on Dreamweaver CS5. An overwhelming majority answered, “PHP or a PHP framework.” It’s a message the team took on board, which resulted in a version of Dreamweaver that
turns it into a powerful development environment for PHP without losing any of its visual
development features. Dreamweaver CS5 is a program that gives programmers and designers
plenty to smile about. For designers, probably the most welcome change is the ability to see a
WordPress, Drupal, or Joomla! site in Live View.

Dreamweaver CS5 can now display WordPress and other popular CMSs directly in Live View.

3


4

LESSON : Why PHP and Why Dreamweaver CS5?

A Rich Mix of PHP Features
For programmers, many new features can be put at the top of the list: vastly improved PHP
code hinting, automatic detection of dynamically related files, autocompletion of variables,
and so on. But the feature that will appeal to beginners and experienced programmers alike
is automatic syntax checking. Dreamweaver CS5 constantly monitors your PHP code and
highlights any line in which there is a mistake.
One thing that hasn’t changed—and this will come as a disappointment to some existing users—
is the range or functionality of PHP server behaviors. A server behavior is a Dreamweaver tool
that automatically generates PHP code for such things as inserting, updating, and deleting
records in a database. Many people like server behaviors because everything is done through
dialog boxes without the need to touch a line of code. Server behaviors are great for rapid prototyping or taking your first steps with PHP, but they take you only so far. You soon discover that
you need to customize them to do anything beyond the most basic tasks—and you can’t do that
unless you understand the code. This book covers the main server behaviors, but also takes you
further to make full use of the new PHP features in Dreamweaver CS5. However, that doesn’t
mean you’ll end up typing endless amounts of PHP code.
Dreamweaver CS5 allows you to take advantage of a much wider range of resources coded by
some of the most experienced people in the business. You’ll learn how to work with one of the
most popular PHP-driven content management systems (CMS), WordPress. You’ll also learn
how to upload files, send emails with attachments, and interact with a database using a selection of components from the Zend Framework. The framework is designed in such a way that
you can cherry pick, making it easy to add sophisticated functionality to a website without
needing to become an expert programmer beforehand.
You don’t need previous PHP experience to use this book—although you’ll be able to progress
more rapidly or jump to lessons that interest you if you do. Everything is explained along the
way, along with details of where to get more information. The only prerequisite is that you be
familiar with building websites using HTML and CSS. Preferably, you should also be familiar
with the Dreamweaver user interface.
NOTE: I use HTML to refer to both HTML (Hypertext Markup Language) and XHTML (Extensible
Hypertext Markup Language). All the HTML code in this book conforms to XHTML 1.0
Transitional, the default setting in Dreamweaver CS5.

Before embarking on the journey ahead, let’s take a quick look at PHP and what it’s for, and
then take a brief tour of the features in Dreamweaver CS5 that make it such a good development environment for PHP-driven websites.


What Is PHP? What Does It Do?

What Is PHP? What Does It Do?
PHP is a server-side technology that builds web pages dynamically. Let’s say you have a product catalog. Instead of updating your web pages each time a product is added or removed,
you can store the details in a database and use PHP to query the database and build the page
automatically. Nor do you need to create a separate page for each product. Just build one page,
and PHP fills in the details. Other uses of PHP include creating login systems, uploading files,
and sending emails. Just about every online store, news website, blog, or social networking site
uses PHP or a similar server-side technology.
Depending on how it’s being used, PHP code can be embedded in a web page or stored in
external files. However, unlike CSS or JavaScript, PHP code always remains on the web server.
Visitors to your website never see it. All they see is the output. For example, lesson01/time.
php contains the following code:
3g5K_\k`d\`eCfe[fe`j36g_g
efn4e\n;Xk\K`d\ 2
efn$5j\kK`d\qfe\e\n;Xk\K`d\Qfe\\Z_fefn$5]fidXk^%`X 2
653&g5
3g5@eCfj8e^\c\j#`kj36g_g
efn$5j\kK`d\qfe\e\n;Xk\K`d\Qfe\8d\i`ZX&CfjV8e^\c\j
\Z_fefn$5]fidXk^%`X 2
653&g5

2

The PHP code is embedded in a couple of HTML paragraphs between 36g_g and 65 tags. Even
if you don’t understand how it works, you can probably guess that this code has something to
do with dates and time zones. In fact, it displays the current time in London and Los Angeles. It
doesn’t matter where you are or when you load the page, as long as the server clock is set correctly,
you will always get the correct time in those two cities. If you right-click to view the source code in
a browser, you see only the HTML output. All the processing is done on the web server.

5


6

LESSON : Why PHP and Why Dreamweaver CS5?

NOTE: You won’t be able to view this file on your computer until you have created a PHP
testing environment, which you’ll learn how to do in Lesson 2. Readers with eagle eyes will have
noticed that the screen shot shows only a seven-hour difference, whereas there’s normally an
eight-hour difference between London and Los Angeles. The screen shot was taken in midMarch after the United States had switched to daylight saving time, but the UK had not. The
PHP code is smart enough to adjust automatically for daylight saving time.

In this example, the PHP code does all the work itself. But in many cases, PHP acts as an
intermediary to a database. The following diagram outlines the basic process.
Page request

Database query

Internet

HTML output

Query result

Web server/
PHP engine

Database
server

This is what happens when a browser requests a web page that uses PHP:
r The web server sends the page to the PHP engine—which resides on the server—
for processing.
r If the PHP script doesn’t need to communicate with the database, the PHP engine
generates the HTML output, and the web server sends it back to the browser.
r If the script needs to interact with the database—getting the results of a search, or inserting or updating data—the PHP engine communicates with the database server. When the
results come back, the PHP engine puts everything together, and the web server sends the
resulting web page back to the browser.
A lot goes on, but most requests take only a few microseconds, and then the web page is on its
way to the browser with no perceptible delay from the user’s point of view.
PHP’s role ends as soon as the output has been sent to the browser. The time shown by time.php
is fixed; it won’t update automatically a minute later. For that to happen, you need to refresh
the page or create a JavaScript function to change the time within the user’s browser. If you
want PHP to do something in response to user action on a web page, it involves another
round trip to the server. In the past, this meant reloading the page. However, as you’ll see later
in this book, you can refresh the page seamlessly by sending the request in the background
and updating the content with Ajax.


What Is PHP? What Does It Do?

What Is Ajax?
Normally, requests to a web server and the response are handled at the same time.
Waiting for the page to reload is disruptive. As a workaround, you can use JavaScript
to send requests directly to the server and update the content only when the result
comes back. Multiple requests can be sent, updating the page as required. In technical
terms, the process is asynchronous. In other words, the user doesn’t have to wait for the
response from the server to continue using the page. The update happens seamlessly
when the response is received from the server.
Originally, the data was sent back formatted as XML (Extensible Markup Language),
a tag-based language similar to HTML. That’s where the name Ajax comes from,
Asynchronous JavaScript and XML. However, data can be sent back in many formats,
even plain text, so XML is not always part of the process.

Why choose PHP?
PHP isn’t the only server-side technology available. In fact, Dreamweaver has varying degrees
of support for seven. Most have similar capabilities, and choosing which is the most suitable
for your circumstances isn’t always easy. However, PHP has the following advantages:
r PHP runs on Windows, Mac OS X, and Linux. With only a few minor exceptions, code
written on one operating system works on any of the others.
r It’s open source and free.
r It’s widely available.
r It’s relatively easy to learn.
r There’s a large community of active users, so help is rarely far away.
r It’s simple enough to incorporate into a small website, yet powerful enough to drive some
of the busiest websites, including Facebook, Wikipedia, and Yahoo!
r In a survey of media executives by the Society of Digital Agencies (SoDA), nearly 50 percent said they regarded PHP as an important tool for their company in 2010. The figure
for ASP.NET was 32 percent. Fewer than 10 percent said they regarded Ruby on Rails as
important for their company.
r In the same survey, more than 50 percent said they would be hiring people with PHP
skills in 2010. The only web-related skills in greater demand were Flash and ActionScript.

7


8

LESSON : Why PHP and Why Dreamweaver CS5?

So are there any disadvantages in choosing PHP?
Comparing server-side technologies is difficult, but the main disadvantage of PHP is, paradoxically, that it’s easy to learn. Many people copy scripts from online tutorials without understanding the code, often leaving gaping security holes in their websites. PHP is as secure as
any other server-side technology, and security-related bugs are usually dealt with very quickly.
But just like the electricity in your house, it’s safe only insofar as it’s used and maintained
correctly. Fortunately, it’s not difficult to write secure code, and there is emphasis on security
throughout this book.

What Does PHP Stand For?
If you really must know, PHP stands for PHP: Hypertext Preprocessor. Why such a
convoluted mouthful? When PHP’s original creator, Rasmus Lerdorf, released the first
version in 1995, he called it Personal Home Page Tools (PHP Tools). It was a userfriendly set of tools to password protect pages, create forms, and process form data. A
couple of years later, Andi Gutmans and Zeev Suraski—who later founded Zend, the
PHP company—decided to cooperate with Rasmus, and turned PHP into a much more
powerful language. The “personal home page” image no longer fit, but it was decided
to keep the initials PHP. And that’s how the rather clunky name came about. Although
Zend is a commercial enterprise, PHP remains open source and free.
Many PHP developers offer an alternative explanation: PHP = Pretty Happy Programmers.

Which database should I choose?
More often than not, PHP is used in conjunction with MySQL, the most popular open-source
database, which is fast, powerful, and well suited for use in websites. It’s the database that
runs WordPress, Drupal, and Joomla! and is also used by high-traffic websites like Flickr,
Facebook, and YouTube.
MySQL is currently owned by Oracle Corporation, one of the leading database software companies, but the Community Edition of MySQL is free. The functionality of the Community
Edition is identical to the for-purchase Enterprise version. The only difference is that no support is offered with the free version. But that’s rarely a problem because of the active community willing to offer help online.


A Tour of the Main PHP Features in Dreamweaver CS5

Like PHP, MySQL works on all the main operating systems, so you can develop on one system and
later transfer your database to another. Also, most hosting companies offer PHP in combination
with MySQL. For all these reasons, the combination of PHP and MySQL is used in this book.
PHP and MySQL have become so closely connected that many people think you can’t have
one without the other. Unlike love and marriage in Frank Sinatra’s 1950s hit, you can have
PHP without MySQL. PHP works with all the main database systems, including Microsoft
SQL Server, Oracle, and PostgreSQL. You can easily adapt much of the code in later lessons to
work with the database of your choice.
NOTE: Dreamweaver’s built-in PHP server behaviors (covered in Lesson 6) are tied exclusively
to MySQL. Lessons 7–12 use the Zend Framework, which supports many databases in addition to MySQL, including Microsoft SQL Server, SQLite, and PostgreSQL.

A Tour of the Main PHP Features in Dreamweaver CS5
Let’s take a closer look at why Dreamweaver CS5 has become such a good development
environment for beginners and more experienced PHP developers. If you’re completely new
to PHP, it might be best for you to skim the rest of this lesson to get a flavor of what’s in store.
Come back later to read each section in more detail when you’re more familiar with PHP to
learn about particular features.

PHP features also in previous versions
For the benefit of newcomers to Dreamweaver, I’ll describe briefly the basic features inherited from previous versions before moving on to the advanced PHP features that are new to
Dreamweaver CS5. In addition, I’ll explain how to set various options, so existing users will
benefit from some of the information here as well.
Features discussed include:
r Line numbering
r Syntax coloring
r Balancing braces
r Code collapse
r Split Code view
r Applying and removing comment tags
r Live Code

9


10

LESSON : Why PHP and Why Dreamweaver CS5?

Line numbering
When working with PHP scripts, it’s essential to be able to find a specific line, because
PHP error messages always refer to the line where a problem was identified. By default,
Dreamweaver displays line numbers in a column on the left of Code view.
If line numbers are not visible or if you want to turn them off, click the Line Numbers
icon
in the Coding toolbar. Alternatively, choose View > Code View Options > Line
Numbers to toggle them on and off.
TIP: The Coding toolbar is displayed by default on the left of Code view and the Code
Inspector. If you can’t see the Coding toolbar in Code view, choose View > Toolbars > Coding to
turn it back on. The Coding toolbar cannot be turned off in the Code Inspector.
Line numbers refer to new lines created by pressing Enter/Return. By default, Dreamweaver
soft wraps long lines in Code view. If you don’t want Dreamweaver to wrap lines of code like
this, you can toggle the option on and off by clicking the Word Wrap icon
in the Coding
toolbar. Alternatively, choose View > Code View Options > Word Wrap.
NOTE: Previous versions of Dreamweaver had an option to insert a newline character automatically after a specified number of characters (hard wrapping). This option no longer exists in
Dreamweaver CS5.

Syntax coloring
Dreamweaver automatically colors different elements of PHP code to make them easier to
identify. PHP tags and strings (text in quotation marks) are colored red, reserved keywords
are green, functions are dark blue, and variables are a lighter blue. If part of your script is the
wrong color, it’s an almost certain sign that there’s an error in your code; the most common
cause is a missing or mismatched quotation mark.

Setting Dreamweaver Preferences
Many default options can be changed in the Preferences panel, which you can access
from the Edit menu on Windows and the Dreamweaver menu in the Mac version.
You can also open the Preferences panel by pressing Ctrl+U/Cmd+U.
Mac users should note that the conventional Mac shortcut, Cmd+comma (,) is assigned
to a different command (Go to Line).


A Tour of the Main PHP Features in Dreamweaver CS5

NOTE: Dreamweaver syntax coloring doesn’t support PHP heredoc and nowdoc syntax. This
book doesn’t use heredoc or nowdoc syntax. For more details, see http://docs.php.net/manual/
en/language.types.string.php.

If you want to change the default colors used by Dreamweaver, select the Code Coloring
category in the Preferences panel. Select PHP in the Document Type field, and click the Edit
Coloring Scheme button to open the following dialog box.

Don’t be confused that the first item in the “Styles for” list is for a different server-side technology (ColdFusion Script Tag). Just click in the Preview pane at the bottom of the dialog box,
and Dreamweaver automatically highlights the appropriate PHP value in the “Styles for” list.
r Select the type of element you want to change in the “Styles for” list or the Preview pane.
r Click the color box next to “Text color” to select a new color.
r Use bold, italic, or underlined text, or any combination of them to change text.
r “Background color” applies a background color only to the selected type of element.
r To change the background color of Code view, click OK to close the Edit Coloring
Scheme for PHP dialog box, and then click the color box next to “Default background” in
the Code Coloring category in the Preferences panel.
Syntax coloring is turned on by default. It can be toggled on and off by choosing View > Code
View Options > Syntax Coloring.
Dreamweaver CS5 supports PHP syntax coloring and code hints in files that use the following
filename extensions: .php, .php3, .php4, .php5, and .phtml. It also recognizes Smarty templates

11


12

LESSON : Why PHP and Why Dreamweaver CS5?

(%kgc files) but treats them the same way as HTML files. Unless you have a specific reason for
choosing a different filename extension, you should always use .php. However, if you need to
use a filename extension that’s not on the list, you can get Dreamweaver to recognize it by following the instructions found at http://go.adobe.com/kb/ts_tn_16410_en-us.

Balancing braces
Curly braces must always be in matching pairs, but there might be dozens or hundreds of
lines of code between the opening and closing braces. Unlike some other editing programs,
Dreamweaver does not automatically insert a closing brace when you type an opening brace,
but Balance Braces is an indispensible visual guide.
With the insertion point anywhere between two braces, click the Balance Braces icon in the
Coding toolbar to highlight the code enclosed by the braces.

Alternatively, choose Edit > Balance Braces, or press Ctrl+’/Cmd+’ (single quotation mark).

Code collapse
When working on a long script, it’s useful to be able to hide one or more sections of the code
so you can see code that might be far from the section you’re currently working on. To collapse a section of code, select it, and click the minus box at the top or bottom of the selection.
In the Mac version, the minus boxes are replaced by a down triangle at the top and an up
triangle at the bottom. Click either triangle to collapse the selected code.
The collapsed section of code displays just a few characters from the first line in a dark gray
box. Hover your pointer over the gray box, and Dreamweaver displays the first ten lines as a
tooltip to remind you what the collapsed section contains.


Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay

×