By GokiSoft.com|
09:30 28/04/2021|
Học PHP
[Share Code] Viết chương trình quản lý sách - PHP/MySQL
#book_form.php
<?php
require_once('dbhelper.php');
if(!empty($_POST)) {
$title = $thumbnail = $price = $author = $nxb = $description = "";
if(isset($_POST['title'])) {
$title = $_POST['title'];
}
if(isset($_POST['thumbnail'])) {
$thumbnail = $_POST['thumbnail'];
}
if(isset($_POST['price'])) {
$price = $_POST['price'];
}
if(isset($_POST['author'])) {
$author = $_POST['author'];
}
if(isset($_POST['nxb'])) {
$nxb = $_POST['nxb'];
}
if(isset($_POST['description'])) {
$description = $_POST['description'];
}
// echo $thumbnail;
//Them du lieu vao database.
//1 transaction
//B1. Mo ket noi toi database
// $conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
// mysqli_set_charset($conn, 'utf8');
//B2. Query
/**
insert into books(thumbnail, title, price, author, nxb, description)
values
("aaa", "aaa", 1000, "aaa", "aaa", "aaa")
*/
$sql = "insert into books(thumbnail, title, price, author, nxb, description)
values
('$thumbnail', '$title', $price, '$author', '$nxb', '$description')";
execute($sql);
// mysqli_query($conn, $sql);
//B3. Dong ket noi database
// mysqli_close($conn);
}
#books.php
<?php
require_once('book_form.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Book Manager - Page</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>
<!-- include summernote css/js -->
<link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote.min.js"></script>
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2 class="text-center">Book Forms</h2>
</div>
<div class="panel-body">
<form method="post">
<div class="form-group">
<label for="usr">Title:</label>
<input required="true" type="text" class="form-control" id="title" name="title">
</div>
<div class="form-group">
<label for="email">Thumbnail:</label>
<input required="true" type="text" class="form-control" id="thumbnail" name="thumbnail">
</div>
<div class="form-group">
<label for="birthday">Price:</label>
<input type="number" step="0.01" class="form-control" id="price" name="price">
</div>
<div class="form-group">
<label for="pwd">Author:</label>
<input required="true" type="text" class="form-control" id="author" name="author">
</div>
<div class="form-group">
<label for="confirmation_pwd">NXB:</label>
<input required="true" type="text" class="form-control" id="nxb" name="nxb">
</div>
<div class="form-group">
<label for="address">Description:</label>
<textarea class="form-control" name="description"></textarea>
</div>
<button class="btn btn-success">Register</button>
</form>
</div>
</div>
</div>
<script type="text/javascript">
$('[name=description]').summernote({
placeholder: 'Hello stand alone ui',
tabsize: 2,
height: 220,
toolbar: [
['style', ['style']],
['font', ['bold', 'underline', 'clear']],
['color', ['color']],
['para', ['ul', 'ol', 'paragraph']],
['table', ['table']],
['insert', ['link', 'picture', 'video']],
['view', ['fullscreen', 'codeview', 'help']]
]
});
</script>
</body>
</html>
#config.php
<?php
define("HOST", "localhost");
define("USERNAME", "root");
define("PASSWORD", "");
define("DATABASE", "c2010g");
#dbhelper.php
<?php
require_once('config.php');
/**
* Phu hop vs lenh: insert, update, delete
*/
function execute($sql) {
//1 transaction
//B1. Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
//B2. Query
/**
insert into books(thumbnail, title, price, author, nxb, description)
values
("aaa", "aaa", 1000, "aaa", "aaa", "aaa")
*/
mysqli_query($conn, $sql);
//B3. Dong ket noi database
mysqli_close($conn);
}
//Dung voi cau lenh Select
function executeResult($sql) {
//doc noi dung trong database
//1 transaction
//B1. Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
//B2. Query
$resultset = mysqli_query($conn, $sql);
$data = [];//du lieu it (30-100 ban nghi thi lam cach nay)
//tham so 1 -> mapping key trong table -> key trong array in php
while(($row = mysqli_fetch_array($resultset, 1)) != null) {
$data[] = $row;
}
//B3. Dong ket noi database
mysqli_close($conn);
return $data;
}
#fibonaci.php
<!DOCTYPE html>
<html>
<head>
<title>Fibonaci</title>
<meta charset="utf-8">
</head>
<body>
<!-- Basic -->
<?php
$f0 = $f1 = 1;
$num = 10;
//B1. Dung cach thong thuong
echo "<h3>Day Fibonaci (C1)</h3><br/>";
echo "$f0 $f1";
for ($i=0; $i < $num - 2; $i++) {
$fn = $f0 + $f1;
echo " $fn";
$f0 = $f1;
$f1 = $fn;
}
//B2. In day Fibonaci.
//Tinh so fibonaci tai vi tri thu $n -> De quy khong nho.
$count = 0;
function fibonaci($n) {
if($n == 0 || $n == 1) return 1;
global $count;
$count++;
return fibonaci($n - 1) + fibonaci($n - 2);
}
//fibonaci(0) -> 1, fibonaci(1) -> 1
//fibonaci(2) = fibonaci(1) + fibonaci(0) = 1 + 1 = 2
//fibonaci(3) = fibonaci(2) + fibonaci(1)
// = fibonaci(1) + fibonaci(0) + 1
// = 1 + 1 + 1 = 3
//fibonaci(4) = fibonaci(3) + fibonaci(2)
// = fibonaci(2) + fibonaci(1) + fibonaci(1) + fibonaci(0)
// = fibonaci(1) + fibonaci(0) + 1 + 1 + 1
// = 1 + 1 + 3 = 5
echo "<h3>Day Fibonaci (C2)</h3><br/>";
for ($i=0; $i < $num; $i++) {
$f = fibonaci($i);
echo "$f ";
}
echo "<br/>count = $count<br/>";
//B2: In day Fibonaci -> Su dung de quy co nho
$data = [1, 1]; //fibonaci(0) = 1, fibonaci(1) = 1
$count = 0;
function fibonaci2($n) {
global $count, $data;
if(isset($data[$n])) return $data[$n];
$count++;
$s = fibonaci2($n - 1) + fibonaci2($n - 2);
$data[$n] = $s;
// echo "$n - $s<br/>";
return $s;
}
// var_dump($data);
echo "<h3>Day Fibonaci (C3)</h3><br/>";
for ($i=0; $i < $num; $i++) {
$f = fibonaci2($i);
echo "$f ";
}
echo "<br/>count = $count<br/>";
?>
</body>
</html>
#list.php
<?php
require_once('book_form.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Book Manager - Page</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">Book Manager</h2>
</div>
<div class="panel-body">
<table class="table table-bordered">
<thead>
<tr>
<th>No</th>
<th>Thumbnail</th>
<th>Title</th>
<th>Price</th>
<th>Nxb</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
//doc noi dung trong database
//1 transaction
//B1. Mo ket noi toi database
// $conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
// mysqli_set_charset($conn, 'utf8');
//B2. Query
//$sql = "select * from books"
$sql = "select id, thumbnail, title, price, nxb, author from books";
$data = executeResult($sql);
// $resultset = mysqli_query($conn, $sql);
// $data = [];//du lieu it (30-100 ban nghi thi lam cach nay)
//tham so 1 -> mapping key trong table -> key trong array in php
// while(($row = mysqli_fetch_array($resultset, 1)) != null) {
// $data[] = $row;
// }
//B3. Dong ket noi database
// mysqli_close($conn);
$count = 0;
foreach ($data as $item) {
echo '<tr>
<td>'.++$count.'</td>
<td><img src="'.$item['thumbnail'].'" style="width: 160px;"/></td>
<td>'.$item['title'].'</td>
<td>'.$item['price'].'</td>
<td>'.$item['nxb'].'</td>
<td><button class="btn btn-warning">Edit</button></td>
<td><button class="btn btn-danger">Delete</button></td>
</tr>';
}
?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)