BuddyDev

Search

[Resolved] How to get all options from select field xprofile ?

Tagged: 

  • Participant
    Level: Initiated
    Posts: 5
    cobe on #30919

    How can i get select box fill with all data xprofile then added to search like that:
    NOTE: I have xprofile field called domaine whish has options.

    <form method="post">
      <select name="domaine">
    	 <?php foreach( $domaines as $domaine ) : ?> 
             <option value="<?php echo esc_attr( $domaine['id'] ); ?>" <?php $domaine['name']; ?>> 
              <?php  echo esc_html( $domaine['name'] ); ?> 
              </option> 
            <?php  endforeach ?> 
      </select>
      <button type="submit" value="execute">SEARCH</button>     
    </form>
    
    <?php if ( bp_has_members( kechweb_custom_ids( 'company' ) && $_POST['domaine']) : ?>
  • Keymaster
    (BuddyDev Team)
    Posts: 22201
    Brajesh Singh on #30933

    Hi,
    You can use the code like this to get an array of field names indexed by field id

    
    		$profile_fields = array();
    
    		$groups = bp_xprofile_get_groups(
    			array(
    				'hide_empty_groups' => true,
    				'fetch_fields'      => true,
    			)
    		);
    
    		foreach ( $groups as $group ) {
    			foreach ( $group->fields as $field ) {
    				$profile_fields[ $field->id ] = $field->name;
    			}
    		}
    
    

    I hope you can take from there.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    cobe on #30951

    First of all thank you Brajesh for your help.
    I tried your code i got all fields from xprofile, i want just to get all data saved from specific field called domaine (whish has list of activity like: architetural, science computer etc…)

    This is the code :

     $profile_fields = array();
    
    		$groups = bp_xprofile_get_groups(
    			array(
    				'hide_empty_groups' => true,
    				'fetch_fields'      => true,
    			)
    		); ?>
    		<select>
    		<?php
    		foreach ( $groups as $group ) {
    			foreach ( $group->fields as $field ) {
    				$profile_fields[ $field->id ] = $field->name; ?>
    				<option><?php echo $field->name;; ?></option>
    				<?php
    				
    			}
    		} ?>
    		</select>
  • Keymaster
    (BuddyDev Team)
    Posts: 22201
    Brajesh Singh on #30959

    Hi Cobe,
    Please clarify your requirement.

    I am sorry, i mot sure I understand it clearly.

    Also, is there something that Bp profile search is not able to assist you with?

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    cobe on #30963

    I have xprofile COUNTRY FIELD wish has list like that : (USA, Canada, GBR) and i want to print all data in listbox.
    this is exemple that i want:

    `<select>
    <option value=”USA”>USA</option>
    <option value=”Canada”>Canada</option>
    </select>`

    Note: I fill COUNTRY FIELD FROM administration->users->profile_field->Country_field_name

  • Keymaster
    (BuddyDev Team)
    Posts: 22201
    Brajesh Singh on #31035

    Hi,
    You may use something like this

    
    $field_id = 23;// please change with actual
    $field    = new  BP_XProfile_Field( $field_id );
    $options  = $field->get_children();
    echo "<select name='some_name'>";
    foreach ( $options as $option ) {
    	echo '<option value="' . esc_attr( $option->name ) . '">' . esc_html( $option->name ) . '</option>';
    }
    
    echo '</select>';
    
    

    Please change the field id and feel free to adapt as you please.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    cobe on #31037

    Thank you so much Brajesh well done!
    Now i want to get value selected and use it for filtring search like: select option Contry and select job of member with form search.

    
    <?php
    		$field_id = 94; // field 
    		$field    = new  BP_XProfile_Field( $field_id );
    		$options  = $field->get_children();
    		echo "<select name='brajesh-Country'>";
    		echo "<option value=''>Select country</option>";
    		foreach ( $options as $option ) {
    			echo '<option value="' . esc_attr( $option->name ) . '">' . esc_html( $option->name ) . '</option>';
    		}
    
    		echo '</select>';
    
    		//Select City
    		$field_id = 103;
    		$field    = new  BP_XProfile_Field( $field_id );
    		$options  = $field->get_children();
    		echo "<select name='brajesh-city'>";
                    echo "<option value=''>Select Job name</option>";
    		foreach ( $options as $option ) {
    			echo '<option value="' . esc_attr( $option->name ) . '">' . esc_html( $option->name ) . '</option>';
    		}
    
    		echo '</select>'; 
    		
    		?>
                <div class="row">
    
    		<div id="brajesh-search-company" class="col-sm-12 col-md-12 col-lg-12">
    			<div id="members-dir-search-company" class="dir-search" role="search">
    				<?php bp_directory_members_search_form(brajesh_custom_ids( 'company_name_field' 
                     )); ?>
    			</div><!-- #members-dir-search -->
    		</div>
    			
    	  </div>
    
    <?php
    function brajesh_custom_ids( $field_name, $field_value = '' ) {
      
      if ( empty( $field_name ) )
        return '';
      
      global $wpdb;
      
      $field_id = xprofile_get_field_id_from_name( $field_name ); 
     
      if ( !empty( $field_id ) ) 
        $query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id;
      else
       return '';
      
      if ( $field_value != '' ) 
        $query .= " AND value LIKE '%" . $field_value . "%'";
          
        // $query .= " AND value = '" . $field_value . "'"; 
          
    	$custom_ids = $wpdb->get_col( $query );
      
      if ( !empty( $custom_ids ) ) {
        // convert the array to a csv string
        $custom_ids_str = 'include=' . implode(",", $custom_ids);
        return $custom_ids_str;
      }
      else
       return '';  
    }
    ?>
    

    Thank you again

  • Participant
    Level: Initiated
    Posts: 5
    cobe on #31079

    Hi again, i added form for search buddypress to make filtring by city and many more… here is my code:

    
    if ( is_user_logged_in() ) { 
    
    do_action( 'bp_before_members_loop' ); ?>
    
    <div class="container">
    	<form action="" method="get" id="search-members-form">
    	<div class="row">
    		
    		<div class="form-group col-sm-12 col-md-4 col-lg-4">
    			<?php
    			$field_id = 94;
    			$field    = new  BP_XProfile_Field( $field_id );
    			$options  = $field->get_children();
    			echo "<option value=''>Selectioner un Domaine d'activité</option>";
    			foreach ( $options as $option ) {
    				echo '<option value="' . esc_attr( $option->name ) . '">' . esc_html( $option->name ) . '</option>';
    			}
    
    			echo '</select>'; ?>
    		</div>
    		
    		<div class="form-group col-sm-12 col-md-4 col-lg-4">
    			<?php
    			//Select City
    			$field_id = 103;
    			$field    = new  BP_XProfile_Field( $field_id );
    			$options  = $field->get_children();
    			echo "<select name='kechweb-city'  class='mdb-select md-form' searchable='Search here..'>";
    			
    			foreach ( $options as $option ) {
    				echo '<option value="' . esc_attr( $option->name ) . '">' . esc_html( $option->name ) . '</option>';
    			}
    
    			echo '</select>'; 
    			
    			?>
    		</div>
    		
    		<div class="form-group col-sm-12 col-md-4 col-lg-4">
    		<input type="text" name="members_search" id="members_search" placeholder="search company name" />
    		<input type="submit" id="members_search_submit" name="members_search_submit" value="Search">
    		</div>
    		
    	</div>
    	</form>
    </div>
    <?php } ?>
    

    I got the slug page with form methode GET like that :
    ?kechweb-domaine=Construction&kechweb-city=NewYork&members_search=&members_search_submit=Search
    Search doesn’t work maybe I need to add

    if(!empty($_POST['kechweb-domaine']) && !empty($_POST['kechweb-city'])){
    
    }
  • Keymaster
    (BuddyDev Team)
    Posts: 22201
    Brajesh Singh on #31126

    Hi,
    I am sorry, It falls outside our support as it will need more than 15 minutes. I will point you in the right direction.

    You will need to do use xprofile_query to search for the field values.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 5
    cobe on #31128

    Thank you!

The topic ‘ [Resolved] How to get all options from select field xprofile ?’ is closed to new replies.

This topic is: resolved