Cơ sở dữ liệu APACHE, PHP, MYSQL - 6

echo " movies are:"; echo "<br>"; listmovies_1();

if ($_REQUEST['movienum'] == 10) listmovies_2();

//end of deleted lines*/

foreach ($favmovies as $currentvalue)

{

echo $currentvalue; echo "<br>n";

}

}

?>

</body>

Có thể bạn quan tâm!

Xem toàn bộ 258 trang tài liệu này.

</html>


2/ Thay đổi movie1.php như sau:

<?php

session_start();

$_SESSION['username'] = $_POST['user'];

$_SESSION['userpass'] = $_POST['pass'];

$_SESSION['authuser'] = 0;

// kiểm tra username và password

if (($_SESSION['username'] == 'Joe') and ($_SESSION['userpass'] == '12345'))

{

$_SESSION['authuser'] = 1;

} else {

echo "Sorry, but you don't have permission to view this page, you loser!";

exit();

}

?>

<html>

<head>

<title>Find my Favorite Movie!</title>

</head>

<body>

<?php include "header.php"; ?>

<?php

$myfavmovie = urlencode("Life of Brian");

echo "<a href='moviesite.php?favmovie=$myfavmovie'>"; echo "Click here to see information about my favorite movie!"; echo "</a>";

echo "<br>";

/*/delete these lines

echo "<a href='moviesite.php?movienum=5'>"; echo "Click here to see my top 5 movies."; echo "</a>";

echo "<br>";

//end of deleted lines*/

//change the following line:

echo "<a href='moviesite.php'>";

echo "Click here to see my top 10 movies."; echo "</a>";

echo "<br>";

echo "<a href='moviesite.php?sorted=true'>";

echo "Click here to see my top 10 movies, sorted alphabetically."; echo "</a>";

?>

</body>

</html>

3/ Cho chạy login.php và click vào dòng

186HClick here to see my top 10 movies.


Bạn sẽ thấy kết quả như Hình 2.10.2.2 :



Hình 2 10 2 2 Quay lại file movie1 php click vào 187HClick here to see my top 10 movies sorted 1

Hình 2.10.2.2

Quay lại file movie1.php click vào: 187HClick here to see my top 10 movies, sorted alphabetically. Bạn sẽ thấy kết quả như sau như Hình 2.10.2.3:


Hình 2 10 2 3 Ở đây đã có sự sắp xếp theo thứ tự alpha Cách thức hoạt 2

Hình 2.10.2.3

Ở đây đã có sự sắp xếp theo thứ tự alpha

Cách thức hoạt động :

Trước tiên bạn đặt danh sách động trong biến, $favmovies với mảng. Sau đó bạn

có thể

để danh sách movie từng cái một bằng cách sử

dụng foreach trong file

moviesite.php. Bạn cũng có thể thêm một liên kết để người sử dụng sắp xếp danh sách

theo alphabel bằng cách dùng biến có tên $_REQUEST[sorted].

Khi biến này là true thì hàm sort() được thực thi và bạn truyền biến true qua URL trong liên kết.

2.11. While và do.. while

Như ta đã thấy hàm foreach hoạt động trên mỗi phần tử của mảng. Ta cũng có thể dùng câu lệnh while để làm điều đó.

Ví dụ:

Sử dụng vòng lặp while để in ra dãy số từ 15

$num = 1;

while ($num <= 5)

{

echo $num; echo “<br>”;

$num = $num + 1;

}

Tương tự với do .. while cũng cho ra cùng kết quả.

$num = 1; do

{

echo $num; echo “<br>”;

$num = $num + 1

}

while ($num <= 5);

Ví dụ : về việc sử dụng hàm while:

1. Thay đổi movie1.php như sau:

<?php

session_start();

$_SESSION[‘username’] = $_POST[‘user’];

$_SESSION[‘userpass’] = $_POST[‘pass’];

$_SESSION[‘authuser’] = 0;

// kiểm tra username và password

if (($_SESSION[‘username’] == ‘Joe’) and $_SESSION[‘userpass’] = = ‘12345’)) {

$_SESSION[‘authuser’] = 1;

} else {

echo “Sorry, but you don’t have permission to view this page, you loser!”;

exit();

}

?>

<html>

<head>

<title>Find my Favorite Movie!</title>

</head>

<body>

<?php include “header.php” ?>

<?php

$myfavmovie=urlencode(“Life of Brian”);

echo “<a href=’moviesite.php?favmovie=$myfavmovie’>”; echo “Click here to see information about my favorite movie!”; echo “</a>”;

echo “<br>”;

/*

echo “<a href=’moviesite.php’>”;

echo “Click here to see my top 10 movies.”; echo “</a>”;

echo “<br>”;

echo “<a href=’moviesite.php?sorted=true’>”;

echo “Click here to see my top 10 movies, sorted alphabetically.”; echo “</a>”;

*/

echo “Or choose how many movies you would like to see:”; echo “</a>”;

echo “<br>”;

?>

<form method=”post” action=”moviesite.php”>

<p>Enter number of movies (up to 10):

<input type=”text” name=”num”>

<br>

Check here if you want the list sorted alphabetically:

<input type=”checkbox” name=”sorted”>

</p>

<input type=”submit” name=”Submit” value=”Submit”>

</form>

</body>

</html>

2. Thay đổi moviesite.php:

<?php

session_start();

//check to see if user has logged in with a valid password if ($_SESSION[‘authuser’] != 1) {

echo “Sorry, but you don’t have permission to view this page, you loser!”;

exit();

}

?>

<html>

<head>

<title>My Movie Site</title>

</head>

<body>

<?php include “header.php”; ?>

<?php

$favmovies = array(“Life of Brian”, “Stripes”,

“Office Space”, “The Holy Grail”, “Matrix”, “Terminator 2”, “Star Wars”,

“Close Encounters of the Third Kind”, “Sixteen Candles”,

“Caddyshack”);

if (isset($_REQUEST[‘favmovie’])) {//isset(xac dinh mot bien co trong //tap hop khong)

echo “Welcome to our site, “; echo $_SESSION[‘username’]; echo “! <br>”;

echo “My favorite movie is “; echo $_REQUEST[‘favmovie’]; echo “<br>”;

$movierate = 5;

echo “My movie rating for this movie is: “; echo $movierate;

} else {

echo “My top “. $_POST[“num”] . “ movies are:<br>”; if (isset($_REQUEST[‘sorted’])) {

sort($favmovies);

}

//list the movies

$numlist = 1;

while ($numlist <= $_POST[“num”])

{

echo $numlist; echo “. “;

echo pos($favmovies); next($favmovies); echo “<br>n”;

$numlist = $numlist + 1;

}

/*

foreach ($favmovies as $currentvalue) { echo $currentvalue;

echo “<br>n”;

}

*/

}

?>

</body>

</html>

Kết quả Hình 2.11


1 foreach favmovies as currentvalue echo currentvalue echo br n body html Kết quả Hình 2 11 3

Hình 2.11

Xem tất cả 258 trang.

Ngày đăng: 06/01/2024
Trang chủ Tài liệu miễn phí