I’m trying to create a search function where a user can find a list of events by selecting multiple music styles (via checkbox) that they’re interested in. Each event can have multiple music styles attached to it, and if one of the music styles searched for matches with an event – then it should show up.
This is where I’m stuck, because since each event has multiple music styles attached, they have to be stored in a different table.
One table events_music_styles contains the event_ID and music_style_ID
And the second, music_styles contains the names of those music styles in different languages.
This is how I joined them in MySql and it works nicely when I want to show all events and their corresponding music styles.
SELECT
events.ID,
events.EVENT_NAME,
GROUP_CONCAT(music_styles.MUSIC_STYLE_NAME) AS MUSIC_STYLE_NAME
FROM events
INNER JOIN events_music_styles
ON events.ID = events_music_styles.event_id
INNER JOIN music_styles
ON events_music_styles.music_style_id = music_styles.id
GROUP BY events.ID
However, when I submit a search query, the data variable looks like this: var MusicStyles = 1,2,3,5,20 …. Each of those numbers should be the ID of events_music_styles.
How would I split those numbers up and send them to my php file which looks like this?
var MusicStyles = $("#music").val();
$.ajax({
type: "POST",
url: "do_search.php",
data: {
dataFromDate: FromDate,
dataToDate: ToDate,
dataMusicStyles: MusicStyles,
dataLocations: Locations,
dataFromPrice: FromPrice,
dataToPrice: ToPrice
}
You can leave the javascript part as it is. On the PHP side you can use the comma separated list in an SQL
IN()statement.Note: This is just an example. Of course you should validate the incoming $_POST values before using them in a MySQL statement.