I have two unlimited multi-value cck fields in one content type: field_before and field_after . In my custom node template file, I need to match up the deltas of each before and after value so I can display my content like this:

Before 0
After 0
Before 1
After 1
Before 2
After 2

So how do I get this output in my node.tpl file?


I would suggest putting this in a node-TYPE.tpl.php instead of node.tpl.php unless this is your only content type, but anyway, here is a quick start to accomplishing what you asked for using a simple for loop:

<?php $max = max(count($node->field_before), count($node->field_after)); ?>
<?php for ($i = 0; $i < $max; $i++): ?>
  <div class='before'>Before <?php print !empty($node->field_before[$i]) ? $node->field_before[$i]['view'] : ''; ?></div>
  <div class='after'>After <?php print !empty($node->field_after[$i]) ? $node->field_after[$i]['view'] : ''; ?></div>
  <?php if ($i != ($max - 1)): ?>
    <div class='separator'>------</div>
  <?php endif; ?>
<?php endfor; ?>

This can obviously be tweaked to add a default value if a field lengths are different, or skip the checking if you know that each "before value" will have a corresponding "answer value".

Also, if using default "unlimited fields", a user could rearrange them via drag and drop and potentially screw up your data. If this is a concern for you, you may want to look at Multigroup for CCK3.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy