PHP foreach循环& html表-显示重复项

r7knjye2  于 12个月前  发布在  PHP
关注(0)|答案(1)|浏览(108)

我使用WordPress,ACF和PHP;我用下面的代码创建了一个自定义块。请让我知道如果需要更多的信息。我对此还相当陌生,不确定要包括什么。
下面的代码按预期工作,除了重复结果。我试图通过this thread没有运气。包含的图片显示了它当前显示的内容。它应该列出APCS 207命令行界面... APCS 242计算机与网络管理BUSI 270会计软件应用程序BUSI 353 Advanced Accounting I.

<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
kx7yvsdv

kx7yvsdv1#

我能弄明白。我在foreach后面添加了一个if语句($progcode_check_checked_values作为$progcode_check_value)。我也可以让注解掉的$code = get_sub_field('discipline_code')正常工作。更新了下面的代码。

<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>

相关问题