By GokiSoft.com|
19:58 18/11/2022|
Học PHP
[Source Code] Tìm hiểu Array & Form trong PHP - C2206L
#array.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Array in PHP</title>
</head>
<body>
<h1>Welcome to learn Array in PHP</h1>
<?php
//Phan 1: Array index
// B1. Khai bao array
$arr = [];
$arr = array();//length, index: 0 -> length - 1, index >= 0
// B2. Them phan tu vao trong mang
array_push($arr, 12); //12 -> length: 1, index: 0
var_dump($arr);
$arr[] = 10; //12, 10 -> length: 2, index: 0 -> 1
$arr[] = 100; //12, 10, 100 -> length: 3, index: 0 -> 2
$arr[] = 12;
var_dump($arr);
// B3. Lay phan tu ra
//Xac dinh vi tri index can lay du lieu ra -> index = 1
echo $arr[1];
// B4. Duyet phan tu trong mang -> count, sizeof
for ($i=0; $i < count($arr); $i++) {
echo "\n".$arr[$i].'<br/>';
}
echo "\n<br/>===================<br/>\n";
foreach ($arr as $item) {
echo "\n".$item.'<br/>';
}
// B5. Xoa 1 phan tu trong mang
array_splice($arr, 2, 1);
var_dump($arr);
// B6. Chen 1 phan tu vao trong mang -> 12, 10, 12 -> 12, 10, 1, 2, 12
array_splice($arr, 2, 0, [1, 2]);
array_splice($arr, 2, 0, 11);
var_dump($arr);
// Phan 2: array index -> co cac phan tu san co
$arr2 = [10, 2, 111];
$arr2 = array(55, 6, 100);
// Phan 3: Array -> key & value
$arr = [];
$arr = [
'fullname' => 'TRAN VAN A',
'age' => 22
];
// Bieu dien 1 thong tin sinh vien: fullname -> TRAN VAN A, age: 22
$arr['fullname'] = 'Tran Van A';
$arr['fullname'] = 'Tran Van B';
$arr['fullname'] = 'Tran Van C';
$arr['age'] = 22;
var_dump($arr);
//Lay du lieu ra
echo $arr['fullname'];
//For
foreach ($arr as $key => $value) {
echo "\n".$key.' -> '.$value."\n";
}
//Xoa 1 key di -> lam the nao???
unset($arr['fullname']);
var_dump($arr);
// unset($arr);
// var_dump($arr);
// Khai bao 1 mang index -> chua cac phan tu la mang key & value
$stdList = [];
$stdList[] = [
'fullname' => 'TRAN VAN A',
'age' => 22
];
$stdList[] = [
'fullname' => 'TRAN VAN B',
'age' => 25
];
$stdList[] = [
'fullname' => 'TRAN VAN C',
'age' => 26
];
var_dump($stdList);
?>
</body>
</html>
#bt1635.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>BT in PHP</title>
</head>
<body>
<?php
//B1. Sinh ngau nhien so N
$N = rand(1, 10);
$bookList = [];
for ($i=0; $i < $N; $i++) {
$bookList[] = [
'bookName' => 'Sach '.$i,
'authorName' => 'Tac gia '.$i,
'price' => 1000 * $i + rand(10, 2000),
'nxb' => 'NXB '.$i
];
}
// var_dump($bookList);
?>
<table border="1" cellpadding="3" cellpadding="3">
<thead>
<th>STT</th>
<th>Ten Sach</th>
<th>Tac Gia</th>
<th>Gia Ban</th>
<th>NXB</th>
</thead>
<tbody>
<?php
$count = 0;
foreach ($bookList as $item) {
echo '<tr>
<td>'.++$count.'</td>
<td>'.$item['bookName'].'</td>
<td>'.$item['authorName'].'</td>
<td>'.$item['price'].'</td>
<td>'.$item['nxb'].'</td>
</tr>';
}
?>
</tbody>
</table>
<table border="1" cellpadding="3" cellpadding="3">
<thead>
<th>STT</th>
<th>Ten Sach</th>
<th>Tac Gia</th>
<th>Gia Ban</th>
<th>NXB</th>
</thead>
<tbody>
<?php
$count = 0;
foreach ($bookList as $item) {
?>
<tr>
<td><?=++$count?></td>
<td><?=$item['bookName']?></td>
<td><?=$item['authorName']?></td>
<td><?=$item['price']?></td>
<td><?=$item['nxb']?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</html>
#form.php
<?php
//?fullname=TRAN+VAN+A&email=gokisoft.com%40gmail.com&birthday=2022-11-18&address=HA+NOI
//$_GET: key & value
// var_dump($_GET);
if(isset($_GET['abc'])) {
echo $_GET['abc'];
}
if(isset($_GET['fullname'])) {
echo $_GET['fullname'];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Registation Form * Form Tutorial</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<!-- Popper JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="text-center">Registation Form - Input User's Detail Information</h2>
</div>
<div class="panel-body">
<form method="get">
<div class="form-group">
<label for="usr">Name:</label>
<input required="true" type="text" class="form-control" id="usr" name="fullname">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input required="true" type="email" class="form-control" id="email" name="email">
</div>
<div class="form-group">
<label for="birthday">Birthday:</label>
<input type="date" class="form-control" id="birthday" name="birthday">
</div>
<div class="form-group">
<label for="address">Address:</label>
<input type="text" class="form-control" id="address" name="address">
</div>
<button type="submit" class="btn btn-success">Register</button>
</form>
</div>
</div>
</div>
</body>
</html>
#form.txt
Form: GET
input -> name: value
Action -> click submit button
->Build URL:
http://localhost/aptech/C2206L/lesson02/form.php?fullname=TRAN+VAN+A&email=gokisoft.com%40gmail.com&birthday=2022-11-18&address=HA+NOI
-> load URL
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)