Show/hide images based on choice - multiple looping of switch inside for - PHP

Viewed 31

I have users in my site and the users choose their group 1/2/3 from their profile. Based on their choice they are grouped into 3 arrays:

$group1_recipients[], $group2_recipients[], $group3_recipients[]

Example data:

$group1_recipients = ["user A","user B","user C"],
$group2_recipients = ["user D","user B","user E"], 
$group3_recipients = ["user A","user B","user E"]

This means User A has chosen Group1 and Group3. Similarly User B has chosen Group1,2 and 3.

Now, I am creating images and I choose Groups 1/2/3 while creating the images. So based on the users groups selection I want to show/hide an image:

Example: If i create an image for Group 1 and Group 2, only the users who have selected those groups can see the image. Also if a user has selected both Group1 and Group2 they should be able to see only 1 image. No duplicates.

I followed the below logic:

$groups = ["1", "2", "3"];
    $match = false;
    
    foreach ($groups as $g) {
        if (in_array($g, $groups)) {
            $match = true;
            break;
        }
    }
    
     if(true===$match)
    {
      if($g == "1"){
        $audience = $group1_recipients;           
      }
        else if($g == "2"){
        $audience = $group2_recipients;  
      }
        else if($g == "3"){
        $audience = $group3_recipients; 
      }
    }
    else
    {
      echo "No matching audience type";
    }

This logic doesn't fully work. When an image is created for all 3 groups and the user has selected only Group 2 or Group 3, the image is not showing. The logic leaves the loop as soon as one matching group is found. Any help on how this can be fixed?

PS: This is a sample code. Pls ignore parse/syntactic errors

1 Answers

It looks like you are getting confused between image_group and group_recipients and you are using $g instead of user

<?php
//The groups this image is for,
$image_groups = [1, 2, 3];

//Recipients for each group
$group1_recipients = ["user A","user B","user C"];
$group2_recipients = ["user D","user B","user E"];
$group3_recipients = ["user A","user B","user E"];

//Array of all recipients
$all_groups = [1=> &$group1_recipients,
               2=> &$group2_recipients,
               3=> &$group3_recipients];

//The User Id
$user = 'user A';

//Start with false
$match = false;

// Loop in each image_group
foreach ($image_groups as $group) {

   //Get the groupn_recipients array
   $recipients = &$all_groups[$group];

   //Set Match to Group if its in the recipients array
   if (in_array ($user, $recipients)) {
       $match = $group;
       break;
   }
}


if ($match) {
  echo "matches with group $group\n";
 }
else {
  echo 'does not match', "\n";
}

?>

This is one way to do it. There are many variations. This sets $match to false or to the group that was matched.

  1. Do not use 0 for group (it equates to false)
  2. The ampersands are pointers, so the contents of the arrays are not duplicated. I am assuming that these arrays might get rather large.
  3. In practice you would put it in a function
  4. I have spread it out to clarify the steps.
Related