0

I have a sub field that have several different values. What I'm trying to do is count the number of rows if they're equal to Mayor.

So my code looks like this:

if( have_rows('candidates') ): 
  while( have_rows('candidates') ): the_row(); 
    $cPosition = get_sub_field('candidate_position');
    if($cPosition == "Mayor"){
      $numrows = count( get_sub_field( 'candidate_position' ) );
    };
  endwhile;
endif;

echo $numrows;

However, I'm getting back ALL the rows. So like 17 instead of 8 or so.

Here is the loop giving me my results:

<div class="ballot-box ballot-box--mayor">
      <h3 class=""><?php echo _e('Mayor', 'HTML5Blank') ?></h3>

      <p>
        <?php echo _e('The Mayor of San Antonio works closely with City Council to pass laws and create policies for the City, as well as the City Manager, who serves as the City’s chief administrator. While City Council members are elected by district, the Mayor represents all of San Antonio (for a two-year term, with a four-term limit). Candidates are listed here as they will appear on the ballot. All information shared is public information.', 'HTML5Blank') ?>
      </p>
      <p class="text-med"><strong>
        <?php echo _e('Candidates:', 'HTML5Blank') ?> 
        <?php  
          if (have_rows('candidates')):
            $total = 0;
            while (have_rows('candidates')): the_row(); 
              $position = get_sub_field('candidate_position');
              if ($position == "Mayor") {
                $total++;
              };
            endwhile;
            echo $total;
          endif;
        ?>

        </strong></p>
      <p><a class="show-hide" role="button" data-toggle="collapse" href="#mayorCollapse" aria-expanded="false" aria-controls="mayorCollapse"><i class="fa fa-caret-down text-med" aria-hidden="true"></i> 
        <span class="toggle-text"><?php echo _e('Show:', 'HTML5Blank') ?> </span> <?php echo _e('Candidates', 'HTML5Blank') ?> </a></p>

      <div class="collapse" id="mayorCollapse">
        <?php 
            if( have_rows('candidates') ): 
        ?>
          <div class="option-box option-box--candidate">
            <?php 
              while( have_rows('candidates') ): the_row(); 
                $cPosition = get_sub_field('candidate_position');
                $cName     = get_sub_field('candidate_name');
                $cImage    = get_sub_field('candidate_photo');
                $cWebsite  = get_sub_field('candidate_website');
                $cPhone    = get_sub_field('candidate_phone');
                $cFB       = get_sub_field('candidate_facebook');
                $cTW       = get_sub_field('candidate_twitter');
                $cIG       = get_sub_field('candidate_instagram');

                if($cPosition == "Mayor"){
            ?>
            <div class="row">
              <div class="col-sm-3">
                <?php if($cImage){ ?>
                  <img class="responsive" src="<?php echo $cImage ?>" alt="<?php echo $cName; ?>" />
                <?php }else{ ?>
                  <img class="responsive mobile-hide" src="<?php echo get_stylesheet_directory_uri(); ?>/images/no-photo.png" alt="no photo provided" />
                <?php } ?>
              </div>

              <div class="col-sm-9">
                <h4 class=""><?php echo $cName; ?></h4>
                <div class="">
                  <?php if($cWebsite){ ?>
                    <a class="btn btn-sm btn-sa-dark-aqua" target="_blank" href="<?php echo $cWebsite ?>" title="view website" onclick="trackOutboundLink('<?php echo $cWebsite; ?>', true, '<?php echo $cName; ?> Website');">Website</a>
                  <?php } ?>
                 <!--  Fililng document
                  <a class="btn btn-sm btn-sa-dark-aqua" target="_blank" href="{{ candidate.filing }}" title="view filing document" onclick="trackOutboundLink('{{ candidate.filing }}', true, '<?php echo $cName; ?> Filing');">Filing Document</a>
                -->
                </div>

                <div class="social-icons">
                  <?php if($cFB){ ?>
                    <a class="text-xl nounderline" target="_blank" href="<?php echo $cFB; ?>" title="view facebook page">
                      <i class="fa fa-facebook-official" aria-hidden="true"></i>
                    </a>
                  <?php } ?>
                  <?php if($cTW){ ?>
                    <a class="text-xl nounderline" target="_blank" href="<?php echo $cTW; ?>" title="view twitter page">
                      <i class="fa fa-twitter-square" aria-hidden="true"></i>
                    </a>
                  <?php } ?>
                  <?php if($cIG){ ?>
                    <a class="text-xl nounderline" target="_blank" href="<?php echo $cIG; ?>" title="view instagram">
                      <i class="fa fa-instagram" aria-hidden="true"></i>
                    </a>
                  <?php } ?>
                </div>

              </div>
            </div>
            <?php }; //end if ?>
            <?php endwhile; ?>
          </div>
        <?php endif; ?>
      </div>
    </div><!--./ballot-box -->

Any and all help is appreciated.

1

PHP's count() function is generally used to count elements in an array, but you're counting the value of a string, and that always returns 1. You can just add 1 in your if statement.

Something like this:

if (have_rows('candidates')):
  $total = 0;
  while (have_rows('candidates')): the_row(); 
    $position = get_sub_field('candidate_position');
    if ($position == "Mayor") {
      $total++;
    };
  endwhile;
  echo $total;
endif;

Other than that, the code actually looks right. Where are you initializing $numrows and where are you checking the value? Do you get a different value using the code above?

  • I still get 1 with your code. But your code does make sense. Not sure why it's not counting correctly. I was using the code from here: support.advancedcustomfields.com/forums/topic/… – Lz430 Mar 28 '19 at 16:02
  • The first thing I would check is that you're actually getting the correct value when you're calling get_sub_field('candidate_position'). Print out each iteration of candidate_position and check if it contains around eight Mayor instances. Because note that you're iterating over candidates, but then calling an entirely different field for counting. Those numbers may not be the same. – NightHawk Mar 28 '19 at 16:06
  • It does. Right below that I have a while loop. The loop is returning everything correctly. I posted the loop code above – Lz430 Mar 28 '19 at 16:10
  • When you said you get 1 with my code, are you saying you're getting a total of just 1? – NightHawk Mar 28 '19 at 16:15
  • 1
    Not sure what happened, but the original answer by NightHawk is working now. Not sure if I was cached or what. Thanks!!! – Lz430 Mar 28 '19 at 17:27

Your Answer

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

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