Support

Account

Home Forums General Issues Display image on condition of selection

Helping

Display image on condition of selection

  • I would like to display different images depending on the value of a select dropdown. I have 5 options in the select dropdown (1 to 5) and a different image for each that I would like to display (image1 – image 5)
    So far I have this code adapted from a previous post

    <?php 
    $activity_rating = get_field('activity_rating');
    <?php if( is_array($activity_rating) && in_array( '1', $activity_rating ) ): ?>
    <?php // the user has selected '1' select dropdown. Display image 1 ?>
    <?php //Display image 1 ?>
    <?php else if ( is_array($activity_rating) && in_array( '2', $activity_rating ) ): ?>
    <?php //Display image 2 ?>
    <?php else if ( is_array($activity_rating) && in_array( '3', $activity_rating ) ): ?>
    <?php //Display image 3 ?>
    <?php else if ( is_array($activity_rating) && in_array( '4', $activity_rating ) ): ?>
    <?php //Display image 4 ?>
    <?php else if ( is_array($activity_rating) && in_array( '5', $activity_rating ) ): ?>
    <?php //Display image 5 ?>
    <?php endif; ?>

    Any ideas very much appreciate!
    p

  • Hi,

    If you have a select field without multiple select it should just return a value not an array..

    This code might get you in the right direction:

    
    <?php 
    $activity_rating = get_field('activity_rating');
    
    switch($activity_rating){
    	
    	case '1':
    		$img = 'urltoimage';
    		break;
    	case '2':
    		$img = 'urltoimage2';
    		break;
    	case '3':
    		$img = 'urltoimage3';
    		break;
    	case '4':
    		$img = 'urltoimage4';
    		break;
    	case '5':
    		$img = 'urltoimage5';
    		break;
    	case default:
    		$img = 'fallbackimage';
    		break;
    		
    	echo '<img src="' . $img . '" alt="" />';
    	
    }
    ?>
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.