Tải bản đầy đủ (.pdf) (73 trang)

Sams Teach Yourself PHP, MySQL and Apache in 24 Hours phần 6 pot

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (4.34 MB, 73 trang )

print "$membership<br>"; // prints "mz02xyz"
PHP also provides a case function that has a useful cosmetic purpose. The ucwords() function
makes the first letter of every word in a string uppercase. In the following snippet, we make the first
letter of every word in a user-submitted string uppercase:
$full_name = "violet elizabeth bott";
$full_name = ucwords($full_name);
print $full_name; // prints "Violet Elizabeth Bott"
Although this function makes the first letter of each word uppercase, it does not touch any other
letters. So if the user had had problems with her Shift key in the previous example and submitted
VIolEt eLIZaBeTH bOTt , our approach would not have done much to fix the string. We would
have ended up with VIolEt ELIZaBeTH BOTt , which isn't much of an improvement. We can
deal with this problem by making the submitted string lowercase with strtolower() before
invoking ucwords() :
$full_name = "VIolEt eLIZaBeTH bOTt";
$full_name = ucwords(strtolower($full_name));
print $full_name; // prints "Violet Elizabeth Bott"
Wrapping Text with wordwrap() and nl2br()
When you present plain text within a Web page, you are often faced with the problem that newlines
are not displayed, and your text runs together into a featureless blob. The nl2br() function is a
convenience method that converts every newline into an HTML break. So
$string = "one line\n";
$string .= "another line\n";
$string .= "a third for luck\n";
print nl2br($string);
will print
one line<br />
another line<br />
a third for luck<br />
Notice that the <br> tags are output in XHTML-compliant form. This feature was introduced in PHP
4.0.5.
The nl2br() function is great for honoring newlines that are already in the text you are converting.


Occasionally, though, you may want to add arbitrary line breaks to format a column of text. The
wordwrap() function is perfect for this task. wordwrap() requires one argument: the string to
be transformed. By default, wordwrap() wraps lines every 75 characters and uses '\n ' as its line
break character. So the code snippet
$string = "Given a long line, wordwrap() is useful as a means of ";
$string .= "breaking it into a column and thereby making it easier to read";
print wordwrap($string);
would output
Given a long line, wordwrap() is useful as a means of breaking it into a
column and thereby making it easier to read
Because the lines are broken with the character '\n ', the formatting does not show up in HTML
mode, of course. wordwrap() has two more optional arguments: a number representing the
maximum number of characters per line and a string representing the end of line string you would
like to use. So applying the function call
print wordwrap( $string, 24, "<br>\n" );
to our $string variable, our output would be
Given a long line,<br>
wordwrap() is useful as<br>
a means of breaking it<br>
into a column and<br>
thereby making it easier<br>
to read
The wordwrap() function doesn't automatically break at your line limit if a word has more
characters than the limit. You can, however, use an optional fourth argument to enforce such a
break. The argument should be a positive integer. So using wordwrap() in conjunction with the
fourth argument, we can now wrap a string, even where it contains words that extend beyond the
limit we are setting. This snippet
$string = "As usual you will find me at />$string .= "chat/eating_green_cheese/forum.php. Hope to see you there!";
print wordwrap( $string, 24, "<br>\n", 1 );
will output

As usual you will find<br>
me at<br>
teringonab<br>
outit.com/chat/eating_gr<br>
een_cheese/forum.php.<br>
Hope to see you there!
instead of
As usual you will find<br>
me at<br>
<br>
Hope to see you there!
Breaking Strings into Arrays with explode()
The delightfully named explode() function is similar in some ways to strtok() . But
explode() will break up a string into an array, which you can then store, sort, or examine as you
want. The explode() function requires two arguments: the delimiter string that you want to use to
break up the source string and the source string itself. The function optionally accepts a third
argument, which will determine the maximum number of pieces the string can be broken into. The
delimiter string can include more than one character, all of which will form a single delimiter (unlike
multiple delimiter characters passed to strtok() , each of which will be a delimiter in its own
right). The following snippet breaks up a date and stores the result in an array:
$start_date = "2002-01-12";
$date_array = explode("-", $start_date);
// $date[0] == "2002"
// $date[1] == "01"
// $date[2] == "12"
Now that your head is filled with PHP string functions, let's move on to MySQL string functions, many
of which perform the same tasks.
[ Team LiB ]

[ Team LiB ]


Frequently Used String Functions in MySQL
MySQL's built-in string-related functions can be used several ways. You can use functions in SELECT
statements without specifying a table to retrieve a result of the function. Or you can use functions to
enhance your SELECT results by concatenating two fields to form a new string.
Even if you never use these functions in your applications, it's good to know they exist, and, if
nothing else, you'll get some good practice in this hour using the MySQL monitor's command-line
interface.
Length and Concatenation Functions
The group of length and concatenation functions focuses on the length of strings and concatenating
strings together. Length-related functions include LENGTH() , OCTET_LENGTH() ,
CHAR_LENGTH() , and CHARACTER_LENGTH() , which do virtually the same thing—count
characters in a string.
mysql> select length('This is cool!');
+ +
| LENGTH('This is cool!') |
+ +
| 13 |
+ +
1 row in set (0.00 sec)
The fun begins with the CONCAT() function, which is used to concatenate two or more strings:
mysql> select concat('My', 'S', 'QL');
+ +
| CONCAT('My', 'S', 'QL') |
+ +
| MySQL |
+ +
1 row in set (0.00 sec)
Imagine using this function with a table containing names, split into firstname and lastname
fields. Instead of using two strings, use two field names to concatenate the firstname and the

lastname fields. By concatenating the fields, you reduce the lines of code necessary to achieve the
same result in your application:
mysql> select concat(firstname, lastname) from table_name;
+ +
| CONCAT(firstname, lastname) |
+ +
| JohnSmith |
| JaneSmith |
| JimboJones |
| AndySmith |
| ChrisJones |
| AnnaBell |
| JimmyCarr |
| AlbertSmith |
| JohnDoe |
+ +
9 rows in set (0.00 sec)
If you're using a field name and not a string in a function, don't enclose the field
name within quotation marks. If you do, MySQL will interpret the string literally. In
the CONCAT() example, you would get the following result:
mysql> select concat('firstname', 'lastname') FROM table_name;
+ +
| CONCAT('firstname', 'lastname') |
+ +
| firstnamelastname |
| firstnamelastname |
| firstnamelastname |
| firstnamelastname |
| firstnamelastname |
| firstnamelastname |

| firstnamelastname |
| firstnamelastname |
| firstnamelastname |
+ +
9 rows in set (0.00 sec)
The CONCAT() function would be useful if there were some sort of separator between the names,
and that's where the next function comes in: CONCAT_WS() .
As you may have figured out, CONTACT_WS() stands for "concatenate with separator." The
separator can be anything you choose, but the following example uses whitespace:
mysql> select concat_ws(' ', firstname, lastname) FROM table_name;
+ +
| CONCAT_WS(' ', firstname, lastname) |
+ +
| John Smith |
| Jane Smith |
| Jimbo Jones |
| Andy Smith |
| Chris Jones |
| Anna Bell |
| Jimmy Carr |
| Albert Smith |
| John Doe |
+ +
9 rows in set (0.00 sec)
If you want to shorten the width of your result table, you can use AS to name the custom result field:
mysql> select concat_ws(' ', firstname, lastname) AS fullname FROM table_name;
+ +
| fullname |
+ +
| John Smith |

| Jane Smith |
| Jimbo Jones |
| Andy Smith |
| Chris Jones |
| Anna Bell |
| Jimmy Carr |
| Albert Smith |
| John Doe |
+ +
9 rows in set (0.00 sec)
Trimming and Padding Functions
MySQL provides several functions for adding and removing extra characters (including whitespace)
from strings. The RTRIM() and LTRIM() functions remove whitespace from either the right or left
side of a string:
mysql> select rtrim('stringstring ');
+ +
| RTRIM('stringstring ') |
+ +
| stringstring |
+ +
1 row in set (0.00 sec)
mysql> select ltrim(' stringstring');
+ +
| LTRIM(' stringstring') |
+ +
| stringstring |
+ +
1 row in set (0.00 sec)
You may have padded strings to trim if the string is coming out of a fixed-width field, and either
doesn't need to carry along the additional padding or is being inserted into a varchar or other

non–fixed-width field. If your strings are padded with a character besides whitespace, use the
TRIM() function to name the characters you want to remove. For example, to remove the leading
"X" characters from the string XXXneedleXXX , use
mysql> select trim(leading 'X' from 'XXXneedleXXX');
+ +
| TRIM(LEADING 'X' from 'XXXneedleXXX') |
+ +
| needleXXX |
+ +
1 row in set (0.00 sec)
Use TRAILING to remove the characters from the end of the string:
mysql> select trim(trailing 'X' from 'XXXneedleXXX');
+ +
| TRIM(TRAILING 'X' from 'XXXneedleXXX') |
+ +
| XXXneedle |
+ +
1 row in set (0.00 sec)
If neither LEADING nor TRAILING is indicated, both are assumed:
mysql> select trim('X' from 'XXXneedleXXX');
+ +
| TRIM('X' from 'XXXneedleXXX') |
+ +
| needle |
+ +
1 row in set (0.00 sec)
Just like RTRIM() and LTRIM() remove padding characters, RPAD() and LPAD() add
characters to a string. For example, you may want to add specific identification characters to a string
that is part of an order number, in a database used for sales. When you use the padding functions,
the required elements are the string, the target length, and the padding character. For example, pad

the string needle with the X character until the string is 10 characters long:
mysql> select rpad('needle', 10, 'X');
+ +
| RPAD('needle', 10, 'X') |
+ +
| needleXXXX |
+ +
1 row in set (0.00 sec)
mysql> select lpad('needle', 10, 'X');
+ +
| LPAD('needle', 10, 'X') |
+ +
| XXXXneedle |
+ +
1 row in set (0.00 sec)
Location and Position Functions
The group of location and position functions is useful for finding parts of strings within other strings.
The LOCATE() function returns the position of the first occurrence of a given substring within the
target string. For example, you can look for a needle in a haystack:
mysql> select locate('needle', 'haystackneedlehaystack');
+ +
| LOCATE('needle', 'haystackneedlehaystack') |
+ +
| 9 |
+ +
1 row in set (0.00 sec)
The substring needle begins at position 9 in the target string. If the substring cannot be found in the
target string, MySQL returns 0 as a result.
Unlike position counting within most programming languages, which starts at
0, position counting using MySQL starts at 1.

An extension of the LOCATE() function is to use a third argument for starting position. If you start
looking for needle in haystack before position 9, you'll receive a result. Otherwise, because
needle starts at position 9, you'll receive a 0 result if you specify a greater starting position:
mysql> select locate('needle', 'haystackneedlehaystack',6);
+ +
| LOCATE('needle', 'haystackneedlehaystack',9) |
+ +
| 9 |
+ +
1 row in set (0.00 sec)
mysql> select locate('needle', 'haystackneedlehaystack',12);
+ +
| LOCATE('needle', 'haystackneedlehaystack',12) |
+ +
| 0 |
+ +
1 row in set (0.00 sec)
Substring Functions
If your goal is to extract a substring from a target string, several functions fit the bill. Given a string,
starting position, and length, you can use the SUBSTRING() function. This example gets three
characters from the string MySQL , starting at position 2:
mysql> select substring("MySQL", 2, 3);
+ +
| SUBSTRING("MySQL", 2, 3) |
+ +
| ySQ |
+ +
1 row in set (0.00 sec)
If you just want a few characters from the left or right ends of a string, use the LEFT() and
RIGHT() functions:

mysql> select left("MySQL", 2);
+ +
| LEFT("MySQL", 2) |
+ +
| My |
+ +
1 row in set (0.00 sec)
mysql> select right("MySQL", 3);
+ +
| RIGHT("MySQL", 3) |
+ +
| SQL |
+ +
1 row in set (0.00 sec)
One of the many common uses of substring functions is to extract parts of order numbers, to find out
who placed the order. In some applications, the system is designed to automatically generate an
order number, containing a date, customer identification, and other information. If this order number
always follows a particular pattern, such as XXXX-YYYYY-ZZ , you can use substring functions to
extract the individual parts of the whole. For example, if ZZ always represents the state to which the
order was shipped, you can use the RIGHT() function to extract these characters and report the
number of orders shipped to a particular state.
String Modification Functions
Your programming language of choice likely has functions to modify the appearance of strings, but if
you can perform the task as part of the SQL statement, all the better.
The MySQL LCASE() and UCASE() functions transform a string into lowercase or uppercase:
mysql> select lcase('MYSQL');
+ +
| LCASE(' MYSQL') |
+ +
| mysql |

+ +
1 row in set (0.00 sec)
mysql> select ucase('mysql');
+ +
| UCASE(' mysql') |
+ +
| MYSQL |
+ +
1 row in set (0.00 sec)
Remember, if you use the functions with field names, don't use quotation marks:
mysql> select ucase(lastname) from table_name;
+ +
| UCASE(lastname) |
+ +
| BELL |
| CARR |
| DOE |
| JONES |
| JONES |
| SMITH |
| SMITH |
| SMITH |
| SMITH |
+ +
9 rows in set (0.00 sec)
Another fun string-manipulation function is REPEAT() , which does just what it sounds like—repeats
a string for a given number of times:
mysql> select repeat("bowwow", 4);
+ +
| REPEAT("bowwow", 4) |

+ +
| bowwowbowwowbowwowbowwow |
+ +
1 row in set (0.00 sec)
The REPLACE() function replaces all occurrences of a given string with another string:
mysql> select replace('bowwowbowwowbowwowbowwow', 'wow', 'WOW');
+ +
| REPLACE('bowwowbowwowbowwowbowwow', 'wow', 'WOW') |
+ +
| bowWOWbowWOWbowWOWbowWOW |
+ +
1 row in set (0.00 sec)
Obscure String Functions
The group of obscure string functions focuses on gathering more information about characters or
converting characters to different bases—far and away the least common usage of string functions in
MySQL, but important nonetheless if you're into such things. The first function is the ASCII()
function, which gets the ASCII code value of a given character. This example gets the ASCII value of
the ampersand (& ) character:
mysql> select ascii('&');
+ +
| ASCII('&') |
+ +
| 38 |
+ +
1 row in set (0.04 sec)
If a string contains multiple characters, the function gets the value of the left-most character:
mysql> select ascii('def');
+ +
| ASCII('def') |
+ +

| 100 |
+ +
1 row in set (0.00 sec)
In this case, "100" is the ASCII value of "d."
The next three functions return string representations of binary, octal, and hexadecimal values. Like
the ASCII() function, the BIN() , OCT() , and HEX() functions do not require a table selection
but return values without a specified table.
The following example gets a string representation of the binary value of the integer 56895:
mysql> select bin(56895);
+ +
| BIN(56895) |
+ +
| 1101111000111111 |
+ +
1 row in set (0.00 sec)
The following example gets a string representation of the octal value of the integer 56895:
mysql> select oct(56895);
+ +
| OCT(56895) |
+ +
| 157077 |
+ +
1 row in set (0.00 sec)
The following example gets a string representation of the hexadecimal value of the integer 56895:
mysql> select hex(56895);
+ +
| HEX(56895) |
+ +
| DE3F |
+ +

1 row in set (0.00 sec)
You can also use the CONV() function to convert numbers between bases. This function has three
parts: the number, the base you're converting from, and the base you're converting to.
For example, to convert the integer 56895 from base 10 to base 8 and return its value, use
mysql> select conv(56895,10,8);
+ +
| CONV(56895,10,8) |
+ +
| 157077 |
+ +
1 row in set (0.00 sec)
This result is equivalent to the OCT() function. Similarly, to convert an integer from base 10 to base
16, use
mysql> select conv(56895,10,16);
+ +
| CONV(56895,10,16) |
+ +
| DE3F |
+ +
1 row in set (0.00 sec)
This result is equivalent to the HEX() function.
You can also convert from base 8 to base 16:
mysql> select conv(157077,8,16);
+ +
| CONV(157077,8,16) |
+ +
| DE3F |
+ +
1 row in set (0.00 sec)
And so on. The minimum base is 2 and the maximum base is 36.

Another function for working with characters and ASCII codes is the CHAR() function, which takes a
series of integers representing ASCII codes and returns a string made up of the results:
mysql> select char(84,104,105,115,32,105,115,32,99,111,111,108,33);
+ +
| CHAR(84,104,105,115,32,105,115,32,99,111,111,108,33) |
+ +
| This is cool! |
+ +
1 row in set (0.00 sec)
[ Team LiB ]

[ Team LiB ]

Summary
In this hour, you examined some of the functions that enable you to take control of the strings in
your PHP scripts. You learned how to format strings with printf() and sprint(). You should be
able to use these functions both to create strings that transform data and lay it out. You learned
about functions that investigate strings. You should be able to discover the length of a string with
strlen(), determine the presence of a substring with strpos(), or extract a substring with
substr(). You should be able to tokenize a string with strtok().
You also learned about functions that transform strings. You can now remove whitespace from the
beginning or end of a string with trim(), ltrim(), or rtrim(). You can change case with
strtoupper(), strtolower(), or ucwords(). You can replace all instances of a string with
str_replace().
After learning the PHP methods for string manipulation, you were introduced to MySQL functions that
perform actions on strings. If you have strings in MySQL you want to concatenate or for which you
want to count characters, you can use functions such as CONCAT(), CONCAT_WS(), and
LENGTH(). To pad or remove padding from strings, use RPAD(), LPAD(), TRIM(), LTRIM(),
and RRIM() to get just the strings you want. You can also find the location of a string within
another, or to return a part of a given string, using the LOCATE(), SUBSTRING(), LEFT(), and

RIGHT() functions. Functions such as LCASE(), UCASE(), REPEAT(), and REPLACE() also
return variations of the original strings. MySQL also has numerous functions for representing strings,
such as ASCII(), BIN(), OCT(), HEX(), and CONV() for converting between bases.
[ Team LiB ]

[ Team LiB ]

Q&A
Q1:
Are there any other string functions that might be useful to me?
A1:
Yes. PHP has more than 60 string functions! You can read about them all in the PHP Manual online at
.
Q2:
Can I use multiple functions in one statement, such as making a concatenated string all
uppercase?
A2:
Sure—just be mindful of your opening and closing parentheses. This example shows how to
uppercase the concatenated first and last names from the master name table:
mysql> SELECT UCASE(CONCAT_WS(' ', firstname, lastname)) FROM table_name;
+ +
| UCASE(CONCAT_WS(' ', firstname, lastname)) |
+ +
| JOHN SMITH |
| JANE SMITH |
| JIMBO JONES |
| ANDY SMITH |
| CHRIS JONES |
| ANNA BELL |
| JIMMY CARR |

| ALBERT SMITH |
| JOHN DOE |
+ +
9 rows in set (0.00 sec)
If you want to uppercase just the last name, use
mysql> SELECT CONCAT_WS(' ', firstname, UCASE(lastname)) FROM master_name;
+ +
| CONCAT_WS(' ', firstname, UCASE(lastname)) |
+ +
| John SMITH |
| Jane SMITH |
| Jimbo JONES |
| Andy SMITH |
| Chris JONES |
| Anna BELL |
| Jimmy CARR |
| Albert SMITH |
| John DOE |
+ +
9 rows in set (0.00 sec)
[ Team LiB ]

[ Team LiB ]

Workshop
The workshop is designed to help you anticipate possible questions, review what you've learned, and
begin learning how to put your knowledge into practice.
Quiz
1:
What conversion specifier would you use with printf() to format an integer as a

double? Indicate the full syntax required to convert the integer 33.
A1:
You use the conversion specifier f to format an integer as a double:
printf("%f", 33);
2:
How would you pad the conversion you effected in question 1 with zeros so that the part
before the decimal point is four characters long?
A2:
You can pad the output from printf() with the padding specifier—that is, a space or a
zero followed by a number representing the number of characters you want to pad by.
printf("%04f", 33);
3:
How would you specify a precision of two decimal places for the floating-point number you
have been formatting in the previous questions?
A3:
The precision specifier consists of a dot (.) followed by a number representing the
precision you want to apply. It should be placed before the conversion specifier:
printf("%04.2f", 33);
4:
What function would you use to extract a substring from a string?
A4:
The substr() function extracts and returns a substring.
5:
How might you remove whitespace from the beginning of a string?
A5:
The ltrim() function removes whitespace from the start of a string.
6:
How would you break up a delimited string into an array of substrings?
A6:
The explode() function splits up a string into an array.

7:
Write an SQL query to find the starting position of a substring "grape" in a string
"applepearbananagrape".
A7:
SELECT LOCATE('grape', 'applepearbananagrape');
8:
Write a query that selects the last 5 characters from the string
"applepearbananagrape".
A8:
SELECT RIGHT("applepearbananagrape", 5);
Activities
Create a feedback form that accepts a user's full name and an email address. Use case
conversion functions to capitalize the first letter of each name the user submits and print the
result back to the browser. Check that the user's email address contains the @ symbol and print
a warning otherwise.
1.
Create an array of doubles and integers. Loop through the array converting each element to a
floating-point number with a precision of 2. Right-align the output within a field of 20
characters.
2.
Using both PHP and MySQL, practice using functions within functions, such as making case
changes on substrings and concatenating strings.
3.
[ Team LiB ]

[ Team LiB ]

Hour 14. Creating a Simple Discussion
Forum
Although the ultimate goal of this hour is to create a simple discussion forum, a majority of the hour

is devoted to understanding the thought processes behind designing an application using a relational
database.
In this hour, you will learn
Three types of table relationships
How to normalize your database
How to implement a good database design process
How to create the tables, input forms, and display of a simple discussion forum
[ Team LiB ]


One-to-One Relationships
In a one-to-one relationship, a key appears only once in a related table. The employees and
departments tables do not have a one-to-one relationship because many employees undoubtedly
belong to the same department. A one-to-one relationship exists, for example, if each employee is
assigned one computer within a company. Figure 14.2 shows the one-to-one relationship of
employees to computers.
Figure 14.2. One computer is assigned to each employee.
The employees and computers tables in your database would look something like Figure 14.3,
which represents a one-to-one relationship.
Figure 14.3. One-to-one relationship in the data model.
One-to-Many Relationships
In a one-to-many relationship, keys from one table appear multiple times in a related table. The
example shown in Figure 14.1, indicating a connection between employees and departments,
illustrates a one-to-many relationship. A real-world example would be an organizational chart of the
department, as shown in Figure 14.4.
Figure 14.4. One department contains many employees.
The one-to-many relationship is the most common type of relationship. Another practical example is
the use of a state abbreviation in an address database; each state has a unique identifier (CA for
California, PA for Pennsylvania, and so on), and each address in the United States has a state
associated with it.

If you have eight friends in California and five in Pennsylvania, you will use only two distinct
abbreviations in your table. One abbreviation represents a one-to-eight relationship (CA), and the
other represents a one-to-five (PA) relationship.
Many-to-Many Relationships
The many-to-many relationship often causes problems in practical examples of normalized
databases, so much so that it is common to simply break many-to-many relationships into a series of
one-to-many relationships. In a many-to-many relationship, the key value of one table can appear
many times in a related table. So far, it sounds like a one-to-many relationship, but here's the
curveball: The opposite is also true, meaning that the primary key from that second table can also
appear many times in the first table.
Think of such a relationship this way, using the example of students and classes. A student has an ID
and a name. A class has an ID and a name. A student usually takes more than one class at a time,
and a class always contains more than one student, as you can see in Figure 14.5.
Figure 14.5. Students take classes, classes contain students.
As you can see, this sort of relationship doesn't present an easy method for relating tables. Your
tables could look like Figure 14.6, seemingly unrelated.
Figure 14.6. The students table and the classes table, unrelated.

×