Jump to content

Problems with checkbox search function


sc00tz

Recommended Posts

I have a simple website set up with a few pages.  The first page is a form with 1 pulldown menu and 6 checkboxes.  The pulldown menu is a list of books (Book1, Book2, Book3, Book4,  Book5) and the 6 checkboxes characterize the book (long, fiction, historical, thriller, crime, young adult).  Users select one of the books and then select as many checkboxes as they like.  This data is submitted to a MySQL table.  The Book value is submitted to the 'book' row, and the checkboxes are submitted as an imploded array to the 'checkboxes' row.

 

There are pages set up for each book (e.g. Book1.php) which display all the different form submissions (like little reviews).  This has been no problem so far.

 

However, I've run into a problem once I tried to create a search page.  The search page is just a form with the same 6 checkboxes.  Users can click whichever characterizations they want, and I want it to spit out a list of books that have been reviewed as having those characterizations.  So I set up this wildcard search:

SELECT books FROM table WHERE checkboxes LIKE '%$searchboxes%'

 

'searchboxes' is an imploded array posted from the search form (so the boxes that were check that the user is searching for).  Now there are 2 problems.

 

Problem 1: The wildcard search only matches the 2 imploded arrays when the selected boxes appear in the exact same order.  For example if someone reviewed Book1 as "fiction, historical, thriller," a search for "fiction, thriller" does not retrieve it (I presume because there's "historical" in the middle.  I want the search function to retrieve all entries with the searched for checkboxes, regardless of whether they have more than that.  I have tried several ways of getting around this problem but can't figure it out.

 

Problem 2: When the results of the search are echoed onto the "search_results.php" page, if the search matches more than one entry for the same book, that book's name will be displayed multiple times.  E.g. a search result might say "Book1, Book2, Book2, Book2, Book5."  I just want it to list Book2 one time only.  Is there any way to get around this?

 

I'm fairly new to MySQL so any help would be greatly appreciated.

Link to comment
https://forums.phpfreaks.com/topic/177006-problems-with-checkbox-search-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.