Skip to content

Commit 486acdf

Browse files
author
RichardHpaYoobee
committed
prepared update form and added new image crop size
1 parent a2939bd commit 486acdf

File tree

10 files changed

+72
-26
lines changed

10 files changed

+72
-26
lines changed

Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ Using the select query, view the entires which you created in the previous task.
4747
4. Add the select query to the update page to be able to see the information which you are updating, based on the entry you are requesting
4848

4949
**Extra Tasks**
50+
5051
Create more complex queries like getting the latest entry, getting the smallest, largest etc.

books/add.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,28 @@
7878
$manager = new ImageManager();
7979
$mainImage = $manager->make($fileTmp);
8080
$mainImage->save($destination."/".$newFileName, 100);
81+
8182
$thumbnailImage = $manager->make($fileTmp);
8283
$thumbDestination = "../images/uploads/thumbnails";
8384
if(! is_dir($thumbDestination)){
8485
mkdir("../images/uploads/thumbnails/", 0777, true);
8586
}
86-
$thumbnailImage->resize(300, null, function($constraint){
87+
$thumbnailImage->resize(null, 250, function($constraint){
8788
$constraint->aspectRatio();
8889
$constraint->upsize();
8990
});
9091
$thumbnailImage->save($thumbDestination."/".$newFileName, 100);
9192

92-
93-
93+
$mediumImage = $manager->make($fileTmp);
94+
$mediumDestination = "../images/uploads/medium";
95+
if(! is_dir($mediumDestination)){
96+
mkdir("../images/uploads/medium/", 0777, true);
97+
}
98+
$mediumImage->resize(300, null, function($constraint){
99+
$constraint->aspectRatio();
100+
$constraint->upsize();
101+
});
102+
$mediumImage->save($mediumDestination."/".$newFileName, 100);
94103

95104
header("Location: book.php?id=$lastID");
96105

books/allBooks.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
<?php foreach($allBooks as $singleBook): ?>
3131
<div class="col-md-4">
3232
<div class="card mb-4 shadow-sm">
33-
<img class="card-img-top" src="./images/uploads/thumbnails/<?= $singleBook['image_name']; ?>" alt="Card image cap">
33+
<img class="card-img-top" src="./images/uploads/medium/<?= $singleBook['image_name']; ?>" alt="Card image cap">
3434
<div class="card-body">
3535
<p class="card-text"><?= $singleBook['book_name']; ?></p>
3636
<div class="d-flex justify-content-between align-items-center">
3737
<div class="btn-group">
3838
<a href="./books/book.php?id=<?= $singleBook['id']; ?>" class="btn btn-sm btn-outline-info">View</a>
39-
<a href="./books/update.php" class="btn btn-sm btn-outline-secondary">Edit</a>
39+
<a href="./books/update.php?id=<?= $singleBook['id']; ?>" class="btn btn-sm btn-outline-secondary">Edit</a>
4040
</div>
4141
</div>
4242
</div>

books/book.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
if($result && mysqli_affected_rows($dbc) > 0){
99
$singleBook = mysqli_fetch_array($result, MYSQLI_ASSOC);
1010
} else if($result && mysqli_affected_rows($dbc) == 0){
11-
die("ERROR 404");
12-
// header("Location: ../errors/404.php");
11+
// die("ERROR 404");
12+
header("Location: ../errors/404.php");
1313
} else {
1414
die("ERROR: cannot get the data requested");
1515
}
@@ -26,14 +26,14 @@
2626

2727
<div class="row mb-2">
2828
<div class="col">
29-
<a class="btn btn-outline-primary" href="./books/update.php">Edit</a>
29+
<a class="btn btn-outline-primary" href="./books/update.php?id=<?= $singleBook['id']; ?>">Edit</a>
3030
<a class="btn btn-outline-danger" href="./books/confirm_delete.php">Delete</a>
3131
</div>
3232
</div>
3333

3434
<div class="row mb-2">
3535
<div class="col-xs-12 col-sm-4 align-self-center">
36-
<img class="img-fluid" src="./images/uploads/thumbnails/<?= $singleBook['image_name']; ?>" alt="Card image cap">
36+
<img class="img-fluid" src="./images/uploads/medium/<?= $singleBook['image_name']; ?>" alt="Card image cap">
3737
</div>
3838
<div class="col-xs-12 col-sm-8 align-self-center">
3939
<h3><?= $singleBook['book_name']; ?></h3>

books/update.php

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
<?php
22
require '../templates/header.php';
3+
4+
$id = $_GET['id'];
5+
$sql = "SELECT * FROM `books` WHERE id = $id";
6+
$result = mysqli_query($dbc, $sql);
7+
8+
if($result && mysqli_affected_rows($dbc) > 0){
9+
$singleBook = mysqli_fetch_array($result, MYSQLI_ASSOC);
10+
} else if($result && mysqli_affected_rows($dbc) == 0){
11+
// die("ERROR 404");
12+
header("Location: ../errors/404.php");
13+
} else {
14+
die("ERROR: cannot get the data requested");
15+
}
16+
317
?>
418
<div class="container">
519
<div class="row mb-2">
620
<div class="col">
7-
<h1>Edit Harry Potter and the Philosopher's Stone</h1>
21+
<h1>Edit <?= $singleBook['book_name']; ?></h1>
822
</div>
923
</div>
1024

1125
<div class="row mb-2">
1226
<div class="col">
13-
<form action="./books/update.php" method="post" enctype="multipart/form-data">
27+
<form action="./books/update.php?id=<?= $singleBook['id']; ?>" method="post" enctype="multipart/form-data">
1428
<div class="form-group">
1529
<label for="title">Book Title</label>
16-
<input type="text" class="form-control" placeholder="Enter book title" value="Harry Potter and the Philosopher's Stone">
30+
<input type="text" class="form-control" placeholder="Enter book title" value="<?= $singleBook['book_name']; ?>">
1731
</div>
1832

1933
<div class="form-group">
2034
<label for="author">Author</label>
21-
<input type="text" class="form-control" placeholder="Enter books author" value="J.K Rowling">
35+
<input type="text" class="form-control" placeholder="Enter books author" value="<?= $singleBook['author']; ?>">
2236
</div>
2337

2438
<div class="form-group">
2539
<label for="author">Book Description</label>
26-
<textarea class="form-control" name="description" rows="8" cols="80" placeholder="Description about the book">Harry Potter has been living an ordinary life, constantly abused by his surly and cold aunt and uncle, Vernon and Petunia Dursley and bullied by their spoiled son Dudley since the death of his parents ten years prior. His life changes on the day of his eleventh birthday when he receives a letter of acceptance into a Hogwarts School of Witchcraft and Wizardrydelivered by a half-giant named Rubeus Hagrid after previous letters had been destroyed by Harry’s Uncle Vernon and his Aunt Petunia. Hagrid explains Harry's hidden past as the wizard son of James and Lily Potter, who were a wizard and witch respectively, and how they were murdered by the most evil and powerful dark wizard of all time, Lord Voldemort, which resulted in the one-year-old Harry being sent to live with his aunt and uncle. The strangest bit of the murder was how Voldemor was unable to kill him, but instead had his own powers removed and blasted away, sparking Harry's immense fame among the magical community</textarea>
40+
<textarea class="form-control" name="description" rows="8" cols="80" placeholder="Description about the book"><?= $singleBook['description']; ?></textarea>
2741
</div>
2842

2943
<div class="form-group">
30-
<img data-src="holder.js/100px225?theme=thumb&bg=55595c&fg=eceeef&text=Thumbnail" alt="Card image cap">
44+
<img src="./images/uploads/thumbnails/<?= $singleBook['image_name']; ?>" alt="Card image cap"><br>
3145
<label for="file">Upload an Image</label>
3246
<input type="file" name="image" class="form-control-file">
3347
</div>

errors/404.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
require '../templates/header.php';
3+
?>
4+
<div class="container">
5+
<div class="jumbotron alert-danger">
6+
<h1 class="display-4">ERROR 404!</h1>
7+
<p class="lead">Page not found</p>
8+
<hr class="my-4">
9+
<p class="lead">
10+
<a class="btn btn-outline-danger btn-lg" href="./" role="button">Go back home</a>
11+
</p>
12+
</div>
13+
</div>
14+
15+
<?php require '../templates/footer.php' ?>

index.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
$result = mysqli_query($dbc, $sql);
77
if($result && mysqli_affected_rows($dbc) > 0){
88
$latestBook = mysqli_fetch_array($result, MYSQLI_ASSOC);
9-
} else {
10-
die("ERROR, cannot get the data requested");
119
}
10+
1211
?>
1312

1413
<div class="container">
@@ -21,7 +20,7 @@
2120
</div>
2221

2322
<div class="row mb-2">
24-
<?php if($latestBook): ?>
23+
<?php if(isset($latestBook)): ?>
2524
<div class="col-md-6">
2625
<div class="card flex-md-row mb-4 shadow-sm h-md-250">
2726
<div class="card-body d-flex flex-column align-items-start">
@@ -33,7 +32,7 @@
3332
<p class="card-text mb-auto">This is a wider card with supporting text below as a natural lead-in to additional content.</p>
3433
<a href="./books/book.php?id=<?= $latestBook['id']; ?>">Continue reading</a>
3534
</div>
36-
<img class="card-img-right flex-auto d-none d-lg-block" data-src="holder.js/200x250?theme=thumb" alt="Card image cap">
35+
<img class="card-img-right flex-auto d-none d-lg-block" src="./images/uploads/thumbnails/<?= $latestBook['image_name']; ?>" alt="Card image cap">
3736
</div>
3837
</div>
3938
<?php endif; ?>

library.sql

+12-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
-- http://www.phpmyadmin.net
44
--
55
-- Host: localhost
6-
-- Generation Time: Oct 14, 2018 at 07:40 PM
6+
-- Generation Time: Oct 16, 2018 at 11:40 AM
77
-- Server version: 5.7.23-0ubuntu0.16.04.1
88
-- PHP Version: 7.2.3-1+ubuntu16.04.1+deb.sury.org+1
99

@@ -31,9 +31,18 @@ CREATE TABLE `books` (
3131
`book_name` varchar(100) NOT NULL,
3232
`author` varchar(100) NOT NULL,
3333
`description` text NOT NULL,
34-
`image_name` varchar(20) NOT NULL DEFAULT 'bookDefault.jpg'
34+
`image_name` varchar(20) NOT NULL DEFAULT 'bookDefault.jpg',
35+
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
3536
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
3637

38+
--
39+
-- Dumping data for table `books`
40+
--
41+
42+
INSERT INTO `books` (`id`, `book_name`, `author`, `description`, `image_name`, `date_created`) VALUES
43+
(4, 'Harry Potter and the Philosopher\'s Stone', 'J K Rowling', 'Harry Potter has been living an ordinary life, constantly abused by his surly and cold aunt and uncle, Vernon and Petunia Dursley and bullied by their spoiled son Dudley since the death of his parents ten years prior. His life changes on the day of his eleventh birthday when he receives a letter of acceptance into a Hogwarts School of Witchcraft and Wizardry, delivered by a half-giant named Rubeus Hagrid after previous letters had been destroyed by Harry’s Uncle Vernon and his Aunt Petunia. Hagrid explains Harry\'s hidden past as the wizard son of James and Lily Potter, who were a wizard and witch respectively, and how they were murdered by the most evil and powerful dark wizard of all time, Lord Voldemort, which resulted in the one-year-old Harry being sent to live with his aunt and uncle. The strangest bit of the murder was how Voldemort was unable to kill him, but instead had his own powers removed and blasted away, sparking Harry\'s immense fame among the magical community.', '5bc514dad6863.jpg', '2018-10-15 22:29:46'),
44+
(5, 'Harry Potter and the Chamber of Secrets', 'J K Rowling', 'On Harry Potter\'s twelfth birthday, the Dursley family—Harry\'s uncle Vernon, aunt Petunia, and cousin Dudley—hold a dinner party for a potential client of Vernon\'s drill-manufacturing company. Harry is not invited, but is content to spend the evening quietly in his bedroom, although he is confused why his school friends have not sent cards or presents. However, when he goes to his room, a house-elf named Dobby warns him not to return to Hogwarts and admits to intercepting Harry\'s post from his friends. Having failed to persuade Harry to voluntarily give up his place at Hogwarts, Dobby then attempts to get him expelled by using magic to smash Petunia\'s dessert on a dinner party guest and framing it on Harry, who is not allowed to use magic out of school. Uncle Vernon\'s business deal falls through, but Harry is given a second chance from the Ministry of Magic, and allowed to return at the start of the school year.', '5bc5154044690.jpg', '2018-10-15 22:31:28');
45+
3746
--
3847
-- Indexes for dumped tables
3948
--
@@ -53,7 +62,7 @@ ALTER TABLE `books`
5362
-- AUTO_INCREMENT for table `books`
5463
--
5564
ALTER TABLE `books`
56-
MODIFY `id` tinyint(6) UNSIGNED NOT NULL AUTO_INCREMENT;
65+
MODIFY `id` tinyint(6) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;
5766
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
5867
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
5968
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

tasks/task3.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ Using the select query, view the entires which you created in the previous task.
44
2. View them individually from a single php page which filters which one you see based on your url
55
3. Create an error 404 page incase someone asks for an entry which doesn't exist
66
4. Add the select query to the update page to be able to see the information which you are updating, based on the entry you are requesting
7+
78
**Extra Tasks**
9+
810
Create more complex queries like getting the latest entry, getting the smallest, largest etc.

templates/header.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
<?php
2-
3-
4-
52
if(is_dir('vendor')){
63
require 'vendor/autoload.php';
74
} else {
@@ -38,7 +35,7 @@
3835
<a class="text-light" href="#">Subscribe</a>
3936
</div>
4037
<div class="col-6 text-center">
41-
<a class="blog-header-logo text-light" href="#">Yoobee School of Design Library</a>
38+
<a class="blog-header-logo text-light" href="./">Yoobee School of Design Library</a>
4239
</div>
4340
<div class="col-3 d-flex justify-content-end align-items-center">
4441
<a class="text-light" href="#">

0 commit comments

Comments
 (0)