WordPress/PHP:修复宴请接待复选框的列值与数组匹配问题
问题描述
我有一张名为wp_invites的表,存储活动受邀人员信息,表中数据如下:
| id | handler | lead_name | party | reference | invited_to_reception | reception_list |
|---|---|---|---|---|---|---|
| 1 | Freddy | John | John, Ben, Steven | NgxaIyNybK4O | yes | |
| 2 | Jason | Peter | Peter, Jamie, Cathy | 6iqIWqdxewf0 | yes | Peter, Cathy |
| 3 | Jason | Mark | Mark | Mx7z9UnCPOQo | yes | |
| 4 | Jason | Kate | Kate, Louise, Lucy | gumWlKmfmyXg | no |
业务规则
- 仅部分群体可受邀参加接待会,由
invited_to_reception字段定义; - 若
invited_to_reception为yes且reception_list为空,则整个party群体都可参加;若reception_list有值,则仅该列表中的party成员可参加; reference为唯一标识,用于确定展示哪条邀请信息,例如rsvp.com?ref=NgxaIyNybK4O会展示John、Ben、Steven的邀请。
我需要实现:判断reception_list中的姓名是否在party中,若是则为该嘉宾显示接待会出席复选框。
当前实现代码
<?php // get reference from URL to determine which invites to show $reference = ( isset( $_GET['ref'] ) ) ? sanitize_text_field( $_GET['ref'] ) : ''; // check if valid invite (if ref exists in wp_invites table) $invites_table = $wpdb->prefix . 'invites'; $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $invites_table WHERE reference=%s", $reference), ARRAY_A ); if($results): foreach ($results as $result): $party = $result['party']; $party_guests = explode(",", $party); $invited_to_reception = $result['invited_to_reception']; $reception_list = $result['reception_list']; $reception_list = explode(",", $reception_list); // $reception_list = (array) $reception_list; // convert to assoc array foreach ($party_guests as $index=>$guest): $guest_name = trim($guest); if ($invited_to_reception == "Yes"): if( in_array($guest_name, $reception_list) ): ?> Show checkbox options for reception <?php endif; endif; endforeach endforeach; endif; ?>
存在问题
- id为1的行,
invited_to_reception为yes,应显示所有嘉宾复选框,但实际未展示,因in_array()判断无法覆盖全员受邀场景; - id为2的行,仅Peter和Cathy应显示复选框,但实际仅Cathy的复选框被展示。
尝试修改后的代码
<?php // get reference from URL to determine which invites to show $reference = ( isset( $_GET['ref'] ) ) ? sanitize_text_field( $_GET['ref'] ) : ''; // check if valid invite (if ref exists in wp_invites table) $invites_table = $wpdb->prefix . 'invites'; $results = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $invites_table WHERE reference=%s", $reference), ARRAY_A ); if($results): foreach ($results as $result): $invited_to_reception = $result['invited_to_reception']; $reception_list = explode( ',', $result['reception_list'] ); $party = explode( ',', $result['party'] ); // if invited to reception and everyone is invited if ( 'yes' === strtolower( $result['invited_to_reception'] ) && empty( $result['reception_list'] )){ $party_guests = $party; } else { // if invited to reception, but only selected people are invited $party_guests = array_intersect( $reception_list, $party ); } foreach ($party_guests as $index=>$guest): if ($invited_to_reception == "yes"): ?> Show checkbox options for reception <?php endif; endforeach endforeach; endif; ?>
但对id为2的行执行var_dump($party_guests)仅返回array(1) { [1]=> string(5) " Cathy" },仍无法正确展示Peter和Cathy的复选框,请求修复该逻辑问题。
内容的提问来源于stack exchange,提问作者Freddy
相关产品推荐
相关产品推荐

