By GokiSoft.com| 20:13 06/05/2021|
Học PHP

[Share Code] Tìm hiểu Object & Array trong PHP - Tìm hiểu kết nối CSDL (database) trong PHP - Lập trình PHP/MySQL

#register.php


<?php
require_once ('register_form.php');
?>

<!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="post">
					<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="pwd">Password:</label>
					  <input required="true" type="password" class="form-control" id="pwd" name="password">
					</div>
					<div class="form-group">
					  <label for="confirmation_pwd">Confirmation Password:</label>
					  <input required="true" type="password" class="form-control" id="confirmation_pwd" name="confirmation_pwd">
					</div>
					<div class="form-group">
					  <label for="address">Address:</label>
					  <input type="text" class="form-control" id="address" name="address">
					</div>
					<button class="btn btn-success">Register</button>
				</form>
			</div>
		</div>
	</div>
</body>
</html>


#register_form.php


<?php
require_once ('dbhelper.php');

$fullname = $email = $birthday = $password = $confirmation_pwd = $address = "";
if (!empty($_POST)) {
	if (isset($_POST['fullname'])) {
		$fullname = $_POST['fullname'];
	}
	if (isset($_POST['email'])) {
		$email = $_POST['email'];
	}
	if (isset($_POST['birthday'])) {
		$birthday = $_POST['birthday'];
	}
	if (isset($_POST['password'])) {
		$password = $_POST['password'];
	}
	if (isset($_POST['confirmation_pwd'])) {
		$confirmation_pwd = $_POST['confirmation_pwd'];
	}
	if (isset($_POST['address'])) {
		$address = $_POST['address'];
	}

	//Kiem tra dieu kien dang ky thanh cong
	if (!empty($fullname) && $password == $confirmation_pwd && !empty($password)) {
		$sql = "insert into users(fullname, email, birthday, password, address) values ('$fullname', '$email', '$birthday', '$password', '$address')";
		querySql($sql);
	}
}


#readme.txt


create table users (
	id int primary key auto_increment,
	fullname varchar(50),
	email varchar(150),
	birthday date,
	password varchar(32),
	address varchar(200)
)


#dbhelper.php


<?php
require_once ('config.php');

function querySql($sql) {
	// Them du lieu vao database
	//B1. Mo ket noi toi database
	$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
	mysqli_set_charset($conn, 'utf8');

	//B2. Thuc hien truy van insert
	mysqli_query($conn, $sql);

	//B3. Dong ket noi database
	mysqli_close($conn);
}


#config.php


<?php
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'C2010L');


#array.php


<!DOCTYPE html>
<html>
<head>
	<title>Array & Object in PHP</title>
	<meta charset="utf-8">

	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
	<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.0/font/bootstrap-icons.css">
</head>
<body>
	<h1>Welcome to learn PHP (Object & Array)</h1>
<?php
// Co 2 loai mang: index - key & value
// Phan 1: Mang index
// Khai báo mảng rỗng trong PHP như nào
$arr1 = array();
$arr2 = [];

// Them 1 phan tu vao trong mang
array_push($arr2, 'Xin chao 1');
array_push($arr2, 'Xin chao 2');
$arr2[] = 'Xin chao 3';
var_dump($arr2);

// Khai niem trong array: length, index = 0 -> length - 1
$length = sizeof($arr2);
echo '<br/>'.$length;
$length = count($arr2);
echo '<br/>'.$length;

// Duyệt qua các phần tử trong mảng
// Duyet theo index
for ($i = 0; $i < $length; $i++) {
	echo '<br/>'.$arr2[$i];
}

//Duyet theo foreach
foreach ($arr2 as $value) {
	echo '<br/>'.$value;
}

// Xoa 1 phan tu ra khoi mang.
// Vidu: xoa phan tu 'xin chao 2' -> index = 1
echo '<br/>';
array_splice($arr2, 1, 1);
var_dump($arr2);

// Them 1 phan tu 'Xin chao 5' -> index = 1
array_splice($arr2, 1, 0, 'Xin chao 5');
echo '<br/>';
var_dump($arr2);
array_splice($arr2, 1, 0, ['Xin chao 6', 'Xin chao 7']);
echo '<br/>';
var_dump($arr2);

// Mang quan ly key & value ($_GET, $_POST, $_REQUEST)
//khai bao mang rong
$book = [
	'title'      => 'Lap Trinh C',
	'authorname' => 'Tran Van Diep',
	'price'      => 300000
];
$book['nxb'] = 'Aptech';
var_dump($book);

//Xoa key khoi mang
unset($book['authorname']);
var_dump($book);
// Lay gia tri trong mang
echo '<br/>'.$book['title'];
// Sua noi dung du lieu
$book['authorname'] = 'TVD';
echo '<br/>'.$book['authorname'];

foreach ($book as $key => $value) {
	echo '<br/>'.$key.' - '.$value;
}

$bookList = [
	[
		'title'      => 'Lap Trinh C',
		'authorname' => 'Tran Van Diep',
		'price'      => 300000
	], [
		'title'      => 'Lap Trinh HTML',
		'authorname' => 'Tran Van Diep',
		'price'      => 200000
	], [
		'title'      => 'Lap Trinh CSS',
		'authorname' => 'Tran Van Diep',
		'price'      => 100000
	]
];
$length = count($bookList);
for ($i = 0; $i < $length; $i++) {
	$item = $bookList[$i];
	echo '<br/>'.$item['title'];
}
?>
<div class="container">
	<table class="table table-bordered">
		<thead>
			<tr>
				<th>No</th>
				<th>Title</th>
				<th>Author Name</th>
				<th>Price</th>
			</tr>
		</thead>
		<tbody>
<?php
$count = 0;
foreach ($bookList as $item) {
	echo '<tr>
				<td>'.(++$count).'</td>
				<td>'.$item['title'].'</td>
				<td>'.$item['authorname'].'</td>
				<td>'.$item['price'].'</td>
			</tr>';
}
?>
</tbody>
	</table>
</div>

<?php
$class = [
	'infor'    => [
		'name'    => 'C2010L',
		'address' => '54 Le Thanh Nghi'
	],
	'studentList' => [
		[
			'name'   => 'Tran Van A',
			'gender' => 'Nam'
		], [
			'name'   => 'Tran Van B',
			'gender' => 'Nam'
		]
	]
];
var_dump($class);
?>
</body>
</html>


Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)