Returning array values in a PICK column
I have 2 pods, projects and types. I have a PICK column that is types and multiple types can be assigned to a project. I am sure this is simple, but I cannot figure out how to return all of the types chosen. I can successfully return the first value, projecttype.name, but that is it. Any with multiple selections return arrays. I have tried looping through the array, but can't get it to work. Is that the way I need to go?
Thanks for any suggestions.
Shawn
8 Answers
Hey Shawn, You need to use a display helper to enumerate the elements of the array. Check this link
http://podscms.org/codex/display_helpers/
on how to use display helpers. There's a few examples there that might be of help too. But here's a display helper that will list out your array elements as a comma separated list.
Try this:
<?php
if(is_array($value) && !empty($value)) {
$v = $value;
$comma_separated = implode('<br/>', $v);
// display the list
echo $comma_separated;
} else {
$v = $this->get_field($name);
// display the single group
echo $v;
}
?>
edited Dec 4 '11 at 5:27
Thanks. I get that, but 2 things.
When I am editing my pod column, here is no Display Helper dopdown.
If I try to use it in my template, I put the code in and assign it a variable, then echo the variable. That doesn't seem to work.
`$project_projecttype = $pods->pod_helper('pick_array', $pods->get_field('projecttype'));
echo $project_projecttype; `
pick_array is what I named the Display Helper.
I am I even close?
edited Dec 6 '11 at 7:27
If you're using this in a Pods Template, your code should look like this:
{@projecttype, pick_array}
If you're using this in a Pods Page or a WP Template, I wouldn't create a helper, I'd just write the PHP code:
$pt = $pods->get_field('projecttype');
if(is_array($pt) && !empty($pt)) {
echo implode(', ', $pt);
}
If that still isn't working, you might need to inspect what that array looks like to make sure it doesn't have multiple indexes (e.g. implode(', ', $pt[0]) ):
$pt = $pods->get_field('projecttype');
echo "<pre>\n"; print_r($pt); echo "</pre>\n";
I am using a WP template.
This is what happened before when I was tying other things...Wherever I place the code, the page stops executing and is just blank after that. It is like it breaks the loop.
Here is the page without the code from above. http://test.cmfi.org.php5-10.websitetestlink.com/partner
It sounds like you've got a syntax error in your PHP code somewhere. If you can, run a syntax check on your dev machine "php -l myfile.php" or run it through an online tool.
That is a great online tool. I am getting this error:
array($pt) && !empty($pt)) { echo implode(', ',$pt);
Not sure how this reports the error, but looking at the string you've got, you've either missed the underscore in is_array() (array() is a declaration) or you're missing the closing } in your if statement.
I really appreciate your help with this Chris.
I have it working now. Not exactly sure what was wrong, but went in and retyped everything and it worked after that. May have had some gremlins in there.


