Another PICK Array question
First I want to say, thank you so much Chris for helping out all of us. I am sure you get tired of answering the same questions that are just different enough to mess someone up.
I am still working on this missionary-projects site. I don't think I have asked about this specific problem. On my project pages, I have a list of missionaries that are working on that project. It is a bi-directional pick. I am having trouble returning those missionaries in a list with as links to their page. Below is what I have, but my $project_missionaryslug returns an array in the link. Am I even approaching this the correct way?
<?php if(is_array($project_missionary) && !empty($project_missionary)) {
echo "<ul>\n<li><a href=\"/partner/discover-great-projects/missionary/$project_missionaryslug\">".implode('</li><li>',$project_missionary)."</a></li>\n</ul>";
}else{
echo $project_missionary;
}
?>
Here is the page,http://cmfi.org/partner/discover-great-projects/proyectoportugal and the list is down in the lower right.
Thanks again for any help.
12 Answers
I'm not really sure how you're currently generating that list on your site, but it does not appear to be through that code. To get rid of the "Array" in your output, you'd also have to implode the $project_missionaryslug variable. If you did that with what you have, it will make a mess of the html.
To get what you want, you need to loop through the pod items one by one. I'd do something like this, assuming $pod is your pod object:
if (0 < $pod->getTotalRows() ):
echo "<ul>\n";
while ($pod->fetchRecord() ):
printf ( "<li><a href='partner/discover-great-projects/missionary/%s'>%s</a></li>\n",
$pod->getField('project_missionary.slug'),
$pod->getField('project_missionary.name'),
);
endwhile;
echo "</ul>\n";
endif;
You could run a foreach on your two variables, but nothing guarantees that the arrays will be keyed the same, so the name of missionary 1 might end up with a link pointing to missionary 3.
I see what the %s is. There is some sort of error in that code, but I haven't found it yet. I am just getting a server error.
There was an extra comma at the end of line 6.
I am not getting anything returned. Here is the code I am using.
<?php if (0 < $project->getTotalRows() ):
echo "<ul>\n";
while ($project->fetchRecord() ):
printf ( "<li><a href='partner/discover-great-projects/missionary/%s'>%s</a></li>\n",
$project->getField('project_missionaryslug'),
$project->getField('project_missionary')
);
endwhile;
echo "</ul>\n";
endif;
?>
General question... When there is a PICK column, do you have to set up variables for each column you want to return? For instance the missionary.name, missionary.slug? Or just the fact that you have the one PICK column missionary.name in there, all the others are available by dot traversal?
edited Jan 26 at 3:01
Your code above isn't going to work unless you have single entry fields named project_missionaryslug and project_missionary. If they are pick fields this will return Array. Otherwise you will get nothing.
For your general question, say I have two pods: Things and Categories. In the Things pod, I have a PICK field called Category. If I have a single category associated with each thing, I can access any field in Categories using dot traversal, like getField('Category.name').
You can go deeper than two levels. Lets say there was also a Classes pod, and Categories has a pick field called Class. If I want to get the class of a Thing, I can use getField('Category.Class.name')
I do have those variables set up. I also tried it the way you had it with $project_missionary.name and .slug with the same results. I was going to use a conditional to see if $project_missionary was an array or not.
I was just thinking... Is it not necessary to define variables like you have in the examples if you use getField()? I know it is not a best practice...
Can you post all your code that generates this page? There is something up here, but I'm not seeing enough here.
My rule for defining a variable depends on how many times I am going to use that piece of data. If I'm only using it once, I just output the result from getField. If I'm going to be using that same piece of data a few times, I'll declare a variable for it.
Here you go.
`<?php
/* Template Name: Partner - Project POD*/
?>
<? get_header(); ?>
</head>
<body id="projects">
<div id="doc2" class="yui-t2">
<?php include('hd.php'); ?>
<div id="bd">
<div id="yui-main">
<div class="yui-b">
<div class="yui-g">
<div id="bodycolumn" class="gutter">
<!-- begin PODS -->
<?php
$found_project = false;
global $pods;
$project_slug = pods_url_variable(last);
$project = new Pod('projects', $project_slug);
//echo $project_slug;
if( !empty( $project->data ) )
{
$found_project = true;
// set our variables
$project_id = $project->get_field('id');
$project_name = $project->get_field('name');
$project_country = $project->get_field('location.name');
$project_map = $project->get_field('location.mapimage');
$project_projecttype = $project->get_field('projecttype.name');
$project_description = $project->get_field('projectdesc');
$project_summary = $project->get_field('summarydesc');
$project_mpowercode = $project->get_field('mpowercode');
$project_image = $project->get_field('projectimage');
$project_fundinggoal = $project->get_field('projectfundinggoal');
$project_fundingtext = $project->get_field('projectfundingtext');
$project_featured = $project->get_field('featuredproject');
$project_missionary = $project->get_field('r_missionary.name');
$project_missionaryslug = $project->get_field('r_missionary.slug');
$project_gallerylink = $project->get_field('gallerylink');
$project_nextgen = $project->get_field('nextgenpic');
$project_nggallery = $project->get_field('nggallery');
$project_nextgentag = $project->get_field('nextgentag');
$project_galleryimage = $project->get_field('galleryimage');
$project_video = $project->get_field('projectvideo');
$project_videolink = $project->get_field('videolink');
// data cleanup
$project_summary = wpautop( $project_summary );
$project_descriptiom = wpautop( $description, $br );
$project_image = $project_image[0]['guid'];
$project_map = $project_map[0]['guid'];
$project_galleryimage = $project_galleryimage[0]['guid'];
}
?>
<div id="content" class="narrowcolumn" role="main">
<?php if( $found_project ) : ?>
<div id="media">
<?php if($project_nggallery == ''){?>
<img src="<?php echo $project_image; ?>" class="main" width="330" />
<?php }else{
echo do_shortcode('[nggtags gallery='.$project_nextgentag .' id='.$project_nggallery.' w=164 h=155]');
?>
<?php } ?>
<div class="sharing">
<span class='st_facebook_hcount' st_title='<?php the_title(); ?>' st_url='<?php the_permalink(); ?>' displayText='share'></span><span class='st_twitter_hcount' st_title='<?php the_title(); ?>' st_url='<?php the_permalink(); ?>' displayText='share'></span><span class='st_sharethis_hcount' st_title='<?php the_title(); ?>' st_url='<?php the_permalink(); ?>' displayText='share'></span>
</div>
<div class="fundinggoal">
<?php
if(strlen($project_fundinggoal)>0) :
$goal= number_format($project_fundinggoal);
?>
<div class="fund"><goal><?=$project_fundingtext?> <!--$<?=$goal?>--></goal></div>
<?php
endif;
?>
<div class="give-btn">
<a href="https://p2p.paperlesstrans.com/default.aspx?i=cmfms&c3=<?php echo urlencode($project_name); ?>&c4=<?php echo $project_mpowercode; ?>"><img src="<?php bloginfo('template_directory'); ?>/_img/btn.give.png" width="75" height="29" /></a>
</div>
</div><!-- end .fundinggoal -->
<div class="location">
<h3>Location</h3>
<img src="<? echo $project_map; ?>" width="164" height="155" />
</div><!-- end .location -->
<div class="photogallery">
<?php
if($project_video != ''){ ?>
<h3>Project Video</h3>
<a href="<?php echo $project_videolink; ?>" class="fancybox">
<img src="<? echo $project_galleryimage; ?>" width="164" height="155" />
</a>
<?php }
?>
</div><!-- end .photogallery -->
<?php if($project_missionary != '') {?>
<div class="team">
<h3>Project Team Members</h3>
<?php if (0 < $project->getTotalRows() ):
echo "<ul>\n";
while ($project->fetchRecord() ):
printf ( "<li><a href='partner/discover-great-projects/missionary/%s'>%s</a></li>\n",
$project->getField('project_missionaryslug'),
$project->getField('project_missionary')
);
endwhile;
echo "</ul>\n";
endif;
?>
</div> <!-- end .team -->
<?php } ?>
</div><!-- end #project-media -->
<div id="project">
<?php echo '<h2>'.$project_name.'</h2>'; ?>
<div class="give-btn"><a href="https://p2p.paperlesstrans.com/default.aspx?i=cmfms&c3=<?php echo urlencode($project_name); ?>&c4=<?php echo $project_mpowercode; ?>"><img src="<?php bloginfo('template_directory'); ?>/_img/btn.give.png" width="75" height="29" /></a>
</div>
<ul class="project-meta">
<li><strong>Country:</strong> <?php echo $project_country; ?></li>
<li><strong>Project Type:</strong>
<?php if(is_array($project_projecttype) && !empty($project_projecttype)) {
echo implode(', ',$project_projecttype);
}else{
echo $project_projecttype;
}
?>
</li>
</ul>
<p><?php echo $project_description; ?></p>
</div>
<?php else: ?>
<div class="post">
<h2>Project Not Found</h2>
<div class="entry">
<p>Sorry, that project could not be found!</p>
</div>
</div>
<?php endif; ?>
</div>
<!-- end PODS -->
</div>
</div>
</div>
</div>
<div class="yui-b">
<div id="leftcolumn">
<div id="leftnav">
<h2><?php echo get_the_title(18); ?></h2>
<ul class="sidenav">
<?php wp_list_pages("title_li=&child_of=18&depth=2"); ?>
</ul>
<?
$sideurl = get('sidebarimagelink');
if ($sideurl){
echo "<a href='".$sideurl."'>" .get_image('SidebarImage'). "</a>";
}else{
echo get_image('SidebarImage');
}
?>
</div>
</div>
</div>
</div>
<?php get_footer(); ?> <?php wp_footer(); ?> </body> </html>`
Okay, I see now. You've got to change this line to reference your fields, not the variable names you've given them:
printf ( "<li><a href='partner/discover-great-projects/missionary/%s'>%s</a></li>\n",
$pod->getField('r_missionary.slug'),
$pod->getField('r_missionary.name'),
);
So this is what I have now:
<?php if (0 < $project->getTotalRows() ):
echo "<ul>\n";
while ($project->fetchRecord() ):
printf ( "<li><a href='partner/discover-great-projects/missionary/%s'>%s</a></li>\n",
$project->getField('r_missionary.slug'),
$project->getField('r_missionary.name')
);
endwhile;
echo "</ul>\n";
endif;
?>
Nothing is returned and no errors. I was supposed to use my pod object ($project) in place of $pod, right?
Any other ideas why this might not be working?
Thanks!
I see where my problem is. Since it is a pick, we've got to loop through all the possible pick choices to get at the missionaries assigned to the project. Try this:
$m_array = $project->get_field('r_missionary');
if ( is_array($m_array) && 0 < count($m_array) ):
echo "<ul>\n";
foreach ($m_array as $m):
printf ( "<li><a href='partner/discover-great-projects/missionary/%s'>%s</a></li>\n",
$m['slug'],
$m['name']
);
endforeach;
echo "</ul>\n";
endif;
That was perfect. Thank you so much Chris! And I actually understand what is going on...
