Skip to content
Snippets Groups Projects
Commit 02fd1ede authored by moonawar's avatar moonawar
Browse files

book pagination [donge]

parent c003bc13
Branches
Tags
No related merge requests found
...@@ -11,40 +11,8 @@ class BookController extends Controller implements ControllerInterface{ ...@@ -11,40 +11,8 @@ class BookController extends Controller implements ControllerInterface{
try{ try{
switch($_SERVER['REQUEST_METHOD']){ switch($_SERVER['REQUEST_METHOD']){
case 'GET': case 'GET':
$bookData = $this->model->getBooks(1); header("Location: /book/search/1", true, 301);
exit;
// User
if(isset($_SESSION['username'])){
$userData = $this->model('UserModel');
$user = $userData->getUserByUsername($_SESSION['username']);
$username = $user['username'];
$role = $user['role'];
$imagePath = $user['image_path'];
$own = $userData->getMyBook($_SESSION['username']);
$have = ['own'=>$own];
$nav = ['username'=>$username, 'role'=> $role, 'profpic'=> $imagePath];
} else {
$nav = ['username'=>null];
$have = ['own'=>null];
}
if(!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
$_SESSION['page'] = $page;
$genre = ['genres'=>$this->model('GenreModel')->getAllGenres()];
$author = ['authors'=>$this->model('AuthorModel')->getAllAuthors()];
$dataset=['book'=>$bookData];
$paginationData = ['totalPages' => $this->model->getTotalPages(8), 'page' => 1];
$bookListView =$this->view('book','BookView', array_merge($dataset, $nav, $genre, $author, $have, $paginationData));
$bookListView->render();
break;
} }
}catch (Exception $e) { }catch (Exception $e) {
http_response_code($e->getCode()); http_response_code($e->getCode());
...@@ -167,10 +135,6 @@ class BookController extends Controller implements ControllerInterface{ ...@@ -167,10 +135,6 @@ class BookController extends Controller implements ControllerInterface{
exit; exit;
} }
// $editBookView = $this->view('admin', 'UpdateBookView',
// ['username' => $username,'role' => $this->model->getUserRole($username)]);
// $editBookView->render();
exit; exit;
} }
...@@ -253,12 +217,23 @@ class BookController extends Controller implements ControllerInterface{ ...@@ -253,12 +217,23 @@ class BookController extends Controller implements ControllerInterface{
switch ($_SERVER['REQUEST_METHOD']) { switch ($_SERVER['REQUEST_METHOD']) {
case 'GET': case 'GET':
$q = ''; $q = '';
if (isset($_GET['q'])) { if (isset($_GET['q'])) {
$q = $_GET['q']; $q = $_GET['q'];
} }
if(!isset($page)) {
$page = 1;
}
$_SESSION['page'] = $page;
$sort = isset($_GET['sort']) ? $_GET['sort'] : 'title'; $sort = isset($_GET['sort']) ? $_GET['sort'] : 'title';
$filter = isset($_GET['filter']) ? $_GET['filter'] : 'all'; $filter = isset($_GET['filter']) ? $_GET['filter'] : 'all';
$bookData = $this->model->getByQuery($q, $_GET['sort'], $_GET['filter'], $page);
$bookData = $this->model->getByQuery($q, $sort, $filter, $page);
$count = $this->model->bookCount($q, $sort, $filter);
$totalPages = ceil($count / BOOK_PER_PAGES);
// User // User
if(isset($_SESSION['username'])){ if(isset($_SESSION['username'])){
...@@ -276,10 +251,14 @@ class BookController extends Controller implements ControllerInterface{ ...@@ -276,10 +251,14 @@ class BookController extends Controller implements ControllerInterface{
$nav = ['username'=>null]; $nav = ['username'=>null];
$have = ['own'=>null]; $have = ['own'=>null];
} }
$q_params = "?filter=$filter&sort=$sort&q=$q";
$genre = ['genres'=>$this->model('GenreModel')->getAllGenres()]; $genre = ['genres'=>$this->model('GenreModel')->getAllGenres()];
$author = ['authors'=>$this->model('AuthorModel')->getAllAuthors()]; $author = ['authors'=>$this->model('AuthorModel')->getAllAuthors()];
$dataset=['book'=>$bookData]; $dataset=['book'=>$bookData];
$bookListView =$this->view('book','BookView', array_merge($dataset, $nav, $genre, $author, $have)); $paginationData = ['totalPages' => $totalPages, 'page' => $page, 'q_params' => $q_params];
$bookListView =$this->view('book','BookView', array_merge($dataset, $nav, $genre, $author, $have, $paginationData));
$bookListView->render(); $bookListView->render();
exit; exit;
default: default:
......
...@@ -252,6 +252,7 @@ class BookModel { ...@@ -252,6 +252,7 @@ class BookModel {
} }
$stmt = $this->db->prepare($query); $stmt = $this->db->prepare($query);
echo $this->db->error_info();
$q = "%$q%"; $q = "%$q%";
...@@ -267,6 +268,51 @@ class BookModel { ...@@ -267,6 +268,51 @@ class BookModel {
$stmt->close(); $stmt->close();
return $books; return $books;
} }
public function bookCount($q, $sort = 'title', $filter = 'all') {
$perPage = BOOK_PER_PAGES;
if ($filter === 'all') {
$query =
"SELECT b.*, GROUP_CONCAT(DISTINCT a.full_name) AS authors, GROUP_CONCAT(DISTINCT g.name) AS genres
FROM book b
JOIN authored_by ab ON b.book_id = ab.book_id
JOIN author a ON ab.author_id = a.author_id
JOIN book_genre bg ON b.book_id = bg.book_id
JOIN genre g ON bg.genre_id = g.genre_id
WHERE (a.full_name LIKE ? OR b.title LIKE ?)
GROUP BY b.book_id
ORDER BY $sort";
} else {
$query =
"SELECT b.*, GROUP_CONCAT(DISTINCT a.full_name) AS authors, GROUP_CONCAT(DISTINCT g.name) AS genres
FROM book b
JOIN authored_by ab ON b.book_id = ab.book_id
JOIN author a ON ab.author_id = a.author_id
JOIN book_genre bg ON b.book_id = bg.book_id
JOIN genre g ON bg.genre_id = g.genre_id
WHERE (a.full_name LIKE ? OR b.title LIKE ?) AND (g.name = ? OR a.full_name = ?)
GROUP BY b.book_id
ORDER BY $sort";
}
$stmt = $this->db->prepare($query);
echo $this->db->error_info();
$q = "%$q%";
if ($filter !== 'all') {
$this->db->bindParams($stmt, "ssss", $q, $q, $filter, $filter);
} else {
$this->db->bindParams($stmt, "ss", $q, $q);
}
$this->db->execute($stmt);
$books = $this->db->getAllRecords($stmt);
$stmt->close();
return count($books);
}
public function getBookByUserId($userId) public function getBookByUserId($userId)
......
...@@ -46,8 +46,8 @@ ...@@ -46,8 +46,8 @@
<option value="year desc" <?php if (isset($_GET['sort']) && $_GET['sort'] == 'year desc') : ?>selected="selected"<?php endif; ?>>Year DSC</option> <option value="year desc" <?php if (isset($_GET['sort']) && $_GET['sort'] == 'year desc') : ?>selected="selected"<?php endif; ?>>Year DSC</option>
</optgroup> </optgroup>
<optgroup label="Book Name"> <optgroup label="Book Name">
<option value="title-asc" <?php if (isset($_GET['sort']) && $_GET['sort'] == 'title asc') : ?> selected="selected"<?php endif; ?>>Title ASC</option> <option value="title asc" <?php if (isset($_GET['sort']) && $_GET['sort'] == 'title asc') : ?> selected="selected"<?php endif; ?>>Title ASC</option>
<option value="title-dsc" <?php if (isset($_GET['sort']) && $_GET['sort'] == 'title dsc') : ?>selected="selected"<?php endif; ?>>Title DSC</option> <option value="title desc" <?php if (isset($_GET['sort']) && $_GET['sort'] == 'title dsc') : ?>selected="selected"<?php endif; ?>>Title DSC</option>
</optgroup> </optgroup>
</select> </select>
<input type="text" name="q" class="search" placeholder="Search"/> <input type="text" name="q" class="search" placeholder="Search"/>
...@@ -112,16 +112,18 @@ ...@@ -112,16 +112,18 @@
$maxPage = $this->data['totalPages']; $maxPage = $this->data['totalPages'];
$prevPage = $page - 1; $prevPage = $page - 1;
$params = $this->data['q_params'];
$topPage = $page + 10; $topPage = $page + 10;
if ($topPage > $maxPage) { if ($topPage > $maxPage) {
$topPage = $maxPage; $topPage = $maxPage;
} }
for ($i = 1; $i <= $topPage; $i++) { for ($i = 1; $i <= $topPage; $i++) {
if ($i == $page) { if ($i == $page) {
echo '<a href="/author/update?page=' . $i . '" class="page-btn"><b>' . $i . '</b></a>'; echo '<a href="/book/search/' . $i . $params . '" class="page-btn"><b>' . $i . '</b></a>';
} else { } else {
echo '<a href="/author/update?page=' . $i . '" class="page-btn"><div class="">' . $i . '</div></a>'; echo '<a href="/book/search/' . $i . $params . '" class="page-btn"><div class="">' . $i . '</div></a>';
} }
} }
?> ?>
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment