Invision Community 4: SEO, prepare for v5 and dormant account notifications By Matt Monday at 02:04 PM
coert_g Posted May 12, 2017 Posted May 12, 2017 We're looking to use a multi select box on our one submission forms (in this case, facilities at a business). This is then naturally stored in the database as an array. I can print the array with the standard call to the field within the pages template. But was wondering if there's an easy way to perform queries on that array. Such looking for value X in the array and if true, then showing something. In practice, we're wanting to see if a facility array contains the value Swimming Pool and then show a tick next to the text Swimming Pool, and if there is a value in the array called Camping, then show a tick next to the text "Camping'. Thanks
clearvision Posted May 12, 2017 Posted May 12, 2017 First I believe multiselect is stored as a comma separated string in the database. When you say "queries" on the array, are you meaning you just want to process the record data for that field in your template record loop (or record display), or you actually want to do a database query on that field to return certain records? If you just want to process the field during the template loop (or record display) then you could do string search on the data, or explode it into an array around the comma and do a in_array to test for presence of your value.
Aiwa Posted May 12, 2017 Posted May 12, 2017 SELECT * FROM table WHERE facility IN ('Swimming Pool'); Or, get the data and explode it so you have an array you can search using php's array functions. $array = explode(',',$string);
clearvision Posted May 12, 2017 Posted May 12, 2017 So I'll assume you don't mean query the database but in your record or listing display want something like ABC ✔ DEF GHI ✔ If so, in the field edit you can check custom formatting for the display (listing or record) and put below in (with classes or whatever for formatting) {{$selarray=array_map('trim',explode(',',$value));}} <ul> <li>ABC {{if in_array('abc',$selarray)}}✔{{endif}}</li> <li>DEF {{if in_array('def',$selarray)}}✔{{endif}}</li> <li>GHI {{if in_array('ghi',$selarray)}}✔{{endif}}</li> </ul> Note that explode is not good enough on the comma data string as it has spaces (at least after the ,) inserted so the trim takes out leading and trailing spaces so it will match.
coert_g Posted May 16, 2017 Author Posted May 16, 2017 Thanks a lot for the responses, will take a look at the suggestions @clearvision You're spot on with what we're hoping to achieve. We have a field setup in a pages application that is an array of facilities, just like you show. And wish to do the same thing. Though I have tried doing a test now using the code you gave above, but doesn't seem to be pulling through correctly. I have: {{$facarray=array_map('trim',explode(',',$array));}} {{if in_array('1test',$facarray)}}yes{{else}}no{{endif}} {{if in_array('2test',$facarray)}}yes{{else}}no{{endif}} {{if in_array('3test',$facarray)}}yes{{else}}no{{endif}} The array in question is named simply, array. And it has 2 of those 3 values as correct (There is both 1test and 3test in the array). But my test above keeps returning no on all counts. I wouldn't need to include any $record prefixes would I? Thanks for the help
clearvision Posted May 16, 2017 Posted May 16, 2017 The code I wrote goes in the field definition, not the template. This would be a select field. In the field definition you can define the display output as custom. You MUST use $value though, not the name of the array. If you look below the text area it tells you what values you can use in your custom code. Note if that does not work you can just display the value and it should be a comma separated string, if it has html or some other format then it is not the select fields "raw" data.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.