If your company has a consistent look and feel to pages on the Web site, you can use
PHP to add the template and standard elements to pages using require().
For example, the Web site of fictional company TLA Consulting has a number of
pages all with the look and feel shown in Figure 5.2.When a new page is needed, the
developer can open an existing page, cut out the existing text from the middle of the
file, enter new text and save the file under a new name.
Consider this scenario:The Web site has been around for a while, and there are now
tens, hundreds, or maybe even thousands of pages all following a common style.A decision
is made to change part of the standard look—it might be something minor, like
adding an email address to the footer of each page or adding a single new entry to the
navigation menu. Do you want to make that minor change on tens, hundreds, or even
thousands of pages?
Directly reusing the sections of HTML that are common to all pages is a much better
approach than cutting and pasting on tens, hundreds, or even thousands of pages.
Saturday, June 13, 2009
Thursday, June 11, 2009
The Basic Steps in Querying a Database from
In any script used to access a database from the Web, you will follow some basic steps:
1. Check and filter data coming from the user.
2. Set up a connection to the appropriate database.
3. Query the database.
4. Retrieve the results.
5. Present the results back to the user.
These are the steps we have followed in the script results.php, and we will go through
each of them in turn.
1. Check and filter data coming from the user.
2. Set up a connection to the appropriate database.
3. Query the database.
4. Retrieve the results.
5. Present the results back to the user.
These are the steps we have followed in the script results.php, and we will go through
each of them in turn.
How Web Database Architectures Work
In Chapter 7,“Designing Your Web Database,” we outlined how Web database architectures
work. Just to remind you, here are the steps again:
1. A user’s Web browser issues an HTTP request for a particular Web page. For example,
the user might have requested a search for all the books written by Michael
Morgan at Book-O-Rama, using an HTML form.The search results page is called
results.php.
2. TheWeb server receives the request for results.php, retrieves the file, and passes it
to the PHP engine for processing.
3. The PHP engine begins parsing the script. Inside the script is a command to connect
to the database and execute a query (perform the search for books). PHP
opens a connection to the MySQL server and sends on the appropriate query.
4. The MySQL server receives the database query, processes it, and sends the results—
a list of books—back to the PHP engine.
5. The PHP engine finishes running the script that will usually involve formatting
the query results nicely in HTML. It then returns the resulting HTML to the Web
server.
6. The Web server passes the HTML back to the br
work. Just to remind you, here are the steps again:
1. A user’s Web browser issues an HTTP request for a particular Web page. For example,
the user might have requested a search for all the books written by Michael
Morgan at Book-O-Rama, using an HTML form.The search results page is called
results.php.
2. TheWeb server receives the request for results.php, retrieves the file, and passes it
to the PHP engine for processing.
3. The PHP engine begins parsing the script. Inside the script is a command to connect
to the database and execute a query (perform the search for books). PHP
opens a connection to the MySQL server and sends on the appropriate query.
4. The MySQL server receives the database query, processes it, and sends the results—
a list of books—back to the PHP engine.
5. The PHP engine finishes running the script that will usually involve formatting
the query results nicely in HTML. It then returns the resulting HTML to the Web
server.
6. The Web server passes the HTML back to the br
Wednesday, June 10, 2009
Formatting Strings for Storage: AddSlashes() and StripSlashes()
As well as using string functions to reformat a string visually, we can use some of these
functions to reformat strings for storage in a database.Although we won’t cover actually
writing to the database until Part II,“Using MySQL,” we will cover formatting strings
for database storage now.
Certain characters are perfectly valid as part of a string but can cause problems, particularly
when inserting data into a database because the database could interpret these
102 Chapter 4 String Manipulation and Regular Expressions
characters as control characters.The problematic ones are quotes (single and double),
backslashes (\), and the NUL character.
We need to find a way of marking or escaping these characters so that databases such
as MySQL can understand that we meant a literal special character rather than a control
sequence.To escape these characters, add a backslash in front of them. For example, "
(double quote) becomes \" (backslash double quote), and \ (backslash) becomes \\
(backslash backslash). (This rule applies universally to special characters, so if you have \\
in your string, you need to replace it with \\\\.)
functions to reformat strings for storage in a database.Although we won’t cover actually
writing to the database until Part II,“Using MySQL,” we will cover formatting strings
for database storage now.
Certain characters are perfectly valid as part of a string but can cause problems, particularly
when inserting data into a database because the database could interpret these
102 Chapter 4 String Manipulation and Regular Expressions
characters as control characters.The problematic ones are quotes (single and double),
backslashes (\), and the NUL character.
We need to find a way of marking or escaping these characters so that databases such
as MySQL can understand that we meant a literal special character rather than a control
sequence.To escape these characters, add a backslash in front of them. For example, "
(double quote) becomes \" (backslash double quote), and \ (backslash) becomes \\
(backslash backslash). (This rule applies universally to special characters, so if you have \\
in your string, you need to replace it with \\\\.)
Smart Form Mail
In this chapter, we’ll look at string and regular expression functions in the context of a
Smart Form Mail application.We’ll add these scripts to the Bob’s Auto Parts site we’ve
been looking at in the last few chapters.
This time, we’ll build a straightforward and commonly used customer feedback form
for Bob’s customers to enter their complaints and compliments, as shown in Figure 4.1.
However, our application will have one improvement over many you will find on the
Web. Instead of emailing the form to a generic email address like feedback@example.
com, we’ll attempt to put some intelligence into the process by searching the input for
96 Chapter 4 String Manipulation and Regular Expressions
key words and phrases and then sending the email to the appropriate employee at Bob’s
company. For example, if the email contains the word “advertising,” we might send the
feedback to the Marketing department. If the email is from Bob’s biggest client, it can go
straight to Bob.
Smart Form Mail application.We’ll add these scripts to the Bob’s Auto Parts site we’ve
been looking at in the last few chapters.
This time, we’ll build a straightforward and commonly used customer feedback form
for Bob’s customers to enter their complaints and compliments, as shown in Figure 4.1.
However, our application will have one improvement over many you will find on the
Web. Instead of emailing the form to a generic email address like feedback@example.
com, we’ll attempt to put some intelligence into the process by searching the input for
96 Chapter 4 String Manipulation and Regular Expressions
key words and phrases and then sending the email to the appropriate employee at Bob’s
company. For example, if the email contains the word “advertising,” we might send the
feedback to the Marketing department. If the email is from Bob’s biggest client, it can go
straight to Bob.
Tuesday, June 9, 2009
Formatting Strings for Storage:
As well as using string functions to reformat a string visually, we can use some of these
functions to reformat strings for storage in a database.Although we won’t cover actually
writing to the database until Part II,“Using MySQL,” we will cover formatting strings
for database storage now.
Certain characters are perfectly valid as part of a string but can cause problems, particularly
when inserting data into a database because the database could interpret these
102 Chapter 4 String Manipulation and Regular Expressions
characters as control characters.
functions to reformat strings for storage in a database.Although we won’t cover actually
writing to the database until Part II,“Using MySQL,” we will cover formatting strings
for database storage now.
Certain characters are perfectly valid as part of a string but can cause problems, particularly
when inserting data into a database because the database could interpret these
102 Chapter 4 String Manipulation and Regular Expressions
characters as control characters.
Formatting Strings
The first step in tidying up is to trim any excess whitespace from the string. Although
this is never compulsory, it can be useful if you are going to store the string in a file or
database, or if you’re going to compare it to other strings.
PHP provides three useful functions for this purpose.We’ll use the trim() function
to tidy up our input data as follows:
this is never compulsory, it can be useful if you are going to store the string in a file or
database, or if you’re going to compare it to other strings.
PHP provides three useful functions for this purpose.We’ll use the trim() function
to tidy up our input data as follows:
Smart Form Mail
In this chapter, we’ll look at string and regular expression functions in the context of a
Smart Form Mail application.We’ll add these scripts to the Bob’s Auto Parts site we’ve
been looking at in the last few chapters.
This time, we’ll build a straightforward and commonly used customer feedback form
for Bob’s customers to enter their complaints and compliments, as shown in Figure 4.1.
However, our application will have one improvement over many you will find on the
Web. Instead of emailing the form to a generic email address like feedback@example.
com, we’ll attempt to put some intelligence into the process by searching the input for
96 Chapter 4 String Manipulation and Regular Expressions
key words and phrases and then sending the email to the appropriate employee at Bob’s
company. For example, if the email contains the word “advertising,” we might send the
feedback to the Marketing department. If the email is from Bob’s biggest client, it can go
straight to Bob.
Figure 4.1 Bob’s feedback form asks customers for
their name, email address, and comments.
We’ll start with the simple script shown in Listing 4.1 and add to it as we go along.
Listing 4.1 processfeedback.php—Basic Script to Email Forms Contents
Smart Form Mail application.We’ll add these scripts to the Bob’s Auto Parts site we’ve
been looking at in the last few chapters.
This time, we’ll build a straightforward and commonly used customer feedback form
for Bob’s customers to enter their complaints and compliments, as shown in Figure 4.1.
However, our application will have one improvement over many you will find on the
Web. Instead of emailing the form to a generic email address like feedback@example.
com, we’ll attempt to put some intelligence into the process by searching the input for
96 Chapter 4 String Manipulation and Regular Expressions
key words and phrases and then sending the email to the appropriate employee at Bob’s
company. For example, if the email contains the word “advertising,” we might send the
feedback to the Marketing department. If the email is from Bob’s biggest client, it can go
straight to Bob.
Figure 4.1 Bob’s feedback form asks customers for
their name, email address, and comments.
We’ll start with the simple script shown in Listing 4.1 and add to it as we go along.
Listing 4.1 processfeedback.php—Basic Script to Email Forms Contents
String Manipulation
IN THIS CHAPTER,WE’LL DISCUSS HOW you can use PHP’s string functions to format and
manipulate text.We’ll also discuss using string functions or regular expression functions
to search (and replace) words, phrases, or other patterns within a string.
These functions are useful in many contexts.You’ll often want to clean up or reformat
user input that is going to be stored in a database. Search functions are great when
building search engine applications (among other things).
In this chapter, we will cover
n Formatting strings
n Joining and splitting strings
n Comparing strings
n Matching and replacing substrings with string functions
n Using regular expressions
manipulate text.We’ll also discuss using string functions or regular expression functions
to search (and replace) words, phrases, or other patterns within a string.
These functions are useful in many contexts.You’ll often want to clean up or reformat
user input that is going to be stored in a database. Search functions are great when
building search engine applications (among other things).
In this chapter, we will cover
n Formatting strings
n Joining and splitting strings
n Comparing strings
n Matching and replacing substrings with string functions
n Using regular expressions
Subscribe to:
Posts (Atom)