0

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. If invited_to_reception is yes and that row has no reception_list defined, then everyone in the party is invited. If reception_list has values, then only those members from party 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 in party is invited. However, when I access that invite, it does not show the reception checkbox. Naturally this is because I have an in_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 from party 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.
1
  • 1
    If I understand it correctly, you can rewrite your code to something like this which basically uses array_intersect(). However, note that this is a PHP question, and shouldn't be asked here.
    – Sally CJ
    Feb 25 at 4:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.