Wildcards - Looking Up a Range of Similar Fields?
5 posts by 3 authors in: Forums > CMS Builder
Last Post: August 23, 2011 (RSS)
By Perchpole - August 22, 2011
I'm trying to build an array from a selection of similarly entitled CMSB fields. It looks something like this...
$myArray = array($record[0]['field1'], $record[0]['field2'], $record[0]['field3'], $record[0]['field4'], $record[0]['field5'], $record[0]['field6']);
As you can see, each field name is almost identical. Only the number on the end is different.
I wondered if, instead of having to identify each field in the array individually, I could use some kind of wildcard to lookup a spread of fields with similar names.
A sort of...
$record[0]['field*']
...approach?!
Any help would be most welcome.
:0)
Perchpole
Re: [Perchpole] Wildcards - Looking Up a Range of Similar Fields?
By Jason - August 22, 2011
Could you give an example of how you would use this wildcard/array? Let us know and we'll see if we can make any suggestions.
Thanks
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Wildcards - Looking Up a Range of Similar Fields?
By Perchpole - August 22, 2011
I'm trying to refine a system for choosing which margin widgets appear on a page.
Currently, when a user creates a category page he can choose 6 widgets to appear in the margin. He/she does this by means of 6 identical drop-down menus listing the available widgets.
So,
field1 might equal widget5
field2 might equal widget3
field3 might equal widget8
field4 might equal widget1
and so on...
Each selection returns a number - which forms an array. When the page is called, I use some very basic code to pull the chosen widgets into the margin of the page.
It's basic and rather clunky - but does the job!
(NB: Initially, instead of 6 lists, I used just one multi-select menu. It was a lot cleaner but it didn't allow you to manipulate the widget order.)
I posted my question because I thought I might learn something new about constructing arrays. It seemed even more clunky to have to list each field in the array code. I just wondered if there was a smarter way of doing it?
:0)
Perch
Re: [Perchpole] Wildcards - Looking Up a Range of Similar Fields?
By robin - August 23, 2011
You could use a loop and an if to get all fields that start with 'field'. Something like:
foreach($record[0] as $key => $value) {
if(substr($key,0,5) == 'field') {
$myArray[] = $value
}
}
Hope that helps,
Robin
Programmer
interactivetools.com
Re: [robin] Wildcards - Looking Up a Range of Similar Fields?
By Perchpole - August 23, 2011
Brilliant! Just what I was hoping for.
Thanks,
:0)
Perch