1

I've got a field image with unlimited number of value and a view who shows 12 of them from different node(in this case I can not use delta). Now instead of showing the 8th and the 11th pictures I want to show a blank box with text.

I would like to insert this loop so I installed view PHP and I wrote this code on the box "Output code"

$counter = 0;
foreach ($row->field_image as $image){
  if ($counter++ = 8){
  echo "something";
continue;
}
  if ($counter++ = 11){
  echo "something else";
continue;
}
print $images;
}

What am i doing wrong? How can I get the result I want? Thanks in advance

2 Answers 2

1

Those if($counter++ = 8) lines look whacky to me.

don't you mean:

$counter++;
if( $counter == 8 ) {
  /* do stuff */
}

checking for equality is == or === not =

1
  • Hi David, sorry it was my mistake, in my code it was == instead of = . Anyway my question was about the manipulation of the field with unlimited number of value. Your way to use the $counter++ outside the if statemants works better and it helped me. Thanks a lot. Anyway I will add the solution. Commented Jun 24, 2012 at 12:54
0

I've found the solution. I was trying to output 12 images from different contents. Each contents has got more than one image using one image field with number of value "unlimited". I was trying using View PHP module and adding something like this

$counter = 0;
foreach ($row->field_image as $image){
  if ($counter++ == 8){
  echo "something";
continue;
}
  if ($counter++ == 11){
  echo "something else";
continue;
}
print $images;
}

But I was getting just numbers 1 and 2 (probably the node id, I've got just two nodes for the moment.)

I start to go around the web with no result so I've started to play around with fields and the view theme (views-view-unformatted--portfolio--page-2.tpl.php). I found one problem there, each group of images of one node was in one row but I should have one image per row, does not matter if they were from the same content (I've got random sort criteria). So I went into the view, I clicked on the image field and under "Multiple field settings" I dunchecked "Display all values in the same row " so I could get them in different rows. I edited the views-view-unformatted--portfolio--page-2.tpl.php in this way

<?php 
$counter = 0;
 foreach ($rows as $id => $row) {
 $counter++;
 print '<div class="'.$classes_array[$id].'">';
 if ($counter == 8) {
   print 'Something';
 } elseif ($counter == 11) {
   print 'Something else';
 } else {
  print $row;
  }
  print '</div>';
  }
  ?>

Here we go. I've got the thing I wanted without using the View PHP. Thanks to David Meister for suggesting me to put the $counter++ outside the if statement, it works much much better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.