Support

Account

Home Forums Front-end Issues Select – if null (nothing selected)

Solved

Select – if null (nothing selected)

  • I have a select field where the user can select one, two or as many as they need and it outputs a specific image related to what is selected.

    <?php 
    	$Logos = array(
    		'Aqua-Max' => $image_url. '/product-logos/aqua-max.jpg',
    		'Aquavantage' => $image_url. '/product-logos/aquavantage.jpg',
    		'GripTop' => $image_url. '/product-logos/grip-top.jpg',
    		'Inter-ax2' => $image_url. '/product-logos/inter-ax2.jpg',
    		'Opt-Emax' => $image_url. '/product-logos/opt-emax.jpg',
    		'Pamrex' => $image_url. '/product-logos/pamrex.jpg',
    		'Ult-Emax' => $image_url. '/product-logos/ult-emax.jpg',
    		'Warrior' => $image_url. '/product-logos/warrior.jpg',
    		'Waterway' => $image_url. '/product-logos/waterway.png',
    	);
    	$values = get_field('product_logo');
    	foreach ($values as $value) {
    	?>
    		<?php if( !empty($values) ): ?>
    			<div class="product_logo">
    		  	<img src="<?php echo $Logos[$value] ?>" alt="<?php the_field('product_logo') ?>">
    	  	</div>
    	<?php endif; ?>
    	<?php 
    	}
    ?>

    However when nothing is selected, it still shows and gives an error;

    <img src="<br />
    <b>Notice</b>:  Undefined index:  in <b>C:\laragon\www\pam-saint-gobain\wp-content\themes\pam\single-products.php</b> on line <b>41</b><br />
    " alt="">

    I even have;

    <?php if( get_field('product_logo') ): ?>

    Beforehand but it still shows that error when even nothing is selected.

  • Solved it with;

    <?php if( $value == '' ): ?>
    	
    <?php else: ?>
    	<div class="product_logo">
      	<img src="<?php echo $logos[$value] ?>" alt="<?php the_field('product_logo') ?>">
    	</div>
    <?php endif; ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Select – if null (nothing selected)’ is closed to new replies.