0

I wrote this code to show certain field contents depending on the selection of the select field. Unfortunately, something doesn't work with the Elseif loop

$selection = get_field('location_select');

if ($selection == 'Leipzig' ) { ?>

<?php if( have_rows('leipzig', 4136) ): //child group field
 while( have_rows('leipzig', 4136) ): the_row();?>


<ul>
    <li>Bundesland: <?php the_sub_field('location_bundes_le',4136); ?></li>
    <li>Einwohner: <?php the_sub_field('location_einw_le', 4136); ?></li>
    <li>Fläche: <?php the_sub_field('location_area_le',4136); ?></li>
    </ul>
     <?php endwhile; ?>
           <?php endif; ?>
        <?php }?>
<?php
elseif ($selection == 'Dresden' ) {?>
<?php if( have_rows('dresden', 4136) ): //child group field
while( have_rows('dresden', 4136) ): the_row(); ?>
<ul>
    <li>Bundesland: <?php the_sub_field('location_bundes_dd',4136); ?></li>
    <li>Einwohner: <?php the_sub_field('location_einw_dd', 4136); ?></li>
    <li>Fläche: <?php the_sub_field('location_area_dd', 4136); ?></li>
 </ul>  

    <?php endwhile; ?>
           <?php endif; ?>
       <?php } ?>

<?php
elseif ($selection == 'Frankfurt' ) { ?>
<?php if( have_rows('frankfurt', 4136) ): //child group field
while( have_rows('frankfurt', 4136) ): the_row(); ?>             
<ul>
    <li>Bundesland: <?php the_sub_field('location_bundes_fr',4136); ?></li>
    <li>Einwohner: <?php the_sub_field('location_einw_fr',4136); ?></li>
    <li>Fläche: <?php the_sub_field('location_area_fr', 4136); ?></li>

 <?php endwhile; ?>
        <?php endif; ?>
        <?php } ?>  
<?php
else {
    echo "";
 } ?>
  • What specifically isn't working? For nested conditions it's usually wise to start with the most basic condition - i.e., create an "if - elseif - else" where each condition just outputs something like "This met the IF condition." From there, you can start nesting your additional "if" statements. This will let you identify exactly where things are going differently than you expect. – WebElaine Feb 21 at 18:03
  • i got an syntax error: unexpected elseif.... – tom84 Feb 21 at 18:24
  • Strip it down to 1 set of if/elseif/else conditions, get that working first, then add nested conditions one at a time so you can isolate which part is not working. It also helps if you indent your code more clearly and always use the same type of formatting - either colons or curly braces. Personally, I find using curly braces and tabbing to indent makes it much clearer where each if/else condition is opening and closing, which in turn makes it easier to tell where something is not working. – WebElaine Feb 24 at 15:43

Your Answer

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

Browse other questions tagged or ask your own question.