This is an excellent tutorial but I have one small issue. The database (I’ve inherited it from a Wordpress site) has the array stored with the pipe ( | ) separator, eg. Single|Double|Parking. The checkboxes are dynamic (I’ve got those working fine) but I think the issue is because the array is being sent comma-separated but the database is pipe-separated.
Actually you don’t need to change the value returned by the checkboxes, it’s in a right format and just splitting it is enough.
The issue is with the DB value, which is not in the required type/format. You store the values in a single field as a pipe separated string, while our example here shows that they are stored separately in separate rows, which returns them as an array.
You will either have to use some custom query for this, or use a separate relational table to store the values in separate rows.
Thanks @Teodor. I’ve been thinking about this and the easiest solution is to simply do a search and replace on the database and change all the pipes to commas. I can do that easily and there aren’t many scripts I’ll need to change to get it all working again. I’ve been trying to build the new site using the same data structure as the existing Wordpress site but when the site launches, I’ll only need to import everything once so doing the search and replace then won’t be a problem.
That's the problem - logically it won't work with data stored like that.
You are comparing the string Single,Internet,Parking stored in your db field with an array of values, returned by the checkboxes. So: Single,Internet,Parking IN ['Internet', 'Parking'] won't return anything.
You just need to store the amenities in a relational table, which stores the record ID and the amenity associated with it, just as:
I have a good understanding of relational databases and would normally use many-to-many tables for all things like this. However, because I’m using an existing database structure, I was trying to keep that structure. However, I shall write a script to convert the data from comma-separated fields to relational tables so it’s a better end product.