wiki:ImportantCase2

Приказ на сите книги

Актери

  • Најавен корисник

Чекор 1

Избор на жанра/и

$genres_filters = $_GET['genres'];
        $placeholders = str_repeat('?,', count($genres_filters) - 1) . '?';

        $stmt = $conn->prepare("SELECT book.bookid, book.CoverImage, book.Title, book.Genre, book.PublishedYear, author.FirstName, author.LastName
                                FROM Book
                                INNER JOIN Book_Author ON Book.BookID = Book_Author.BookID
                                INNER JOIN Author ON Book_Author.AuthorID = Author.AuthorID
                                WHERE book.genre IN ($placeholders) AND book.PublishedYear BETWEEN ? AND ?;");

        $stmt->execute(array_merge($genres_filters, [$query_from, $query_to]));

Чекор 2

Избор на временски период

$year_query = "SELECT MIN(PublishedYear) as min_year, MAX(PublishedYear) as max_year FROM Book";
$year_stmt = $conn->prepare($year_query);
$year_stmt->execute();
$year_range = $year_stmt->fetch(PDO::FETCH_ASSOC);


$where_conditions = [];
$params = [];

$year_from = isset($_GET['year_from']) ? (int)$_GET['year_from'] : null;
$year_to = isset($_GET['year_to']) ? (int)$_GET['year_to'] : null;

$year_min = $year_range['min_year'];
$year_max = $year_range['max_year'];

$query_from = $year_from;
$query_to = $year_to;

if($year_from === null) {
    $query_from = $year_min;
}
if($year_to === null) {
    $query_to = $year_max;
}

$stmt = $conn->prepare("SELECT book.bookid, book.CoverImage, book.Title, book.Genre, book.PublishedYear, author.FirstName, author.LastName
                                FROM Book
                                INNER JOIN Book_Author ON Book.BookID = Book_Author.BookID
                                INNER JOIN Author ON Book_Author.AuthorID = Author.AuthorID WHERE book.PublishedYear BETWEEN :year_from AND :year_to;
        ");
        $stmt->bindParam(":year_from", $query_from, PDO::PARAM_STR);
        $stmt->bindParam(":year_to", $query_to, PDO::PARAM_STR);
        $stmt->execute();

Чекор 3

Сортирање на книгите по избор, и селектирање на "Apply Filters"

  function sortBooks(books, sortType) {
    const sortedBooks = [...books]; 
    
    switch(sortType) {
        case 'name-asc':
            sortedBooks.sort((a, b) => a.title.localeCompare(b.title));
            break;
        case 'name-desc':
            sortedBooks.sort((a, b) => b.title.localeCompare(a.title));
            break;
        case 'year-desc':
            sortedBooks.sort((a, b) => b.publishedYear - a.publishedYear);
            break;
        case 'year-asc':
            sortedBooks.sort((a, b) => a.publishedYear - b.publishedYear);
            break;
        case 'relevance':
        default:
            break;
    }
    
    return sortedBooks;
}

Чекор 4

Резултат

Last modified 5 days ago Last modified on 01/10/25 15:18:53

Attachments (4)

Download all attachments as: .zip

Note: See TracWiki for help on using the wiki.