Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
PHP Programming With MySQL Second Edition.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
43.07 Mб
Скачать

CHAPTER 6

Manipulating Arrays

Discovery Project 6-3

Create a PHP script with an associative array that uses image file-

names as the keys and image captions as the values. Use the keys and

Values from the array to create a thumbnail gallery of images in which

you display a small hyperlinked image to open a larger version of

the image. Use the zodiac sign images that you saved in your Images

folder in Chapter 1. Set a width and height for the thumbnails that are

less than half of the height and width of the images. Format the dis-

play of the thumbnail gallery.

Save the file as ZodiacGallery.php and upload it to the

ChineseZodiac folder on the Web server. Test the script.

Reopen inc_web_forms.php and add another text link (Image

Gallery) to the text navigation bar that links to the Image Gallery

descriptive text. Below the Alphabetizing Signs descriptive text,

add a new section called “Image Gallery” with descriptive text and

hyperlinks for “[Test the Script]” and “[View the Source Code]” for

ZodiacGallery.php.

378

Discovery Project 6-4

Reopen the BirthYear_switch.php file created in Discovery Project

5-2, and modify the script to create a multidimensional array named

$AnimalSigns[] that stores the information in the following table:

Animal Signs

“Start Date”

“Rat”

“Ox”

“Tiger”

“Rabbit”

“Dragon”

“Snake”

“Horse”

“Goat”

“Monkey”

“Rooster”

“Dog”

“Pig”

1900

1901

1902

1903

1904

1905

1906

1907

1908

1909

1910

1911

“End Date”

2020

2021

2022

2023

2024

2025

2026

2027

2028

2029

2030

2031

“President”

George Washington

Barack Obama

Dwight Eisenhower

John Adams

Abraham Lincoln

John Kennedy

Theodore Roosevelt

James Madison

Harry Truman

Grover Cleveland

George Walker Bush

Ronald Reagan


Discovery Projects

For example, $AnimalSigns["Monkey"] is an array that con-

tains three elements: $AnimalSigns["Monkey"]["Start Date"]

is 1908, $AnimalSigns["Monkey"]["End Date"] is 2028, and

$AnimalSigns["Monkey"]["President"] is “Harry Truman.”

Access the multidimensional array to display the following message

in addition to the message and image displayed by the original script

(text in bold will change depending on the sign being displayed): “If

your Chinese zodiac sign is the Rat, you share a zodiac sign with

President George Washington. Years of the Rat include 1900, 1912,

1924, 1936, 1948, 1960, 1972, 1984, 1996, 2008, and 2020.”

To display the message shown above, you can use the following code.

The variable $ChosenSign contains the name of the animal. The year

string is stored in the $SignMessage variable.

$SignMessage = "If your Chinese zodiac sign is

the $ChosenSign, you share a zodiac sign

with President " .

$AnimalSigns[$ChosenSign]["President"] .

". ";

$SignMessage .= "Years of the $ChosenSign

include ";

for ($i = $AnimalSigns[$ChosenSign]["Start Date"];

$i < $AnimalSigns[$ChosenSign]["End Date"];

$i+=12)

$SignMessage .= $i . ", ";

$SignMessage .= "and " .

$AnimalSigns[$ChosenSign]["End Date"] . ".";

379

Save the BirthYear_switch.php script and upload the file to the

ChineseZodiac folder on the Web server. Open the main page

(index.php) for the Chinese Zodiac site. Click the Control Structures

button or Control Structures text link and select the Switch

Statement link to test the functionality of the script.


CHAPTER

Working with

Databases and

MySQL

In this chapter you will:

Study the basics of databases and MySQL

Work with MySQL databases

Define database tables

Modify user privileges

Work with database records

Work with phpMyAdmin

7

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]