Support

Account

Home Forums Front-end Issues Multiple Select Output

Solved

Multiple Select Output

  • Hi all, hoping someone can help please!

    I am trying to use multiple select field where the user back end selects apple, oranges, pears etc for example.

    I am trying to get the front end to show images of each one if selected. So far I have the following;

    <?php 
          $labels = array();
          $field = get_field_object('fruit');
          $values = get_field('fruit');
          foreach ($values as $value) {
          $labels[] = $field['choices'][ $value ];}
    
          echo implode(', ', $labels);
    ?>

    Can anyone help please so it shows each one separately and as an image?

  • How to you plan on associateing each select value with an image. The select field itself doesn’t have the images as values, or does it?

  • Hi John,

    Thanks for getting back to me, I’ve tried a few methods to output the selected values as an image but because they are multiple values it doesn’t work. When I set it as a single select it works perfectly and that’s my stumbling block…

    Thanks

  • I don’t understand how you are outputting even a single selection has an image? Where is the image tag coming from?

  • For single selection I used the following code;

    <?php if( get_field('association') == 'Apple' ): ?>
    	<img src="<?php echo get_template_directory_uri() ?>/dist/images/media/image-01.png" alt="">									
    <?php elseif( get_field('association') == 'Orange' ): ?>								
    	<img src="<?php echo get_template_directory_uri() ?>/dist/images/media/image-02.png" alt="">							
    <?php elseif( get_field('association') == 'Pear' ): ?>
    	<img src="<?php echo get_template_directory_uri() ?>/dist/images/media/image-03.png" alt="">
    <?php endif; ?>	

    But when I changed the settings to allow multiple selections the code above didn’t work so looked at alternatives

  • Ok, that makes a log more sense, for a multi select you’d need to loop through the values. I would suggest setting up the images in an array first just to make life easier and to remove the if/elseif logic

    
    $images = array(
      'Apple' => 'get_template_directory_uri()'./dist/images/media/image-01.png',
      'Orange' => 'get_template_directory_uri()'./dist/images/media/image-02.png',
      'Pear' => 'get_template_directory_uri()'./dist/images/media/image-02.png',
    )
    $values = get_field('association');
    foreach ($values as $value) {
      ?><img src="<?php echo $images[$value] ?>" alt=""><?php 
    }
    
  • Thanks for getting back to me, however it keeps breaking and giving the error;

    Parse error: syntax error, unexpected ‘/’

    I think it has something to do with only three ‘ being in each array string, e.g.

    'Apple' => 'get_template_directory_uri()'./dist/images/media/image-01.png',

    It needs four, but can’t work out where it needs to be

  • The '. on each line is backwards, dyslexic typing and should be '. Sorry, I don’t debug code I type here as examples.

    
    $images = array(
      'Apple' => 'get_template_directory_uri().'/dist/images/media/image-01.png',
      'Orange' => 'get_template_directory_uri().'/dist/images/media/image-02.png',
      'Pear' => 'get_template_directory_uri().'/dist/images/media/image-02.png',
    )
    $values = get_field('association');
    foreach ($values as $value) {
      ?><img src="<?php echo $images[$value] ?>" alt=""><?php 
    }
    
  • No worries, appreciate the help

    Sadly still not working;

    Parse error: syntax error, unexpected ‘png’

  • Cracked it!

    $images = array(
      'Apple' => get_template_directory_uri(). '/dist/images/media/image-01.png',
      'Orange' => get_template_directory_uri(). '/dist/images/media/image-02.png',
      'Pear' => get_template_directory_uri(). '/dist/images/media/image-02.png',
    );
    $values = get_field('codes');
    foreach ($values as $value) {
      ?>
      <img src="<?php echo $images[$value] ?>" alt="">
      <?php 
    }

    Thanks for you help John! 🙂

  • The editor I use hightlights all my typos that cause errors in php. It’s so easy to make tiny mistakes, like the single quote before the function call 😛

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.