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>';
            }
2

4 Answers 4

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:';
  ...
}
Sign up to request clarification or add additional context in comments.

Comments

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 }

Comments

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">';

   ....
}

2 Comments

Doesn't $i++; $final = ($i + 1 == $total)? true : false; do the same as this (shorter) code: $final = ++$i + 1 == $total
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: hastebin.com/pomaviqare.php
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' : '') . '">';    

    [...]

 }

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.