Support

Account

Home Forums Add-ons Repeater Field How to display a value in array ones if it exists in the sub-field?

Solved

How to display a value in array ones if it exists in the sub-field?

  • Hi Everyone,

    I’m new to this plugin. So here’s my concern.

    I created a field group named “State Lists” and added repeater_fields: State_Name which is a Select_type (values are: Atland, California etc… ), state_link (page_link), City_name(text), and city_link(page_link).

    In my Directory page I will used this custom fields. This page will somehow look like Craigslists index that displays the State and Cities under it. I created a template “state.php” see it below. But my problem is the state keeps on repeating to since it’s part of the repeater-field. My goal is to display only ones the STATE_NAME and displays all the CITY_NAME if they have all the same STATE_NAME.

    
    $rows = get_field('state_lists');  //this is the ACF instruction to get everything in the repeater field
      $i = 0;  //this is a variable which sets up the counter starting at 0
    
        if($rows)
        {
          echo '<ul class="state-list">';
          
          foreach($rows as $row)
    
          { 
            echo '<li class="repeater-item one-fourth">            
                      <a title="'. $row['state_name'] . '" href="' . $row['state_link'] .'">
                      <span class="repeater-title">' . $row['state_name']. '</span>
                      </a>
                        <ul class="city_list">
                            <li class repeater-item one-third">
                              <a title="'. $row['city_name'] . '" href="' .$row['city_link'] . '">
                              <span class="repeater-title"> ' .$row['city_name']. ' </span>
                            </li>
                        </ul>
                    </li>' ;
          } 
          echo '</ul>';
        }

    Is there a way of working this out? I need your assistance guys!

    Regards!

  • Hello I need help guys! Cheers!

  • Hi @kidcajes

    Can you please clarify what you mean by:
    “My goal is to display only ones the STATE_NAME and displays all the CITY_NAME if they have all the same STATE_NAME.”

    This is a bit confusing, but I think you want to display all the repeater field data grouped into headings of city_name and state_name?

    perhaps attach a drawing of the desired visual output?

    Thanks
    E

  • Hi E,

    Target Output:

    Alabama (state_name)
    – auburn (city_name)
    – birmingham
    – dothan

    Illinois
    – bloomington
    – urbana
    – chicago

    My Current Output:
    Alabama
    – auburn
    Alabama
    – birmingham
    Alabama
    – dothan

    Illinois
    – bloomington
    Illinois
    – urbana
    Illinois
    – chicago

    I hope this clear things up!

    Cheers

  • Hi @kidcajes

    Thanks for the info.
    You will need to loop through your rows and first create an array which holds only the different states.

    Next, loop through these states and then find all the rows which match the current state.

    Something like this should work:

    
    <?php 
    
    $states = array();
    $rows = get_field('state_lists'); 
    
    if( $rows )
    {
    	foreach( $rows as $row )
    	{
    		// vars
    		$state = $row['state_name'];
    		$link = $row['state_link'];
    		
    		
    		// add state to list of states
    		if( ! in_array($state, $states) )
    		{
    			$states[] = array(
    				'title' => $state,
    				'link' => $link,
    			)
    		}
    		
    	}
    }
    		
    		
    
    if( $states )
    {
    	echo '<ul class="state-list">';
    	
    	foreach( $states as $state )
    	{
    		echo '<li class="repeater-item one-fourth">
    				<a title="'. $state['title'] . '" href="' . $state['link'] .'">
                    	<span class="repeater-title">' . $state['title']. '</span>
                    </a>
                    <ul class="city_list">';
                        
                    	foreach( $rows as $row )
                        {
                        	// validate
                        	if( $state != $row['state_name'] )
                        	{
    	                    	continue;
                        	}
                        	
    	                    echo '<li class repeater-item one-third">
    	                          <a title="'. $row['city_name'] . '" href="' .$row['city_link'] . '">
    	                          <span class="repeater-title"> ' .$row['city_name']. ' </span>
    	                        </li>';
                        }
                            
    				echo '</ul>
                  </li>' ;
    		
    	}
    	
    	echo '</ul>';
    }
     ?>
    
  • Hi E,

    Thanks! It works. I also tried using nested repeater and it works better and easier.

    Cheers!

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

The topic ‘How to display a value in array ones if it exists in the sub-field?’ is closed to new replies.