nsilva
nsilva

Reputation: 5614

PHP - if statement with multiple values

I have the following code:-

<ul class="grid effect-2" id="grid">

    <?php
    if( have_rows('gallery') ):
        while ( have_rows('gallery') ) : the_row(); ?>

            <?php
            $image_location = get_sub_field('image_location');

            if (empty($_GET['filter'])) {
                $image_filter = $image_location == 'Nottingham' || $image_location == 'Corby';
            } else {
                $image_filter = $image_location == $_GET['filter'];
            }

            ?>

            <?php 
            if($image_filter) {
            ?>

                <li><a class="fancybox" rel="gallery" href="<?php the_sub_field('image'); ?>" title="<?php echo $image_location . ' @ Planet Bounce ' . get_sub_field('image_location'); ?>"><img src="<?php the_sub_field('image'); ?>" alt="<?php echo get_sub_field('image_caption') . ' @ Planet Bounce ' . $image_location; ?>" /></a></li>

            <?php } ?>

        <?php endwhile;
    else : endif;
    ?>

</ul>

$image_location can either be 'Nottingham' or 'Corby' or both. The image filter is basically filtering which images are being shown.

Although the above code works, I don't think it is right to do it this way.

If anybody could help me with a better practise way of doing the same query that would be much appreciated.

If you need me to explain in better detail, please let me know.

Upvotes: 0

Views: 73

Answers (1)

Pupil
Pupil

Reputation: 23948

The condition can be wrapped in a two liner code with use of array.

$imageFilters = array('Nottingham'=>'Nottingham', 'Corby'=>'Corby');
$image_filter = isset($imageFilters[$_GET['filter']]) ? $imageFilters[$_GET['filter']] :  $_GET['filter'];

Upvotes: 1

Related Questions