Search code examples
phpadvanced-custom-fieldswordpress-block-theme

PHP foreach loop & html table - displaying duplicates


I am utilizing WordPress, ACF, and PHP; I've created a custom block with the code below. Please let me know if more information is needed. I am still fairly new to this and not sure what all to include.

The following code is working as intended with the exception of duplicating results. I did attempt to following through this thread with no luck. The picture included show what it's currently displaying. It should list APCS 207 Command Line Interface... APCS 242 Computer & Network Management... BUSI 270 Accounting Software Applications... BUSI 353 Advanced Accounting I...

list

<table style="width: 800px;">
<thead>
            <tr>
                <th>Course</th>
                <th>Course Name</th>
                <th>Course Credits</th>
            </tr>
</thead>

<tbody>
<?php 
$rawcourses = get_field('course_selector'); 
$progcode_check_checked_values = get_field('prog_discipline_code'); 


$i = 0;


foreach($rawcourses as $rawcourse) : 

    if( have_rows('course_list', $rawcourse) ): ?>
        
            

        <?php // Grab all courses

        while( have_rows('course_list', $rawcourse) ) : the_row(); 

            // Look for selected code
            // $code = get_sub_field('discipline_code');
            if ($progcode_check_checked_values) : ?>

                <?php foreach ($progcode_check_checked_values as $progcode_check_value) : ?>
<tr>
                        <td><a href="#" data-bs-toggle="modal" data-bs-target="#modal<?php get_sub_field('course_number');?><?php echo $i;?>"><?php echo strtoupper($progcode_check_value) ?>
                        <?php the_sub_field('course_number'); ?></a>  </td>
                        <td><?php echo get_the_title($rawcourse); ?> </td>
                        <td><?php the_sub_field('credits'); ?></td>
                        
                        <div class="modal fade" id="modal<?php get_sub_field('course_number');?><?php echo $i;?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                          <div class="modal-dialog">
                            <div class="modal-content">
                              <div class="modal-header">
                                <h5 class="modal-title" id="exampleModalLabel"><?php echo get_the_title($rawcourse); ?></h5>
                                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                              </div>
                              <div class="modal-body">
                                <?php $content_post = get_post($rawcourse);
                                $content = $content_post->post_content;
                                $content = apply_filters('the_content', $content);
                                $content = str_replace(']]>', ']]&gt;', $content);
                                echo $content; ?>
                              </div>
                              <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                              </div>
                            </div>
                          </div>
                        </div>
</tr>
                <?php endforeach; ?>

            <?php endif; 

        // End loop.
        endwhile; ?>

            
        

    <?php $i++; // No value.
    else :
        // Do something...
    endif;
    
endforeach; ?>
</tbody>
</table>type here

Solution

  • I was able to figure out. I added an if statement after the foreach($progcode_check_checked_values as $progcode_check_value). I was able to get the commented out $code = get_sub_field('discipline_code') to work as well. Updated code below.

    <table style="width: 800px;">
    <thead>
        <tr>
            <th>Course</th>
            <th>Course Name</th>
            <th>Course Credits</th>
        </tr>
    </thead>
    
    <tbody>
        <?php 
        $rawcourses = get_field('course_selector'); 
        $progcode_check_checked_values = get_field('prog_discipline_code'); 
        $i = 0;
        
        foreach($rawcourses as $rawcourse) : 
            if( have_rows('course_list', $rawcourse) ): ?>
        
            <?php
            while( have_rows('course_list', $rawcourse) ) : the_row(); 
                $code = get_sub_field('discipline_code');
            
                if ($progcode_check_checked_values) : ?>
                
                <?php foreach ($progcode_check_checked_values as $progcode_check_value) : ?>
                    <?php if ($progcode_check_value == $code ) : ?>
                        <tr>
                            <td><a href="#" data-bs-toggle="modal" data-bs-target="#modal<?php get_sub_field('course_number');?><?php echo $i;?>"><?php echo strtoupper($progcode_check_value) ?><?php the_sub_field('course_number'); ?></a>  </td>
                            <td><?php echo get_the_title($rawcourse); ?> </td>
                            <td><?php the_sub_field('credits'); ?></td>
                            
                            <div class="modal fade" id="modal<?php get_sub_field('course_number');?><?php echo $i;?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title" id="exampleModalLabel"><?php echo get_the_title($rawcourse); ?></h5>
                                            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                        </div>
                                        
                                        <div class="modal-body">
                                            <?php $content_post = get_post($rawcourse);
                                            $content = $content_post->post_content;
                                            $content = apply_filters('the_content', $content);
                                            $content = str_replace(']]>', ']]&gt;', $content);
                                            echo $content; ?>
                                        </div>
                                        
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </tr>
                    <?php endif; ?>
                <?php endforeach; ?>
                <?php endif; 
            endwhile; ?>
            <?php $i++; 
            endif;
        endforeach; ?>
    </tbody>