Support

Account

Home Forums Add-ons Repeater Field Repeater field to create table

Solving

Repeater field to create table

  • Hi,

    I have a couple of repeater fields set up (and working well).

    I am using the following code to create an HTML table from the data:

    <?php
    
    /**
     * Taxonomy Template - Used to display each rate of pay within the agreement
     *
     * @package WordPress
     * @subpackage Twenty_Fourteen
     * @since Twenty Fourteen 1.0
     */
    
    get_header();
    
    $term_id = get_queried_object()->term_id;
    
    $paydeal_dates = get_terms(
    	array(
    		'taxonomy'   => 'paydeals',
    		'hide_empty' => false,
    		'orderby'    => 'id',
    	)
    );
    
    print_r($paydeal_dates);
    
    ?>
    
    <section class="main">
    	<article>
    		<h1 class="display-4"><?php single_term_title(); ?></h1>
    			<table class="table table-bordered">
    				<thead>
    					<tr>
    						<th scope="col"></th>
    						<?php
    						// check if the repeater field has rows of data
    						foreach ( $paydeal_dates as $paydeals ) {
    							if ( have_rows( 'pay_deal_dates', 'paydeals_' . $paydeals->term_id ) ) :
    
    								// loop through the rows of data to get the dates
    								while ( have_rows( 'pay_deal_dates', 'paydeals_' . $paydeals->term_id ) ) : the_row();
    
    									// Vars
    									$today      = date( 'j M Y' );
    									$start_date = get_sub_field( 'start_date' );
    									$end_date   = get_sub_field( 'end_date' );
    
    									// display sub field values with current year highlighted
    									if ( $start_date <= $today && $today <= $end_date ) :
    										echo '<th scope="col" class="current_rates">' . $start_date . ' - ' . $end_date . '</th>';
    									else :
    										echo '<th scope="col">' . $start_date . ' - ' . $end_date . '</th>';
    									endif;
    
    								endwhile; //End of dates row
    
    							endif;
    						};
    						?>
    					</tr>
    				</thead>
    				<tbody>
    					<?php
    					foreach ( $paydeal_dates as $paydeals ) {
    						if ( have_rows( 'pay_deal_dates', 'paydeals_' . $paydeals->term_id ) ) :
    
    							//loop through to get rows
    							while ( have_rows( 'pay_deal_dates', 'paydeals_' . $paydeals->term_id ) ) : the_row();
    
    							echo '<tr>';
    
    							if ( have_rows( 'job_titles', 'term_' . $term_id ) ) :
    
    								//loop through the rows of data to get each job title
    								while ( have_rows( 'job_titles', 'term_' . $term_id ) ) : the_row();
    
    									//Vars
    									$job_title = get_sub_field( 'job_title' );
    									echo '<th scope="row">' . $job_title . '</th>';
    
    									if ( have_rows( 'payrate', 'term_' . $term_id ) ) :
    
    										//loop through the rows of data to get each payrate
    										while ( have_rows( 'payrate', 'term_' . $term_id ) ) : the_row();
    
    											//Vars
    											$payrate = get_sub_field( 'rate' );
    											echo '<td scope="col">';
    											echo $payrate;
    											echo '</td>';
    
    										endwhile; //end payrate loop
    
    									endif;
    
    									echo '</tr>';
    
    								endwhile; //end job title loop
    
    							endif;
    
    						endwhile; //end paydeal dates loop
    
    					endif;
    
    				}; ?>
    				</tbody>
    			</table>
    	</article>
    </section>
    
    <aside class="sidebar">
    	<?php dynamic_sidebar( 'sidebar' ); ?>
    </aside>
    
    <aside class="adverts">
    	<?php dynamic_sidebar( 'adverts' ); ?>
    </aside>
    
    <?php get_footer(); ?>
    

    The problem is that this generates a table like the screenshot. I understand why the code does this but i need it to just output the job title row once for each set of dates.

    Screenshot

    What I am trying to do is style the cells in the column under the green header in the same way which is based on the date.

    I can remove the second foreach loop to get the layout I want but I am then not able to do the date check – as far as I can work out.

    Any help appreciated.

    Chris

  • I have the same issue (sort of). I managed to build a list without duplicated items http://apexgt.disaine.com.br/campeonato/liga-apexgt-2019/ but I can’t get the data I need of each pilot from each race.

    This is my PHP

    <?php

    function unique_multidim_array($array, $key) {
    $temp_array = array();
    $i = 0;
    $key_array = array();

    foreach($array as $val) {
    if (!in_array($val[$key], $key_array)) {
    $key_array[$i] = $val[$key];
    $temp_array[$i] = $val;
    }
    $i++;
    }
    return $temp_array;
    }

    $finals = array();
    $car = ”;
    $race_name = ”;
    $divisions = ”;
    $races = ”;
    $pilots = array();

    if( have_rows(‘champ_division’) ):
    while( have_rows(‘champ_division’) ): the_row();

    // vars
    $divisions = get_sub_field(‘division_name’);
    $races = get_sub_field(‘division_races’);
    $pilots = array();
    $finals = array();

    echo ‘<h4 class=”text-uppercase”>’.$divisions.'</h4>’; ?>
    <table class=”table champ_table” id=””>

    <?php
    foreach( $races as $post): // variable must be called $post (IMPORTANT)
    setup_postdata($post);
    while ( have_rows (‘race_grid’)) : the_row();
    while (have_rows(‘race_pilot’)) : the_row();
    $pilots[] = get_sub_field(‘pilot_name’);
    $finals[] = get_sub_field(‘final_position’);
    $penalty = get_sub_field(‘pilot_penaly’);
    $bestlap = get_sub_field(‘pilot_bestlap’);
    $warn = get_sub_field(‘pilot_warning’); ?>
    <?php endwhile;
    endwhile;
    endforeach;
    wp_reset_postdata();
    $drivers = unique_multidim_array($pilots,’display_name’);
    $race_id = unique_multidim_array($races,’ID’);

    if (!empty($drivers)) { ?>
    <thead>
    <tr>
    <th>Nome do piloto</th>
    <th>Carro</th>
    <th>Número</th>
    <th>Equipe</th>
    <?php } foreach( $races as $post): // variable must be called $post (IMPORTANT)
    setup_postdata($post);
    echo ‘<th>’;
    the_title();
    echo ‘</th>’;
    endforeach;
    echo ‘</tr></thead>’;
    wp_reset_postdata(); ?>
    <?php
    foreach ($drivers as $driver) :
    $pid = ‘user_’.$driver[‘ID’];
    $car = ”;
    $pnumber = ”;
    $team = get_field(‘user_team’, $pid);
    while ( have_rows (‘user_car’, $pid) ) : the_row();
    $car = get_sub_field(‘car_model’);
    $pnumber = get_sub_field(‘car_number’);
    endwhile;
    ?>
    <tbody>
    <tr>
    <td><?php echo $driver[‘display_name’]; ?></td>
    <td><?php if(!empty($car)) { echo $car->post_title;} ?></td>
    <td><?php echo $pnumber; ?></td>
    <td><?php echo $team; ?></td>
    <?php foreach( $races as $post): // variable must be called $post (IMPORTANT)
    setup_postdata($post);
    echo ‘<td>’;
    if (in_array($driver, $pilots)){
    echo $driver[‘display_name’];
    echo $bestlap;
    $warn = get_sub_field(‘pilot_warning’);
    print_r ($penalty);
    }
    echo ‘</td>’;
    endforeach;
    echo ‘</tr></tbody>’;
    wp_reset_postdata(); ?>
    <?php endforeach; //Piloto Linha ?>
    </tr>
    </tbody>

    </table>
    <?php // IMPORTANT – reset the $post object so the rest of the page works correctly
    endwhile;// IMPORTANT – reset the $post object so the rest of the page works correctly
    endif;
    ?>

  • I got the in_array thing but I’m stuck. It’s not clear to me how I’m gonna pick the driver and have other data (fields) relate to it from every single race in the championship.

  • @chrisdavies71 It appears that you have the same repeater field on multiple terms and they have the same values. What you need to do is check to see if you’ve already shown that value from a previous term and skip it. To do this you need to store the values as you show them and then check new values against the list.

    This is a simple example:

    
    // create a variable to hold values shown
    $already_shown = array();
    while (have_rows('pay_deal_dates', 'term_'.$paydeals->term_id )) {
      the_row();
      $payrate = get_sub_field('rate');
      if (in_array($payrate, $already_shown)) {
        // skip this one
        continue;
      }
      // code to display row here
    }
    


    @luizhscotta
    I can’t read you’re code the way is is formatted. You should also start a new topic and explain in more detail what you’re specific problem in, maybe someone will help you.

  • Sorry for the code.

    <?php 
        
        function unique_multidim_array($array, $key) { 
            $temp_array = array(); 
            $i = 0; 
            $key_array = array(); 
            
            foreach($array as $val) { 
                if (!in_array($val[$key], $key_array)) { 
                    $key_array[$i] = $val[$key]; 
                    $temp_array[$i] = $val; 
                } 
                $i++; 
            } 
            return $temp_array; 
        }
        
        $finals = array();
        $car = '';
        $race_name = '';
        $divisions = '';
        $races = '';
        $pilots = array();
        if( have_rows('champ_division') ): 
            while( have_rows('champ_division') ): the_row();
    		// vars
            $divisions = get_sub_field('division_name');
            $races = get_sub_field('division_races');
            $pilots = array();
            $finals = array();
            
            echo '<h4 class="text-uppercase">'.$divisions.'</h4>'; ?>
            <table class="table champ_table" id="">
            
                <?php 
                    foreach( $races as $post): // variable must be called $post (IMPORTANT) 
                    setup_postdata($post); 
                    while ( have_rows ('race_grid')) : the_row();
                        while (have_rows('race_pilot')) : the_row();
                            $pilots[] = get_sub_field('pilot_name');
                            $finals[] = get_sub_field('final_position');
                            $penalty = get_sub_field('pilot_penaly');
                            $bestlap = get_sub_field('pilot_bestlap');
                            $warn = get_sub_field('pilot_warning'); ?>
                        <?php endwhile;
                    endwhile;
                endforeach;
                wp_reset_postdata();
                $drivers = unique_multidim_array($pilots,'display_name'); 
                $race_id = unique_multidim_array($races,'ID');         
                
                if (!empty($drivers)) { ?>
                    <thead>
                        <tr>
                            <th>Nome do piloto</th>
                            <th>Carro</th>
                            <th>Número</th>
                            <th>Equipe</th>
                <?php } foreach( $races as $post): // variable must be called $post (IMPORTANT) 
                    setup_postdata($post);
                    echo '<th>';
                    the_title();
                    echo '</th>'; 
                endforeach;
                 echo '</tr></thead>';
                wp_reset_postdata(); ?>
                <?php 
                foreach ($drivers as $driver) : 
                    $pid = 'user_'.$driver['ID'];
                    $car = '';
                    $pnumber = '';
                    $team = get_field('user_team', $pid);
                    while ( have_rows ('user_car', $pid) ) : the_row();
                        $car = get_sub_field('car_model');
                        $pnumber = get_sub_field('car_number');                                                
                    endwhile; 
                    ?>
                    <tbody>
                    <tr>
                        <td><?php echo $driver['display_name']; ?></td>
                        <td><?php if(!empty($car)) { echo $car->post_title;}  ?></td>
                        <td><?php echo $pnumber; ?></td>
                        <td><?php echo $team; ?></td>
                        <?php foreach( $races as $post): // variable must be called $post (IMPORTANT) 
                            setup_postdata($post);
                            echo '<td>';
                                
                                if (in_array($driver, $pilots)){
                                    print_r($finals);
                                    //$warn = get_sub_field('pilot_warning');
                                    //print_r ($penalty);
                                }                             
                            echo '</td>';
                            //print_r ($group_pilots); 
                        endforeach;
                        echo '</tr></tbody>';
                        wp_reset_postdata(); ?>
                <?php endforeach; //Piloto Linha ?>
                </tr>
            </tbody>
            
            </table>
         <?php   // IMPORTANT - reset the $post object so the rest of the page works correctly 
            endwhile;// IMPORTANT - reset the $post object so the rest of the page works correctly
         endif;
    ?>

    In my example, in_array gets to build the list of drivers with no duplicates. But now I have to bring every single race result regarding that driver. I’m stuck but I’ll check your code.

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

The topic ‘Repeater field to create table’ is closed to new replies.