英文:
PHP foreach loop & html table - displaying duplicates
问题
以下是您要翻译的内容:
"我正在使用WordPress、ACF和PHP;我已经创建了一个自定义块,代码如下。请让我知道是否需要更多信息。我对此还相当新手,不确定需要包括哪些内容。
以下代码按预期工作,但存在重复结果的问题。我尝试按照此线程的建议,但没有成功。附带的图片显示了当前的显示情况。应该列出以下内容:
APCS 207 命令行界面...
APCS 242 计算机与网络管理...
BUSI 270 会计软件应用...
BUSI 353 高级会计学 I...
<table style=\"width: 800px;\">
<thead>
<tr>
<th>课程</th>
<th>课程名称</th>
<th>课程学分</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) : ?>
<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\"><?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(']]>', ']]>', $content);
echo $content; ?>
</div>
<div class=\"modal-footer\">
<button type=\"button\" class=\"btn btn-secondary\" data-bs-dismiss=\"modal\">关闭</button>
</div>
</div>
</div>
</div>
</tr>
<?php endforeach; ?>
<?php endif;
// 结束循环
endwhile; ?>
<?php $i++; // 无值。
else :
// 做一些其他事情...
endif;
endforeach; ?>
</tbody>
</table>
英文:
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...
<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
答案1
得分: 0
我能够弄清楚。我在foreach($progcode_check_checked_values as $progcode_check_value)
之后添加了一个if语句。我也能够让被注释的$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"><?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(']]>', ']]>', $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>
</table>
英文:
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>
</table>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论