I have a table called wp_invites
which contains a list of names (and other data) for those invited to an event. Below are 4 rows from the table:
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 |
To explain the above:
- Only certain groups are invited to the reception (defined by
invited_to_reception
) - In some instances, only certain members of a
party
are invited to the reception. Ifinvited_to_reception
isyes
and that row has noreception_list
defined, then everyone in theparty
is invited. Ifreception_list
has values, then only those members fromparty
are invited. reference
is unique identifier which determines which invite to show. For example,rsvp.com?ref=NgxaIyNybK4O
will show invites for "John, Ben, Steven"
What I'm trying to do is check if the names in reception_list
are in party
and if so, show them a checkbox option to ask if they're attending the reception.
Current methodology:
<?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; ?>
Issues with the above:
- Firstly, for the row with
id
1,invited_to_reception
is "yes" meaning everyone inparty
is invited. However, when I access that invite, it does not show the reception checkbox. Naturally this is because I have anin_array()
check running beforehand, but unsure how I can run both instances (where all are invited and in other cases some are invited) without duplicating the code? - For the row with
id
2, only 2 people fromparty
are invited to the reception. However, when I access this invite, only one of the names has the reception checkboxes, when it should appear for both of them.
array_intersect()
. However, note that this is a PHP question, and shouldn't be asked here.