By GokiSoft.com|
18:38 21/05/2022|
Học PHP
[Examination] Thi kết thúc khóa học PHP - Đề 1
Đề Thi Môn PHP
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-06-30 09:21:44
@extends ('booklibrary.layouts.master')
@section('js')
<script type="text/javascript" src="{{asset('js/booklibrary.js')}}"></script>
@stop
@section('css')
<link rel="stylesheet" type="text/css" href="{{asset('css/booklibrary.css')}}">
@stop
@section('content')
<div id="total" class="container">
<p id = "title">The Book List</p>
<form method="get">
<div class="input-group mb-3">
<input value="{{ $search }}" name="search" type="text" class="form-control" placeholder="Search by title...">
<div class="input-group-append">
<button class="btn btn-success" type="submit">Search</button>
</div>
</div>
</form>
<table class="table table-striped">
<thead>
<tr>
<th>STT</th>
<th>Title</th>
<th>ISBN</th>
<th>Publish Year</th>
<th>Availabe</th>
</tr>
</thead>
<tbody>
@foreach ($books as $book)
<tr>
<td>{{ ++$counter }}</td>
<td>{{ $book->title }}</td>
<td>{{ $book->ISBN }}</td>
<td>{{ $book->pub_year }}</td>
<td>{{ $book->available }}</td>
</tr>
@endforeach
</tbody>
</table>
{{$books -> links()}}
</div>
@stop
![Nguyễn Anh Vũ [T2008A]](https://www.gravatar.com/avatar/8863d24ed74b396082becbc4db8331fd.jpg?s=80&d=mm&r=g)
Nguyễn Anh Vũ
2021-06-30 09:21:30
thi lý thuyết 70%
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateLibraryTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('library', function (Blueprint $table) {
$table->id('bookid');
$table->integer('authorid');
$table->string('title');
$table->string('ISBN');
$table->integer('pub_year');
$table->integer('available');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('library');
}
}
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class LibrarySeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
for ($i=1; $i <= 10; $i++) {
DB::table('library')->insert([
'authorid' => $i,
'title' => 'Quyen sach ' . $i,
'ISBN' => '123' . $i,
'pub_year' => '200'.$i,
'available' => $i + 20
]);
}
}
}
<!doctype html>
<html lang="en">
<head>
<title>Book List</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<style>
body{
background-color: red !important;
}
h1{
text-align: center;
color: blue;
}
input{
width: 180px !important;
}
</style>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1>Book list</h1>
<form method="get">
<div class="form-group">
<input type="text" class="form-control" name="search">
</div>
</form>
<table class="table table-bordered">
<thead>
<tr>
<th>No</th>
<th>Title</th>
<th>ISBN</th>
<th>Publish Year</th>
<th>Available</th>
</tr>
</thead>
<tbody>
@foreach ($bookList as $item)
<tr>
<td>{{$index++}}</td>
<td>{{$item -> title}}</td>
<td>{{$item -> ISBN}}</td>
<td>{{$item -> pub_year}}</td>
<td>{{$item -> available}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</body>
</html>
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-06-30 09:21:28
<!DOCTYPE html>
<html>
<head>
<title></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>
@yield('css')
</head>
<body>
<div class="container">
@yield('content')
</div>
@yield('js')
</body>
</html>
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-06-30 09:20:54
<?php
namespace App\Http\Controllers\BookLibrary;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
class BookLibraryController extends Controller
{
public function indexBooks(Request $request) {
$search = $request->search;
if (isset($search) && $search != '') {
$books = DB::table('booklibrary') ->where('booklibrary.title', 'like', '%'.$search.'%') -> paginate(10);
}else{
$books = DB::table('booklibrary') ->paginate(10);
}
return view('booklibrary.index-books') -> with([
'books' => $books,
'counter' => 0,
'search' => $search,
]);
}
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-06-30 09:20:12
<?php
Route::group(['prefix' => '/booklibrary'], function(){
Route::get('/index-books', [App\Http\Controllers\BookLibrary\BookLibraryController::class, 'indexBooks']) -> name('index-books');
});
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-06-30 09:19:08
Ly thuyet Do Trung Duc
![Trần Thị Khánh Huyền [T2008A]](https://www.gravatar.com/avatar/554e115833778e4294a01aebe228f3d6.jpg?s=80&d=mm&r=g)
Trần Thị Khánh Huyền
2021-06-30 09:18:51
#book.php
<?php
Route::group(['prefix' => 'book'], function () {
Route::get('/list','book\bookController@showList')->name('list');
Route::get('/detail','book\bookController@showDetail')->name('detail');
});
?>
![Bùi Văn Mạnh [T2008A]](https://www.gravatar.com/avatar/17e9c94870c94e61c9203ee31dccf01c.jpg?s=80&d=mm&r=g)
Bùi Văn Mạnh
2021-06-30 09:15:12
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class LibrarySeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
for ($i=1; $i <= 10; $i++) {
DB::table('library')->insert([
'authorid' => $i,
'title' => 'Quyen sach ' . $i,
'ISBN' => '123' . $i,
'pub_year' => '200'.$i,
'available' => $i + 20
]);
}
}
}
![Trần Thị Khánh Huyền [T2008A]](https://www.gravatar.com/avatar/554e115833778e4294a01aebe228f3d6.jpg?s=80&d=mm&r=g)
Trần Thị Khánh Huyền
2021-06-30 09:15:01
#bookController.php
<?php
namespace App\Http\Controllers\book;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
class bookController extends Controller
{
function showList(Request $request){
if (isset($request->title)) {
$list= DB::table('book')
->where('title','like','%'.$request->title.'%')
->paginate(10);
}else{
$list= DB::table('book')->paginate(10);
}
return view('book.list')->with([
'list' => $list,
'index'=>1
]);
}
function showDetail(Request $request){
if (isset($request->id)) {
# code...
$detail = DB::table('book')
->where('id',$request->id)
->first();
}
return view('book.detail')->with([
'detail' => $detail
]);
}
}<font color="#000000" face="Arial, sans-serif"><span style="font-size: 15px; white-space: normal;">
</span></font>
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2021-06-30 09:14:40
#addBook.php
<?php
require_once ('dbhelper.php');
$authorid = $title = $isbn = $pub_year = $available = '';
if(!empty($_POST)) {
$id = '';
if (isset($_POST['authorid'])) {
// code...
$authorid = $_POST['authorid'];
}
if (isset($_POST['title'])) {
// code...
$title = $_POST['title'];
}
if (isset($_POST['isbn'])) {
// code...
$isbn = $_POST['isbn'];
}
if (isset($_POST['pub_year'])) {
// code...
$pub_year = $_POST['pub_year'];
}
if (isset($_POST['available'])) {
// code...
$available = $_POST['available'];
}
if (isset($_POST['id'])) {
// code...
$id = $_POST['id'];
}
$authorid =str_replace('\'', '\\\'', $authorid);
$title =str_replace('\'', '\\\'', $title);
$isbn =str_replace('\'', '\\\'', $isbn);
$pub_year =str_replace('\'', '\\\'', $pub_year);
$available =str_replace('\'', '\\\'', $available);
if($id != '') {
$sql =
"update book set where id =".$id;
} else {
$spl = "insert into book(authorid, title, isbn, pub_year, available) values ('$authorid', '$title', '$isbn', '$pub_year', '$available')";
}
execute($sql);
header('Location: Book.php');
$id = '';
if (isset($_GET['id'])) {
// code...
$id = $_GET['id'];
$sql = 'select * from book where id = '.$id;
$bookList = executeResult($sql);
if ($bookList != null && count($bookList)>0 ) {
// code...
$List = $bookList[0];
$authorid = $List['authorid'];
$title = $List['title'];
$isbn = $List['isbn'];
$pub_year = $List['pub_year'];
$available = $List['available'];
} else {
$id = '';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Add Book</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">Input Book</h2>
</div>
<div class="panel-body">
<form method="post">
<div class="form-group">
<label for="usr">Authorid:</label>
<input type="name" name="id" value="<?=$id?>" style="display: none;">
<input required="true" type="number" class="form-control" id="authorid" name="authorid" value="<?=$authorid?>">
</div>
<div class="form-group">
<label for="usr">Title:</label>
<input required="true" type="text" class="form-control" id="title" name="title" value="<?=$title?>">
</div>
<div class="form-group">
<label for="address">ISBN:</label>
<input type="text" class="form-control" id="isbn" name="isbn" value="<?=$isbn?>">
</div>
<div class="form-group">
<label for="address">Pub_year:</label>
<input type="text" class="form-control" id="pub_year" name="pubyear" value="<?=$pub_year?>">
</div>
<div class="form-group">
<label for="address">Available:</label>
<input type="text" class="form-control" id="available" name="available" value="<?=$available?>">
</div>
<a href="Book.php"><button type="button" class="btn btn-danger">BookList</button></a>
<button class="btn btn-success">Save</button>
</form>
</div>
</div>
</div>
</body>
</html>
#Book.php
<?php
require_once ('dbhelper.php');
$book = executeResult('select * from book');
?>
<!DOCTYPE html>
<html>
<head>
<title>Book</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</h2>
</div>
<div class="panel-body">
<a href="addBook.php"><button class="btn btn-success" style="margin-bottom: 15px">Add Book</button></a>
<input type="text" name="input" size="50" style="margin-left: 500px" placeholder="Enter to search...">
<table class="table table-bordered">
<thead>
<tr>
<th width="50px">Bookid</th>
<th>Authorid</th>
<th>Title</th>
<th>ISBN</th>
<th>pub_year</th>
<th>Available</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php
$count = 0;
foreach ($book as $item) {
# code...
echo '<tr>
<td>'.(++$count).'</td>
<td>'.$item['authorid'].'</td>
<td>'.$item['title'].'</td>
<td>'.$item['isbn'].'</td>
<td>'.$item['pub_year'].'</td>
<td>'.$item['available'].'</td>
<td><button class="btn btn-warning" onclick=\'window.open("addstudent.php?id='.$std['id'].'", "_self")\'>Edit</button></td>
<td><button class="btn btn-danger" onclick="deleteStudent('.$std['id'].')">Delete</button></td>
</tr>';
}
?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
#config.php
<?php
define('HOST', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', '');
define('DATABASE', 'dbexam');
#dbhelper.php
<?php
require_once ('config.php');
//insert, update, delete
function execute($sql) {
//save data -> database (product)
//B1. Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
//B2. Thuc thi query (select, insert, update, delete)
mysqli_query($conn, $sql);
//B3. Dong ket noi database
mysqli_close($conn);
}
function executeResult($sql) {
//save data -> database (product)
//B1. Mo ket noi toi database
$conn = mysqli_connect(HOST, USERNAME, PASSWORD, DATABASE);
mysqli_set_charset($conn, 'utf8');
//B2. Thuc thi query (select, insert, update, delete)
$data = [];
$resultset = mysqli_query($conn, $sql);
while (($row = mysqli_fetch_array($resultset, 1)) != null) {
$data[] = $row;
}
//B3. Dong ket noi database
mysqli_close($conn);
return $data;
}
function removeSpecialCharacter($str) {
$str = str_replace('\\', '\\\\', $str);
$str = str_replace('\'', '\\\'', $str);
return $str;
}
function getPOST($key) {
$value = '';
if (isset($_POST[$key])) {
$value = $_POST[$key];
}
return removeSpecialCharacter($value);
}