Menu
Coddy logo textTech

Search Functionality

Part of the Object Oriented Programming section of Coddy's PHP journey — lesson 85 of 91.

challenge icon

Challenge

Easy

Let's enhance our Library Management System by adding search functionality. Users need to find books easily, whether they're looking for a specific title, browsing by author, or checking what's currently available to borrow.

You'll organize your code across four files:

  • Book.php — Your Book class with readonly properties (isbn, title, author), a private $isAvailable property starting as true, and methods: isAvailable(), markAsBorrowed(), and markAsReturned().
  • User.php — Include the Book file. Your User class with readonly properties (id, name), a private $borrowedBooks array, a MAX_BOOKS constant of 3, and methods: getBorrowedBooks(), canBorrow(), addBook(Book $book), and removeBook(string $isbn).
  • Library.php — Include the User file. Expand your Library class with the existing functionality plus three new search methods:
    • searchByTitle(string $keyword): array — returns all books where the title contains the keyword (case-insensitive)
    • searchByAuthor(string $author): array — returns all books where the author name contains the search term (case-insensitive)
    • getAvailableBooks(): array — returns only books that are currently available for borrowing

    Use array_filter with arrow functions and stripos for case-insensitive matching. Remember that stripos returns false when no match is found, so check for !== false.

  • main.php — Include the Library file. You'll receive two inputs: a search keyword and an author name.

    Create a Library and add these four books:

    • ISBN: "978-0", Title: "PHP Basics", Author: "John Smith"
    • ISBN: "978-1", Title: "Advanced PHP", Author: "Jane Doe"
    • ISBN: "978-2", Title: "Python Guide", Author: "John Smith"
    • ISBN: "978-3", Title: "Web Development", Author: "Alice Brown"

    Borrow the book with ISBN "978-1" using a User with ID 1 and name "TestUser".

    Then perform these searches and print the results:

    1. Search by title using the first input, print each matching book's title on a new line
    2. Search by author using the second input, print the count of matching books
    3. Get available books and print the count

    Output format:

    Title matches:
    [title1]
    [title2]
    ...
    Author matches: [count]
    Available: [count]

This challenge demonstrates how array_filter combined with stripos creates powerful, user-friendly search functionality. The case-insensitive matching means users don't need to worry about exact capitalization when searching.

Try it yourself

<?php

require_once 'Library.php';

// Read inputs
$searchKeyword = trim(fgets(STDIN));
$authorName = trim(fgets(STDIN));

// TODO: Create a Library instance

// TODO: Add the four books to the library:
// ISBN: "978-0", Title: "PHP Basics", Author: "John Smith"
// ISBN: "978-1", Title: "Advanced PHP", Author: "Jane Doe"
// ISBN: "978-2", Title: "Python Guide", Author: "John Smith"
// ISBN: "978-3", Title: "Web Development", Author: "Alice Brown"

// TODO: Create a User with ID 1 and name "TestUser", register them

// TODO: Borrow the book with ISBN "978-1"

// TODO: Search by title using $searchKeyword and print results
echo "Title matches:\n";
// Print each matching book's title on a new line

// TODO: Search by author using $authorName and print the count
echo "Author matches: " . /* count here */ "\n";

// TODO: Get available books and print the count
echo "Available: " . /* count here */ "\n";

?>

All lessons in Object Oriented Programming