在线课程组长进度追踪表:无数据时空白单元格生成问题修复请求
问题描述
我们为客户的在线课程实现了组长进度追踪功能,通过表格展示学员的课程进度数据,数据来源于数据库。当前遇到的问题是:当对应的课程数据不存在时,现有逻辑无法生成所需的空白单元格,进而导致表格格式错乱。尝试多种解决方案均未成功,请求技术协助。
现有代码
<?php /* Template Name: User Course Record Leadership */ get_header(); $user_id=get_current_user_id(); $user=get_userdata($user_id); $useremail = $user->user_email; $userrole=$user->roles; $usersemail=array(); global $wpdb; ?> <div id="et-main-area"> <div id="main-content"> <article id="post-262" class="post-262 page type-page status-publish hentry"> <div class="entry-content"> <?php while(have_posts()): the_post(); the_content(); endwhile; ?> <div class="et_pb_section et_pb_section_0 et_section_regular"> <div class=" et_pb_row et_pb_row_0"> <div class="et_pb_column et_pb_column_4_4 et_pb_column_0 et_pb_css_mix_blend_mode_passthrough et-last-child"> <div class="et_pb_text et_pb_module et_pb_bg_layout_light et_pb_text_align_left et_pb_text_0"> <div class="et_pb_text_inner"> <?php if(is_user_logged_in()){ if($userrole[0]=='group_leader'){ $sql_str = $wpdb->prepare("SELECT usermeta.meta_value as group_ids FROM wpb0_usermeta as usermeta INNER JOIN wpb0_posts as posts ON posts.ID=usermeta.meta_value WHERE user_id = %d AND meta_key LIKE %s AND (posts.post_status = 'publish' OR posts.post_status = 'draft')", $user_id, 'learndash_group_leaders_%'); $group_ids = $wpdb->get_col($sql_str); //print_r($group_ids); die; foreach($group_ids as $ids){ $key='learndash_group_users_'.intval($ids); $getall_users = $wpdb->get_results("select wpb0_users.ID,wpb0_users.user_email from wpb0_users inner join wpb0_usermeta on wpb0_users.ID =wpb0_usermeta.user_id where wpb0_usermeta.meta_key='$key'"); foreach($getall_users as $users){ // echo $users->user_email.$ids; $usersemail['users'][] =array('user_id' =>$users->ID,'group_id'=>$ids,'user_email' => $users->user_email); } } $lession_complete= false; echo "<table id='users_progress_leadership' class='display'> <thead> <tr> <tr> <th> </th> <th> </th> <th> </th> <th> </th> <th> </th> <th> Modules </th> </tr> <th>Employee Name</th> <th>Color Self-Assessment</th> <th>1</th> <th>2</th> <th>3</th> <th>4</th> <th>5</th> <th>6</th> <th>7</th> <th>8</th> <th>Course Completed</th> <th>Results</th> </tr> </thead><tbody>"; foreach($usersemail['users'] as $userdata){ $dtd= get_user_meta($userdata['user_id'] ,'_sfwd-course_progress',true); ?> <tr><td><?php echo get_user_meta($userdata['user_id'],'first_name',true).' '.get_user_meta($userdata['user_id'],'last_name',true); ?></td> <?php $needle_keys=['21032', '20999','21001', '21004', '21007', '21009', '21013', '21015', '21018']; foreach($dtd as $dt){ $lessons_keys = array_keys($dt['lessons']); foreach($lessons_keys as $key){ if(in_array($key, $needle_keys)){ if($dt['lessons'][$key]){ $lession_complete= true; echo "<td>✓ </td>"; } else { echo "<td> </td>"; } } } } ?> <td> <?php if($dt['completed']>='9'){ echo "Complete "; } ?></td> <td class="user-results"> <a href="https://hrhazmat.com/user-progress-report-single-page?group_id=<?php echo $ids; ?>&user_id=<?php echo $userdata['user_id']; ?>"> View Results</a></td> <?php echo "</tr>"; } echo " </tbody></table >"; echo "<style> table.display th{ text-align:left; } .dataTables_wrapper .dataTables_paginate .paginate_button.current, .dataTables_wrapper .dataTables_paginate .paginate_button.current:hover { color: #333 !important; /* border: 1px solid #979797; */ background-color: #005574 !important; background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #fff), color-stop(100%, #dcdcdc)); background: -webkit-linear-gradient(top, #fff 0%, #dcdcdc 100%); background: -moz-linear-gradient(top, #fff 0%, #dcdcdc 100%); background: -ms-linear-gradient(top, #fff 0%, #dcdcdc 100%); background: -o-linear-gradient(top, #fff 0%, #dcdcdc 100%); background: linear-gradient(to bottom, #005574 50%, #d0b232 100%) !important; color: white !important; margin-bottom:12px !important; } </style>"; } } /* .w3-table,.w3-table-all{border-collapse:collapse;border-spacing:0;width:100%;display:table}.w3-table-all{border:1px solid #ccc} .w3-bordered tr,.w3-table-all tr{border-bottom:1px solid #ddd}.w3-striped tbody tr:nth-child(even){background-color:#f1f1f1} .w3-table-all tr:nth-child(odd){background-color:#fff}.w3-table-all tr:nth-child(even){background-color:#f1f1f1} .w3-hoverable tbody tr:hover,.w3-ul.w3-hoverable li:hover{background-color:#ccc}.w3-centered tr th,.w3-centered tr td{text-align:center} .w3-table td,.w3-table th,.w3-table-all td,.w3-table-all th{padding:8px 8px;display:table-cell;text-align:left;vertical-align:top;border:1px solid #0000002e; } .w3-table th:first-child,.w3-table td:first-child,.w3-table-all th:first-child,.w3-table-all td:first-child{padding-left:16px}*/ ?> </div></div></div></div></div> </div> </article> </div> </div> <?php get_footer(); ?>
解决方案
问题根源是现有逻辑仅遍历用户已有的课程数据,若某个模块数据不存在,就不会生成对应的<td>单元格,导致表格列数与表头不一致,格式错乱。
修改思路:直接遍历需要展示的模块ID列表($needle_keys),对每个模块ID,检查用户进度中是否存在该模块,再根据状态输出对应单元格;若不存在,则输出空白单元格,确保每行列数统一。
修改后的核心代码片段
<?php $needle_keys=['21032', '20999','21001', '21004', '21007', '21009', '21013', '21015', '21018']; // 初始化用户模块状态,默认所有模块未完成/不存在 $user_lesson_status = array_fill_keys($needle_keys, false); // 遍历用户现有进度,更新对应模块的完成状态 if (!empty($dtd)) { foreach($dtd as $dt) { if (!empty($dt['lessons'])) { foreach($dt['lessons'] as $key => $status) { if (in_array($key, $needle_keys)) { $user_lesson_status[$key] = $status; } } } } } // 遍历所有需要展示的模块,逐个输出单元格 foreach($needle_keys as $key) { if ($user_lesson_status[$key]) { echo "<td>✓ </td>"; } else { echo "<td> </td>"; } } ?>
补充:处理课程完成状态的容错
避免用户无进度数据时$dt['completed']报错,修改完成状态判断逻辑:
<td> <?php $completed = 0; if (!empty($dtd)) { foreach($dtd as $dt) { if (!empty($dt['completed'])) { $completed = max($completed, $dt['completed']); } } } if($completed >= '9'){ echo "Complete "; } ?></td>
修改后,无论用户是否有对应模块的进度数据,都会为每个模块生成一个<td>单元格,保证表格列数与表头一致,彻底解决格式错乱问题。
内容的提问来源于stack exchange,提问作者maynar79
相关产品推荐
相关产品推荐

