Home › Forums › Front-end Issues › A table for a championship
Hello!
I’m trying to get this table right. I have managed to display a single list of drivers (since every race list them all again) but now I have to complete my table with each race results and each result regarding the pilot in that line.
<?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;
?>
Ideas?
This is the address where it can now be seen, by the way.
http://apexgt.disaine.com.br/campeonato/liga-apexgt-2019/
I have cleaned the code a bit but I am still stuck. I get back to the repeater loop but what I fail to do is matching the subfields in my ‘race_pilot’ repeater with the driver on that specific row.
<?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'); ?>
<?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 = '';
//$final = $finals;
$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>';
while (have_rows('race_grid')) : the_row();
while (have_rows('race_pilot')) : the_row();
if (in_array($driver, $pilots)) {
the_sub_field('final_position');
}
$final = get_sub_field('final_position');
endwhile;
endwhile;
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;
?>
The topic ‘A table for a championship’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.