-----
Repeating actions through iteration (p46)
Breaking out of a control structure or script (p50)
Employing alternative control structure syntax (p51)
Using declare (p51)
– Put the lab on the your free hosting, test to make sure it run well
Xem thêm: Tổng hợp các bài viết về Học làm web
Xem thêm: Học làm web (x5) - PHP: crash course 5 (file)
Repeating actions through iteration (p46)
while loops (p47)
The basic structure of a while loop is:
while (condition)
expression;
Example, the following while
loop will display the numbers from 1 to 5:
$num = 1;
while($num <= 5) {
echo $num.
"<br>";
$num++;
}
At the beginning of each iteration, the condition is tested.
If the condition is false, the block
will not be executed and the loop will end. The next statement after the loop
will then be executed.
Lab 6. Iteration
Make the freight (transport goods) table to resemble (show)
in following picture:
The HTML of freight table (freight.html):
<!DOCTYPE html>
<html>
<head>
<title>Bob's Auto
Parts - Freight Costs</title>
</head>
<body>
<table
style="border: 0px; padding: 3px">
<tr>
<td
style="background: #cccccc; text-align:
center;">Distance</td>
<td
style="background: #cccccc; text-align: center;">Cost</td>
</tr>
<tr>
<td
style="text-align: right;">50</td>
<td
style="text-align: right;">5</td>
</tr>
<tr>
<td
style="text-align: right;">100</td>
<td style="text-align:
right;">10</td>
</tr>
<tr>
<td
style="text-align: right;">150</td>
<td
style="text-align: right;">15</td>
</tr>
<tr>
<td
style="text-align: right;">200</td>
<td
style="text-align: right;">20</td>
</tr>
<tr>
<td
style="text-align: right;">250</td>
<td
style="text-align: right;">25</td>
</tr>
</table>
</body>
</html>
Now, we use a while loop
to generate the freight table.
[freight.php]
<!DOCTYPE html>
<html>
<head>
<title>Bob's Auto
Parts - Freight Costs</title>
</head>
<body>
<table
style="border: 0px; padding: 3px">
<tr>
<td
style="background: #cccccc; text-align: center;">Distance</td>
<td
style="background: #cccccc; text-align: center;">Cost</td>
</tr>
<?php
$distance = 50;
while($distance
<= 250){
echo
'<tr>';
echo
'<td style="text-align: right">'.$distance.'</td>';
echo
'<td style="text-align: right">'.($distance/10).'</td>';
echo
"</tr>\n";
$distance
+=50;
}
?>
</table>
</body>
</html>
To make the HTML generated by the script readable, you need
to include newlines and spaces. As already mentioned, browsers ignore the whitespace, but it is important for human readers. You often need to look at
the HTML if your output is not what you were seeking. \n inside some of the
strings to make a new line.
Saving the freight.php, refesh browser to see the result.
for and foreach loop
(p49)
The basic structure of a for
loop is:
for( expressions1;
condition; expression2)
expression3
– expression1 is
executed once at th start. Here, you usually set the initial value of a
counter.
– the condition expression
is tested before each iteration. If the expression returns false, iteration stops. Here, you usually test the counter against
a limit.
– expression2 is
executed at the end of each iteration. Here, you usually adjust the value of
the counter
– expression3 is
executed once per iteration. This expression is usually a block of code and
contains the bulk of the loop code.
You can rewrite the while
loop in Lab 6 as a for loop. In this
case, the PHP code becomes
<?php
for($distance =
50; $distance <= 250; $distance += 50){
echo
'<tr>';
echo
'<td style="text-align: right">'.$distance.'</td>';
echo
'<td style="text-align: right">'.($distance/10).'</td>';
echo
"</tr>\n";
}
?>
You can combine variable variables with a for loop to iterate through a series of
repetitive form fields. If, for example, you have form fields with names such
as name1, name2, name3, and so on, you can process them like this:
for ($i = 1; $i <= $numnames; $i++){
$temp =
“name$i”;
echo
htmlspecialchars($$temp).’<br>’;
}
do…while Loops (p50)
do
expression;
while( condition );
The statement or block within the loop is always executed at
least once. Even if you consider this example in which the condition will be false at the start and can never become true, the loop will be executed once
before checking the condition and ending:
$num = 100;
do{
echo
$num.“<br>”;
}while ($num < 1);
Breaking out of a control structure or script (p50)
If you want to stop executing a piece of code, you can
choose from three approaches, depending on the effect you are trying to
achieve.
– break statement:
to stop executing a loop, execution of the script will continue at the next
line of the script after the loop
– continue
statement: to jump to the next loop iteration
– exit statement:
to finish executing the entire PHP script, it is useful when you are performing
error checking. Example:
if ($totalqty == 0){
echo “You dit
not order anything on the previous page! <br>”;
exit;
}
Employing alternative control structure syntax (p51)
You can use a colon (:) replace the opening brace ({), and
use endif, endswitch, endfor, endwhile or endforeach replace the close brace
(}) depending on which control structure is being used. No alternative systax
is available for do…while loops.
For example, the code
if ($totalqty == 0) {
echo “You did
not order anything on the previous page!<br>”;
exit;
}
could be converted to this code,
if ($totalqty == 0) :
echo “You did
not order anything on the previous page!<br>”;
exit;
endif;
Using declare (p51)
Lab 7. Get your constellation with
Vietnamese version.
User interface:
Requirements:
– User input the birthday (date and month)
– If the birthday is valid, then click on button “Lấy chòm
sao” to show the result, include: image, constellation, and date of
constellation.
– Put the lab on the your free hosting, test to make sure it run well
– Push the lab up the your remote repository (Github, or
Gitlab)
List of constellations:
– ARIES – DƯƠNG CƯU birthday is 21/3 – 20/4
– TAURUS – KIM NGƯU birthday is 21/4 – 21/5
– GEMINI – SONG TỬ birthday is 22/5 – 21/6
– CANCER – CỰ GIẢI birthday is 22/6-23/7
– LEO – HẢI SƯ birthday is 24/7 – 23/8
– VIRGO – XỬ NỬ birthday is 24/8 – 23/9
– LIBRA – THIÊN XỨNG birthday is 24/9 – 23/10
– SCORPIO – HỔ CÁP birthday is 24/10 – 22/11
– SAGITTARIUS – NHÂN MÃ birthday is 23/11 – 21/12
– CAPRICORNUS – MA KẾT birthday is 22/12 – 20/1
– AQUARIUS – BẢO BÌNH birthday is 21/1 – 19/2
– PISCES – SONG NGƯ birthday is 20/2 – 20/3
Lab 8. Make program named “In tam
giac” by using for, while, do while iteration.
Program’s snapshot:
Requirements:
– Program allows user choose one of iterations by using radio
button, then input number of triangle’s line, default is 6.
– When user click on one of the rectangles (with shape of
triangle), triangle will be drawed underneath respectively.
Lab 9. Make a simulation ATM.
Simulation ATM’s snapshot:
Requirements:
– First user input amount of money that they want to drawth,
validating to make sure amount of money is over 1000 VND and multipling of
1000.
– When user click on “Rut tien” button, “Menh gia”, “So luong”
and “Thanh tien” will be displayed then, with amount of bills is minimum.
-----------
Cập nhật [23/4/2019]
-----------
Xem thêm: Học làm web (x5) - PHP: crash course 5 (file)