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

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

PHP foreach loop & html table - displaying duplicates

问题

以下是您要翻译的内容:

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

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

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

  1. <table style=\"width: 800px;\">
  2. <thead>
  3. <tr>
  4. <th>课程</th>
  5. <th>课程名称</th>
  6. <th>课程学分</th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. <?php
  11. $rawcourses = get_field('course_selector');
  12. $progcode_check_checked_values = get_field('prog_discipline_code');
  13. $i = 0;
  14. foreach($rawcourses as $rawcourse) :
  15. if( have_rows('course_list', $rawcourse) ): ?>
  16. <?php // 获取所有课程
  17. while( have_rows('course_list', $rawcourse) ) : the_row();
  18. // 查找选定的代码
  19. // $code = get_sub_field('discipline_code');
  20. if ($progcode_check_checked_values) : ?>
  21. <?php foreach ($progcode_check_checked_values as $progcode_check_value) : ?>
  22. <tr>
  23. <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) ?>
  24. <?php the_sub_field('course_number'); ?></a></td>
  25. <td><?php echo get_the_title($rawcourse); ?></td>
  26. <td><?php the_sub_field('credits'); ?></td>
  27. <div class=\"modal fade\" id=\"modal<?php get_sub_field('course_number'); ?><?php echo $i;?>\" tabindex=\"-1\" aria-labelledby=\"exampleModalLabel\" aria-hidden=\"true\">
  28. <div class=\"modal-dialog\">
  29. <div class=\"modal-content\">
  30. <div class=\"modal-header\">
  31. <h5 class=\"modal-title\"><?php echo get_the_title($rawcourse); ?></h5>
  32. <button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"modal\" aria-label=\"Close\"></button>
  33. </div>
  34. <div class=\"modal-body\">
  35. <?php $content_post = get_post($rawcourse);
  36. $content = $content_post->post_content;
  37. $content = apply_filters('the_content', $content);
  38. $content = str_replace(']]>', ']]&gt;', $content);
  39. echo $content; ?>
  40. </div>
  41. <div class=\"modal-footer\">
  42. <button type=\"button\" class=\"btn btn-secondary\" data-bs-dismiss=\"modal\">关闭</button>
  43. </div>
  44. </div>
  45. </div>
  46. </div>
  47. </tr>
  48. <?php endforeach; ?>
  49. <?php endif;
  50. // 结束循环
  51. endwhile; ?>
  52. <?php $i++; // 无值。
  53. else :
  54. // 做一些其他事情...
  55. endif;
  56. endforeach; ?>
  57. </tbody>
  58. </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表格 – 显示重复项

  1. &lt;table style=&quot;width: 800px;&quot;&gt;
  2. &lt;thead&gt;
  3. &lt;tr&gt;
  4. &lt;th&gt;Course&lt;/th&gt;
  5. &lt;th&gt;Course Name&lt;/th&gt;
  6. &lt;th&gt;Course Credits&lt;/th&gt;
  7. &lt;/tr&gt;
  8. &lt;/thead&gt;
  9. &lt;tbody&gt;
  10. &lt;?php
  11. $rawcourses = get_field(&#39;course_selector&#39;);
  12. $progcode_check_checked_values = get_field(&#39;prog_discipline_code&#39;);
  13. $i = 0;
  14. foreach($rawcourses as $rawcourse) :
  15. if( have_rows(&#39;course_list&#39;, $rawcourse) ): ?&gt;
  16. &lt;?php // Grab all courses
  17. while( have_rows(&#39;course_list&#39;, $rawcourse) ) : the_row();
  18. // Look for selected code
  19. // $code = get_sub_field(&#39;discipline_code&#39;);
  20. if ($progcode_check_checked_values) : ?&gt;
  21. &lt;?php foreach ($progcode_check_checked_values as $progcode_check_value) : ?&gt;
  22. &lt;tr&gt;
  23. &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;
  24. &lt;?php the_sub_field(&#39;course_number&#39;); ?&gt;&lt;/a&gt; &lt;/td&gt;
  25. &lt;td&gt;&lt;?php echo get_the_title($rawcourse); ?&gt; &lt;/td&gt;
  26. &lt;td&gt;&lt;?php the_sub_field(&#39;credits&#39;); ?&gt;&lt;/td&gt;
  27. &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;
  28. &lt;div class=&quot;modal-dialog&quot;&gt;
  29. &lt;div class=&quot;modal-content&quot;&gt;
  30. &lt;div class=&quot;modal-header&quot;&gt;
  31. &lt;h5 class=&quot;modal-title&quot; id=&quot;exampleModalLabel&quot;&gt;&lt;?php echo get_the_title($rawcourse); ?&gt;&lt;/h5&gt;
  32. &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;
  33. &lt;/div&gt;
  34. &lt;div class=&quot;modal-body&quot;&gt;
  35. &lt;?php $content_post = get_post($rawcourse);
  36. $content = $content_post-&gt;post_content;
  37. $content = apply_filters(&#39;the_content&#39;, $content);
  38. $content = str_replace(&#39;]]&gt;&#39;, &#39;]]&amp;gt;&#39;, $content);
  39. echo $content; ?&gt;
  40. &lt;/div&gt;
  41. &lt;div class=&quot;modal-footer&quot;&gt;
  42. &lt;button type=&quot;button&quot; class=&quot;btn btn-secondary&quot; data-bs-dismiss=&quot;modal&quot;&gt;Close&lt;/button&gt;
  43. &lt;/div&gt;
  44. &lt;/div&gt;
  45. &lt;/div&gt;
  46. &lt;/div&gt;
  47. &lt;/tr&gt;
  48. &lt;?php endforeach; ?&gt;
  49. &lt;?php endif;
  50. // End loop.
  51. endwhile; ?&gt;
  52. &lt;?php $i++; // No value.
  53. else :
  54. // Do something...
  55. endif;
  56. endforeach; ?&gt;
  57. &lt;/tbody&gt;
  58. &lt;/table&gt;type here

答案1

得分: 0

我能够弄清楚。我在foreach($progcode_check_checked_values as $progcode_check_value)之后添加了一个if语句。我也能够让被注释的$code = get_sub_field('discipline_code')正常工作。下面是更新后的代码。

  1. <table style="width: 800px;">
  2. <thead>
  3. <tr>
  4. <th>Course</th>
  5. <th>Course Name</th>
  6. <th>Course Credits</th>
  7. </tr>
  8. </thead>
  9. <tbody>
  10. <?php
  11. $rawcourses = get_field('course_selector');
  12. $progcode_check_checked_values = get_field('prog_discipline_code');
  13. $i = 0;
  14. foreach($rawcourses as $rawcourse) :
  15. if( have_rows('course_list', $rawcourse) ): ?>
  16. <?php
  17. while( have_rows('course_list', $rawcourse) ) : the_row();
  18. $code = get_sub_field('discipline_code');
  19. if ($progcode_check_checked_values) : ?>
  20. <?php foreach ($progcode_check_checked_values as $progcode_check_value) : ?>
  21. <?php if ($progcode_check_value == $code ) : ?>
  22. <tr>
  23. <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>
  24. <td><?php echo get_the_title($rawcourse); ?></td>
  25. <td><?php the_sub_field('credits'); ?></td>
  26. <div class="modal fade" id="modal<?php get_sub_field('course_number');?><?php echo $i;?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  27. <div class="modal-dialog">
  28. <div class="modal-content">
  29. <div class="modal-header">
  30. <h5 class="modal-title"><?php echo get_the_title($rawcourse); ?></h5>
  31. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
  32. </div>
  33. <div class="modal-body">
  34. <?php $content_post = get_post($rawcourse);
  35. $content = $content_post->post_content;
  36. $content = apply_filters('the_content', $content);
  37. $content = str_replace(']]>', ']]&gt;', $content);
  38. echo $content; ?>
  39. </div>
  40. <div class="modal-footer">
  41. <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. </tr>
  47. <?php endif; ?>
  48. <?php endforeach; ?>
  49. <?php endif;
  50. endwhile; ?>
  51. <?php $i++;
  52. endif;
  53. endforeach; ?>
  54. </tbody>
  55. </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.

  1. &lt;table style=&quot;width: 800px;&quot;&gt;
  2. &lt;thead&gt;
  3. &lt;tr&gt;
  4. &lt;th&gt;Course&lt;/th&gt;
  5. &lt;th&gt;Course Name&lt;/th&gt;
  6. &lt;th&gt;Course Credits&lt;/th&gt;
  7. &lt;/tr&gt;
  8. &lt;/thead&gt;
  9. &lt;tbody&gt;
  10. &lt;?php
  11. $rawcourses = get_field(&#39;course_selector&#39;);
  12. $progcode_check_checked_values = get_field(&#39;prog_discipline_code&#39;);
  13. $i = 0;
  14. foreach($rawcourses as $rawcourse) :
  15. if( have_rows(&#39;course_list&#39;, $rawcourse) ): ?&gt;
  16. &lt;?php
  17. while( have_rows(&#39;course_list&#39;, $rawcourse) ) : the_row();
  18. $code = get_sub_field(&#39;discipline_code&#39;);
  19. if ($progcode_check_checked_values) : ?&gt;
  20. &lt;?php foreach ($progcode_check_checked_values as $progcode_check_value) : ?&gt;
  21. &lt;?php if ($progcode_check_value == $code ) : ?&gt;
  22. &lt;tr&gt;
  23. &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;
  24. &lt;td&gt;&lt;?php echo get_the_title($rawcourse); ?&gt; &lt;/td&gt;
  25. &lt;td&gt;&lt;?php the_sub_field(&#39;credits&#39;); ?&gt;&lt;/td&gt;
  26. &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;
  27. &lt;div class=&quot;modal-dialog&quot;&gt;
  28. &lt;div class=&quot;modal-content&quot;&gt;
  29. &lt;div class=&quot;modal-header&quot;&gt;
  30. &lt;h5 class=&quot;modal-title&quot; id=&quot;exampleModalLabel&quot;&gt;&lt;?php echo get_the_title($rawcourse); ?&gt;&lt;/h5&gt;
  31. &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;
  32. &lt;/div&gt;
  33. &lt;div class=&quot;modal-body&quot;&gt;
  34. &lt;?php $content_post = get_post($rawcourse);
  35. $content = $content_post-&gt;post_content;
  36. $content = apply_filters(&#39;the_content&#39;, $content);
  37. $content = str_replace(&#39;]]&gt;&#39;, &#39;]]&amp;gt;&#39;, $content);
  38. echo $content; ?&gt;
  39. &lt;/div&gt;
  40. &lt;div class=&quot;modal-footer&quot;&gt;
  41. &lt;button type=&quot;button&quot; class=&quot;btn btn-secondary&quot; data-bs-dismiss=&quot;modal&quot;&gt;Close&lt;/button&gt;
  42. &lt;/div&gt;
  43. &lt;/div&gt;
  44. &lt;/div&gt;
  45. &lt;/div&gt;
  46. &lt;/tr&gt;
  47. &lt;?php endif; ?&gt;
  48. &lt;?php endforeach; ?&gt;
  49. &lt;?php endif;
  50. endwhile; ?&gt;
  51. &lt;?php $i++;
  52. endif;
  53. endforeach; ?&gt;
  54. &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:

确定