By GokiSoft.com|
20:36 05/01/2024|
Học PHP
Hiển thị ngẫu nhiên N quyển sách bằng PHP - Quản lý sách bằng PHP
Sinh ngẫu nhiên một mảng gồm N quyển sách
- N cũng là số ngẫu nhiên đc sinh ra từ PHP
- Mỗi quyển sách gồm các thuộc tính tên sách, tác giả, giá bán, nhà xuất bản => mỗi giá trị cũng đc sinh ngẫu nhiên.
Hiển thị dữ liệu ra bảng
Chú ý: Giá trị sinh ngẫu nhiên như sau :
Quyển sách 1:
- Tên: quyển sách 1
- Tác giả: tác giả 1
- Giá 100
- Nhà xuất bản: nhà xuất bản 1
(thay số 1 thành các số bất kỳ từ 1-100)
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![nguyễn văn huy [T1907A]](https://www.gravatar.com/avatar/b107d14d7d43c142b68c12c377262371.jpg?s=80&d=mm&r=g)
nguyễn văn huy
2020-06-12 12:55:29
<!DOCTYPE html>
<html>
<head>
<title>Quản lý sách</title>
<meta charset="utf-8">
</head>
<body>
<?php
$ranm = mt_rand(1,100);
$bookList = [];
for ($i=0; $i < $ranm; $i++) {
$book = [
'Name' => 'quyển sách '.($i+2),
'tacgia' => 'tác giả '.($i+2),
'gia' => mt_rand(1,100),
'NXB' => 'nhà xuất bản '.($i+2)
];
$bookList[] = $book;
}
?>
<div >
<table >
<thead>
<tr>
<th>Tên sách</th>
<th>Tác Giả</th>
<th>Giá</th>
<th>Nhà xuất bản</th>
</tr>
</thead>
<tbody>
<?php
for ($i=0; $i < $ranm; $i++) {
$book1 = $bookList[$i];
echo '<tr>'.
'<td>'.($book1['Name']).'</td>'.
'<td>'.($book1['tacgia']).'</td>'.
'<td>'.($book1['gia']).'</td>'.
'<td>'.($book1['NXB']).'</td>'.
'</tr>';
}
?>
</tbody>
</table>
</div>
</body>
</html>
![Phan Bạch Tùng Dương [T1907A]](https://www.gravatar.com/avatar/e74e3ec62fe3a191929e12eecbe01edf.jpg?s=80&d=mm&r=g)
Phan Bạch Tùng Dương
2020-06-12 09:48:06
<!DOCTYPE html>
<html>
<head>
<title>Book Manage</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<body>
<h1 style="text-align: center; font-size:50px;">Book Manage</h1>
<?php
$rand = mt_rand(1, 100);
$bookList= [];
for ($i=0; $i < $rand; $i++) {
$book = [
'bookName' => 'book ' .($i+1),
'author' => 'author ' .($i+1),
'price' => mt_rand(100,10000).'$',
'producer' => 'producer ' .($i+1)
];
$bookList[] = $book;
}
?>
<div class="container-fluid">
<table class="table table-dark table-striped">
<thead>
<tr>
<th>Book Name: </th>
<th>Author: </th>
<th>Price: </th>
<th>Producer: </th>
</tr>
</thead>
<tbody>
<?php
for ($i=0; $i < $rand; $i++) {
$book1 = $bookList[$i];
echo '<tr>'.
'<td>'.($book1['bookName']).'</td>'.
'<td>'.($book1['author']).'</td>'.
'<td>'.($book1['price']).'</td>'.
'<td>'.($book1['producer']).'</td>'.
'</tr>';
}
?>
</tbody>
</table>
</div>
</body>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</html>
![Phí Văn Long [T1907A]](https://www.gravatar.com/avatar/5db166b7b74443c5c221e4c0068d6da9.jpg?s=80&d=mm&r=g)
Phí Văn Long
2020-06-12 09:40:24
<?php
$random = mt_rand(1,100);
$BookList = [];
for ($i=0; $i < $random ; $i++) {
$Book = [
'Name' => 'quyen sach '.($i + 1),
'Author' => 'Tac gia '.($i + 1),
'Price' => mt_rand(1,1000000000),
'Producer' => 'Nha xuat ban '.mt_rand(1,10)
];
$BookList[] = $Book;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title> QLSV</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.0/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.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Quan Lí Sinh Viên Bằng PHP</h2>
<table class="table">
<thead>
<tr>
<th>Ten sach :</th>
<th>Ten tac gia :</th>
<th>Gia :</th>
<th>Nha xuat ban :</th>
</tr>
</thead>
<tbody>
<?php
for ($i=0; $i < $random ; $i++) {
$Book1 = $BookList[$i];
print
'<tr>'.
'<td>'.($Book1['Name']).'</td>'.
'<td>'.($Book1['Author']).'</td>'.
'<td>'.($Book1['Price']).'</td>'.
'<td>'.($Book1['Producer']).'</td>'.
'<tr>';
}
?>
</tbody>
</table>
</div>
</body>
</html>
![Trương Công Vinh [T1907A]](https://www.gravatar.com/avatar/223a7e3a46f4a747f81b921fe023fcc4.jpg?s=80&d=mm&r=g)
Trương Công Vinh
2020-06-12 09:25:07
<?php
require_once ('book.php');
$bookList = [];
for ($i=0; $i < rand(10,1000); $i++) {
$book = new Book(uniqid(),uniqid(),rand(),uniqid());
$bookList[]=$book;
/*echo "Quyển sách ".($i+1).":</br>";
echo " \t\t Tên:".$book->bookName."</br>";
echo " \t\tTác giả:".$book->authorName."</br>";
echo " \t\tGiá :".$book->price."</br>";
echo " \t\t Nhà xuất bản: ".$book->NXB."</br>";*/
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Book</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/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.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th>Tên</th>
<th>Tác giả</th>
<th>Giá</th>
<th>Nhà xuất bản</th>
</tr>
</thead>
<tbody>
<?php
foreach($bookList as $b){
echo "<tr>";
echo "<td>.$b->bookName.</td>";
echo "<td>.$b->bookName.</td>";
echo "<td>.$b->bookName.</td>";
echo "<td>.$b->bookName.</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</body>
</html>
<?php
class Book
{
public $bookName ;
public $authorName;
public $price;
public $NXB ;
function __construct($bookName,$authorName,$price,$NXB)
{
$this->bookName = $bookName;
$this->authorName = $authorName;
$this->price = $price;
$this->NXB = $NXB;
}
}
?>
![Lê Minh Bắc [T1907A]](https://www.gravatar.com/avatar/22abcac77d8ca5e01144e240abb48d22.jpg?s=80&d=mm&r=g)
Lê Minh Bắc
2020-06-12 09:13:35
<!DOCTYPE html>
<html>
<head>
<title>Quản lý sách</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.0/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.0/js/bootstrap.min.js"></script>
</head>
<body>
<?php
$ran = mt_rand(1,100);
$bookList = [];
for ($i=0; $i < $ran; $i++) {
$book = [
'bookName' => 'quyển sách '.($i+1),
'authorName' => 'tác giả '.($i+1),
'price' => mt_rand(100,10000),
'NXB' => 'nhà xuất bản '.($i+1)
];
$bookList[] = $book;
}
?>
<div class="container-fluid">
<table class="table table-dark table-striped">
<thead>
<tr>
<th>Tên sách</th>
<th>Tác Giả</th>
<th>Giá</th>
<th>Nhà xuất bản</th>
</tr>
</thead>
<tbody>
<?php
for ($i=0; $i < $ran; $i++) {
$book1 = $bookList[$i];
echo '<tr>'.
'<td>'.($book1['bookName']).'</td>'.
'<td>'.($book1['authorName']).'</td>'.
'<td>'.($book1['price']).'</td>'.
'<td>'.($book1['NXB']).'</td>'.
'</tr>';
}
?>
</tbody>
</table>
</div>
</body>
</html>
![Đỗ Văn Huấn [T1907A]](https://www.gravatar.com/avatar/04c40301dd027839d265b3c3c9dc6e6b.jpg?s=80&d=mm&r=g)
Đỗ Văn Huấn
2020-06-12 09:12:19
<?php
$n = rand(1, 100);
echo 'n = '.$n;
$booklist = array();
for ($i = 1; $i <= $n; $i++) {
$book = [
'name' => 'sach' . $i,
'author' => 'tac gia' . $i,
'price' => 'gia' . $i,
'nhaxuatban' => 'nha xuat ban' . $i
];
$booklist[] = $book;
}
$tr = '';
foreach ($booklist as $bo) {
$tr .= '<tr>' .
'<td>' .$bo['name']. '</td>' .
'<td>' . $bo['author'] . '</td>' .
' <td>' . $bo['price'] . '</td>' .
'<td>' . $bo['nhaxuatban'] . '</td>' .
'</tr>';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.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.5.0/js/bootstrap.min.js"></script>
<title>Document</title>
</head>
<body>
<div class="container">
<table class="table table-bordered">
<thead>
<tr>
<th>Name book</th>
<th>Author</th>
<th>Price</th>
<th>nha xuat ban</th>
</tr>
</thead>
<tbody>
<?= $tr ?>
</tbody>
</table>
</div>
</body>
</html>