1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5/MySQL Programming- P32 potx

5 249 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 176,77 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Introducing the Word Search Program Creator By the end of this chapter you can create a fun program that generates word search puzzles.. The user enters a series of words into a list box

Trang 1

In this chapter you learn some important skills that improve your work with data You learn about some more-sophisticated ways to work with arrays

and how to manage text information with more flair Specifically, you learn how to do these things:

• Manage arrays with the foreach loop

• Create and use associative arrays

• Extract useful information from some of PHP’s built-in arrays

• Build basic two-dimensional arrays

• Build two-dimensional associative arrays

• Break a string into smaller segments

• Search for one string inside another

Better Arrays

and String Handling

5

C H A P T E R

Trang 2

Introducing the Word

Search Program Creator

By the end of this chapter you can create a fun program that generates word search puzzles The user enters a series of words into a list box, as shown in Figure 5.1

The program then tries to generate a word search based on the user’s word list (It isn’t always possible, but the program can usually generate a puzzle.) One pos-sible solution for the word list shown in Figure 5 1 is demonstrated in Figure 5.2

If desired, the program can also generate an answer key based on the puzzle This capability is shown in Figure 5.3

The secret to the word find game (and indeed most computer programs) is the way the data is handled Once I determined a good scheme for working with the data

in the program, the actual programming wasn’t too tough

134

g r

s o

l u

g in

e r

FIGURE 5.1

The user enters a

list of words and a

size for the finished

puzzle.

Trang 3

Using the foreach Loop

to Work with an Array

that makes it even easier to step through each array element

135

l i

FIGURE 5.2

This puzzle

contains all the

words in the list.

FIGURE 5.3

Here’s the answer

key for the puzzle.

Trang 4

Introducing the foreach.php Program

The HTML page is generated by surprisingly simple code:

<html>

<head>

<title>Foreach Demo</title>

</head>

<body>

<?

$list = array(“alpha”, “beta”, “gamma”, “delta”, “epsilon”);

print “<ul>\n”;

foreach ($list as $value){

print “ <li>$value</li>\n”;

} // end foreach

print “</ul>\n”;

?>

</body>

</html>

136

g r

s o

l u

g in

e r

FIGURE 5.4

Although it looks

just like normal

HTML, this page

was created with

an array and a

foreach loop.

Trang 5

All the values in the list are created in the $listvariable using the arrayfunction.

foreachloop steps through the $listarray as many times as necessary Each time

foreach ($list as $value){

print “ <li>$value</li>\n”;

} // end foreach

for ($i = 0; $i < length($list); $i++);

$value = $list[$i];

print “ <li>$value</li>\n”;

} // end for loop

The main difference between a foreach loop and a for loop is the presence of the index variable ($i in this example) If you’re using a foreach loop and need

to know the current element’s index, use the key() function.

value of an array Since this is a common task, knowing how to use the foreach

loop is an important skill As you learn some other kinds of arrays, you see how

Creating an Associative Array

PHP is known for its extremely flexible arrays You can easily generate a number

of interesting and useful array types in addition to the ordinary arrays you’ve

already made One of the handiest types is called an associative array

While regular arrays rely on numeric indices, an associative array has a string

index Figure 5.5 shows a page created with two associative arrays

T R I C K

137

l i

Ngày đăng: 07/07/2014, 02:20