You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress/PHP:修复宴请接待复选框的列值与数组匹配问题

问题描述

我有一张名为wp_invites的表,存储活动受邀人员信息,表中数据如下:

idhandlerlead_namepartyreferenceinvited_to_receptionreception_list
1FreddyJohnJohn, Ben, StevenNgxaIyNybK4Oyes
2JasonPeterPeter, Jamie, Cathy6iqIWqdxewf0yesPeter, Cathy
3JasonMarkMarkMx7z9UnCPOQoyes
4JasonKateKate, Louise, LucygumWlKmfmyXgno

业务规则

  • 仅部分群体可受邀参加接待会,由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; ?>

存在问题

  1. id为1的行,invited_to_reception为yes,应显示所有嘉宾复选框,但实际未展示,因in_array()判断无法覆盖全员受邀场景;
  2. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 02:18:12