INFO 2480-61: Website Database Implementation
  • Syllabus
  • Welcome Letter
  • Week 1: Software and Setup
    • Folder Structure
    • Git: Getting Started
      • Installing a Git Client
      • Creating a Local Repository
      • Making Changes and Merging them
      • Git Wrap Up
    • Git: Github
      • Creating a New Account
      • Creating a Repo on Github
      • Cloning and Forking
      • Cloning a Repo from Github
      • Pushing Changes to Github.com
      • Github: Adding a Collaborator
    • Command Box
      • Downloading the Software
      • Unzipping and Running the Software for the first time
      • Using CommandBox And Installing the UML-Info package.
      • Setting Up Your Local Server
      • Last Step
    • IDE ( Integrated Development Environment )
      • VS Code
      • IntelliJ
    • Journal
  • Week 2: Review of HTML and CSS
    • Making Point In Time and Working Branches
    • Opening our Project from Week 1 and Starting our BoxLang server
    • HTTP Calls and Requests
    • Making a Static Website Dynamic
    • Creating The Management Index.bxm
    • Creating An HTML Form with Bootstrap
    • Last Steps
  • Week 3: Working Forms and our First Table
    • Data Types
    • Case Types and Naming Conventions
    • Database Tools and Your First DB
      • Installing MySql WorkBench
      • Connecting to your MySQL database
      • Creating Your First Table
    • Working Forms
      • Separation of Concerns
      • Configuring Our Site To Use Our Database
      • Capturing Your Form Data
      • Creating Our Server Side Logic
  • Week 5: SQL and Modelling
    • Our Project And Its Users
    • Introduction to SQL
    • Completing our Articles page.
      • Adapting manageArticles.bxm to display existing articles in the database
      • Completing ManageArticles.bxm to Edit Existing Articles.
      • Making Active Articles Appear On The Public Page
  • Week 6: Managing Books
    • Introduction To Modelling
    • Our Data Models
    • Adapting Our Management Page To Be Multi-Tool
    • Creating The Manage Books Page
    • Adding Search To Our Front Index Page
  • Week 7: Working with Selects, Files, WYSIWYG and more.
    • Adding WYSIWYG Capabilities
    • Adding WYSIWYG to the Manage Books Tool
    • Adding Images To Our Store
    • Creating Our Publisher Select Control
  • Week 9: Joins and Better Searching
    • Displaying Our Publisher
    • Searching By Publisher
    • Creating a Browse by Genre
      • Adapting Our Database
      • Building our Queries: Part 1
      • Assigning Genres to a Book in our AddEdit.bxm page
      • Building our Queries: Part 2
      • Building the GenreNav.bxm
      • Adapting The Details.bxm Page to Search By Genre
  • End of Project Checkllist
Powered by GitBook
On this page
  1. Week 9: Joins and Better Searching
  2. Creating a Browse by Genre

Adapting The Details.bxm Page to Search By Genre

The details page itself only need to be slightly tweaked.

  1. Currently it is searching on the form.searchTerm submission. Let's change that so it searches on searchTerm regardless of whether it comes in on a form or in a link. Add <bx:param name="searchTerm" default="" /> to the top of the page. Let's also add a param for the genre variable. <bx:param name="genreId" default=0 /> Why did we make the searchTerm default to "" but the genreId default to 0?

  2. The second is to pass the “genre” param to our searchBooks function in bookstore.common.books.

<bx:set bookinfo = bookstoreFunctions.searchBooks( searchTerm, genreId ) />

  1. In the searchBooks function, we need to change our arguments to accept both a searchTerm and a genre. Don't forget to put in default values of "" and 0 in case they are not passed in.

  2. We already have a query in there to search based on the “searchTerm” param. We need to add an “if / then” statements so that when we are searching by “searchTerm” it will use one query and another if we are submitting a genre”. While we're at it, let's also adapt our query so that it is a little more secure by using named parameters. These are submitted separately from the main SQL and are examined more closely for issues from bad actors such as SQL injection attacks and so on.

if(searchTerm.len() != 0) {
     return queryExecute("
        SELECT * from books
        INNER JOIN publishers
        ON books.publisherId=publishers.id
        WHERE title like :searchTerm
        OR isbn13 like :searchTerm",
        { searchTerm: '%#trim(arguments.searchTerm)#%'})
 
} else if( genreId > 0){
    return queryExecute("
        SELECT * from books b
        INNER JOIN genresToBooks gtb
            ON b.isbn13 = gtb.isbn13
        WHERE gtb.genreid=:genreId ",
        {
           genreId:arguments.genreId
        }); 
}

Notice how both queries have the book data as well as the name of the publisher so that our “view” code will be able to work regardless of which query is returned. Since our queryToBooks table only allows each combination of book and genre to be entered once, we shouldn't get duplicate records of books.

Bonus

Can you make it so that you can show the genres to which a book belongs on the OneResults.bxm page? How about clicking on the name of that genre to get other books in the same genre?

Can you make it so that you can click on the publisher's name in the book listing and get books by that publisher?

PreviousBuilding the GenreNav.bxmNextEnd of Project Checkllist

Last updated 2 months ago