1

I have a block of code thats working perfectly to pull data about different office locations.

What I would like to do is be able to make the last iteration of this loop change the div class to something else so I can apply a different set of css styles.

$fields = get_group('Offices');

            foreach($fields as $field){
                echo'<div class="oloc">';
                if($locationVar==NULL || $locationVar!=$field['office-location'][1]) {
                    echo '<a name="' . strtolower(str_replace(' ', '-', $field['office-location'][1])) . '"></a><h3>' . $field['office-location'][1] . '</h3>';
                    $locationVar = $field['office-location'][1];
                } else {
                    echo "<br />";
                }

                if($field['office-gm'][1]){
                    echo '<div class="gm"><img src="http://maps.googleapis.com/maps/api/staticmap?center=' . $field['office-gm'][1] . '&zoom=9&size=250x250&markers=color:blue|label:A|' . $field['office-gm'][1] . '&sensor=false"></div>';
                }

                if($field['office-name'][1]){
                    echo '<strong>' . $field['office-name'][1] . '</strong><br /><br />';
                }

                if($field['office-phone'][1]){
                    echo 'Phone: ' . $field['office-phone'][1] . '<br />';
                }

                if($field['office-fax'][1]){
                    echo 'Fax: ' . $field['office-fax'][1] . '<br />';
                }

                if($field['office-address'][1]){
                    echo '<br />Address:<br />' . strip_tags($field['office-address'][1], '<br><br />') . '<br />';
                }

                if($field['office-webpage'][1]){
                    echo 'Web: ' . '<a href="' . $field['office-webpage'][1] . '">Office Webpage</a><br />';
                }

                if($field['office-email'][1]){
                    echo 'Email: ' . '<a href="' . $field['office-email'][1] . '">Office Email</a><br />';
                }
                if($field['office-emp'][1]){
                    echo 'Jobs: ' . '<a href="' . $field['office-emp'][1] . '">Employment Application</a><br />';
                }

                if($field['office-fb'][1]){
                    echo 'Facebook: ' . '<a href="' . $field['office-fb'][1] . '">Facebook</a><br />';
                }
                if($field['office_office_twitter'][1]){
                    echo 'Twitter: ' . '<a href="' . $field['office_office_twitter'][1] . '">Twitter</a><br />';
                } 
            echo '</div>';
            }
  • 1
    See this question: http://stackoverflow.com/q/665135/902654 – someone Oct 11 '11 at 00:52
  • 1
    Depending on your intended level of browser support, you could just use the [`:last-child` selector](http://reference.sitepoint.com/css/pseudoclass-lastchild) – Phil Oct 11 '11 at 00:52

4 Answers4

1

For cases like these you can use a CachingIterator and the hasNext() method:

$fields = get_group('Offices');

$it = new CachingIterator(new ArrayIterator($fields));
foreach($it as $field)
{
  ...
  if (!$it->hasNext()) echo 'Last:';
  ...
}
hakre
  • 193,403
  • 52
  • 435
  • 836
0

Put every echo in a var, this class definition in a other var. Then at the end of your foreach you check like so:

$i = 0;
foreach(...

if( $i == count($fields) ) { // change class }
Wesley
  • 2,190
  • 17
  • 26
0

Try this

$i = 0;
$total = count($fields);
$final = false;
foreach($fields as $field){

   $i++;
   $final = ($i + 1 == $total)? true : false;

   if($final)
       echo'<div class="NEW_CLASS">';
   else
       echo'<div class="oloc">';

   ....
}
brenjt
  • 15,997
  • 13
  • 77
  • 118
  • Doesn't `$i++; $final = ($i + 1 == $total)? true : false;` do the same as this (shorter) code: `$final = ++$i + 1 == $total` – Luc Sep 04 '12 at 18:51
  • I don't think this post is better than any other, that's why. You got more rep for the upvote than from the downvote anyway. Also, you could optimize it like this: http://hastebin.com/pomaviqare.php – Luc Sep 06 '12 at 08:37
0

First you should get the total count of the offices so that when you are on the last iteration, you can do something about it.

$fields = get_group('Offices');
$fields_count = count($fields);

$i = 0;

foreach ($fields as $field) {

    $is_final = ++$i == $fields_count;

    echo '<div class="oloc' . ($is_final ? ' oloc-final' : '') . '">';    

    [...]

 }
voldomazta
  • 1,300
  • 1
  • 10
  • 19