PHP foreach循环和HTML表格 – 显示重复项

huangapple go评论74阅读模式
英文:

PHP foreach loop & html table - displaying duplicates

问题

以下是您要翻译的内容:

"我正在使用WordPress、ACF和PHP;我已经创建了一个自定义块,代码如下。请让我知道是否需要更多信息。我对此还相当新手,不确定需要包括哪些内容。

以下代码按预期工作,但存在重复结果的问题。我尝试按照此线程的建议,但没有成功。附带的图片显示了当前的显示情况。应该列出以下内容:
APCS 207 命令行界面...
APCS 242 计算机与网络管理...
BUSI 270 会计软件应用...
BUSI 353 高级会计学 I...

PHP foreach循环和HTML表格 – 显示重复项

<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(']]>', ']]&gt;', $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...

PHP foreach循环和HTML表格 – 显示重复项

&lt;table style=&quot;width: 800px;&quot;&gt;
&lt;thead&gt;
			&lt;tr&gt;
				&lt;th&gt;Course&lt;/th&gt;
				&lt;th&gt;Course Name&lt;/th&gt;
				&lt;th&gt;Course Credits&lt;/th&gt;
			&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;?php 
$rawcourses = get_field(&#39;course_selector&#39;); 
$progcode_check_checked_values = get_field(&#39;prog_discipline_code&#39;); 


$i = 0;


foreach($rawcourses as $rawcourse) : 

	if( have_rows(&#39;course_list&#39;, $rawcourse) ): ?&gt;
		
			

		&lt;?php // Grab all courses

		while( have_rows(&#39;course_list&#39;, $rawcourse) ) : the_row(); 

			// Look for selected code
			// $code = get_sub_field(&#39;discipline_code&#39;);
			if ($progcode_check_checked_values) : ?&gt;

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

			&lt;?php endif; 

		// End loop.
		endwhile; ?&gt;

			
		

	&lt;?php $i++; // No value.
	else :
		// Do something...
	endif;
    
endforeach; ?&gt;
&lt;/tbody&gt;
&lt;/table&gt;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(']]>', ']]&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>
英文:

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.

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

</table>

huangapple
  • 本文由 发表于 2023年6月22日 05:43:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定