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

如何基于动态单选按钮结果将宾客分入attending_array与not_attending_array?

问题

需要为宴会宾客生成单选按钮选择出席状态,一个宴会可能包含多名宾客,需将选择出席/缺席的宾客分别存入attending_array和not_attending_array后写入WordPress数据库。当前生成单选按钮的代码无法动态区分用户选择,无法正确归类宾客。

现有代码:

<?php
foreach ($results as $result) { 
    $party = $result['party']; // 数据库varchar类型,值为"John, Doe"
    $party_guests = explode(",", $party);

    $attending_array = array();
    $not_attending_array = array();
  
    foreach ($party_guests as $index => $guest){
        ?>
        <span class="guest__name"><?php echo $guest; ?></span>
        <div class="guest__options-option">
            <span>Attending?</span>
            <div class="guest__options-group">
                <input class="guest__attendance-input" id="attending_array[<?php echo $index; ?>]" type="radio" name="attending-<?php echo $index; ?>" value="<?php echo $guest; ?>" required/>
                <label for="attending-yes-<?php echo $index; ?>">Yes</label>
            </div>
            <div class="guest__options-group">
                <input class="guest__attendance-input" id="attending_array[<?php echo $index; ?>]" type="radio"  name="attending-<?php echo $index; ?>" value="<?php echo $guest; ?>" required/>
                <label for="attending-no-<?php echo $index; ?>">No</label>
            </div>
        </div>
        <?php
    }
}
?>

解决方案

1. 优化前端单选按钮代码

现有代码存在三个核心问题:同一宾客的单选按钮value重复、id与label不关联、字段命名不利于批量处理。修改后的代码如下:

<?php
foreach ($results as $result) { 
    $party = $result['party'];
    // 分割后去除姓名前后空格,避免无效字符
    $party_guests = array_map('trim', explode(",", $party));
  
    foreach ($party_guests as $index => $guest){
        // 生成唯一ID,关联label与单选框
        $yes_id = "attending-yes-{$index}";
        $no_id = "attending-no-{$index}";
        ?>
        <span class="guest__name"><?php echo esc_html($guest); ?></span>
        <div class="guest__options-option">
            <span>Attending?</span>
            <div class="guest__options-group">
                <!-- 用数组格式命名name,后端可直接批量获取选择状态 -->
                <input class="guest__attendance-input" id="<?php echo $yes_id; ?>" 
                       type="radio" name="attendance[<?php echo esc_attr($guest); ?>]" 
                       value="yes" required/>
                <label for="<?php echo $yes_id; ?>">Yes</label>
            </div>
            <div class="guest__options-group">
                <input class="guest__attendance-input" id="<?php echo $no_id; ?>" 
                       type="radio" name="attendance[<?php echo esc_attr($guest); ?>]" 
                       value="no" required/>
                <label for="<?php echo $no_id; ?>">No</label>
            </div>
        </div>
        <?php
    }
}
?>

关键修改点:

  • 用array_map('trim', ...)清理姓名前后空格
  • 为每个单选框生成唯一id,确保label能正确关联
  • 表单name采用attendance[宾客姓名]的数组格式,方便后端遍历
  • 两个单选按钮的value设为yes/no,明确区分选择状态

2. 后端处理表单提交

在接收表单的PHP文件中,通过遍历提交的数组完成宾客归类:

<?php
// 初始化归类数组
$attending_array = array();
$not_attending_array = array();

// 检查表单提交状态
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['attendance'])) {
    // 遍历所有宾客的选择结果
    foreach ($_POST['attendance'] as $guest_name => $status) {
        // 过滤姓名,确保数据安全
        $clean_guest = sanitize_text_field($guest_name);
        
        // 根据选择状态归类
        if ($status === 'yes') {
            array_push($attending_array, $clean_guest);
        } elseif ($status === 'no') {
            array_push($not_attending_array, $clean_guest);
        }
    }

    // 将数组写入WordPress数据库(示例)
    global $wpdb;
    $wpdb->insert(
        'your_table_name', // 替换为你的目标表名
        array(
            'attending' => json_encode($attending_array), // 用JSON格式存储数组
            'not_attending' => json_encode($not_attending_array)
        ),
        array('%s', '%s')
    );
}
?>

说明:

  • 用sanitize_text_field过滤输入,避免恶意内容
  • 可选json_encode或serialize存储数组,JSON格式更易读取
  • 若需关联特定宴会,可在表单中添加隐藏字段传递宴会ID,后端一并存入数据库

内容的提问来源于stack exchange,提问作者Freddy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 03:05:42