Toggle show/hide image multiple times in PHP using for loop
P粉567281015
P粉567281015 2023-09-13 15:06:29
0
1
609

I have users on my site, the user selects their group 1/2/3 from their profile. Based on their selection, they are divided into 3 arrays:

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

Sample data:

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

This means user A selected group 1 and group 3. Likewise, User B selected groups 1, 2, and 3.

Now, I am creating the image and selecting group 1/2/3 when creating the image. So based on user group selection I want to show/hide a picture:

Example: If I create an image for Group 1 and Group 2, only users who have selected these groups can see the image. And if a user selects group 1 and group 2, they can only see one picture. No duplication.

I followed the following 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 "没有匹配的受众类型";
    }

This logic is not entirely valid. When creating pictures for all 3 groups, the user only selects group 2 or group 3 and the pictures are not displayed. This logic leaves the loop once it finds a matching group. Any help on how to fix this?

PS: This is a sample code. Please ignore parsing/syntax errors

P粉567281015
P粉567281015

reply all(1)
P粉178894235

Looks like you have some confusion between image_group and group_recipients, and you are using $g instead of user

<?php
//这个图片所属的组
$image_groups = [1, 2, 3];

//每个组的收件人
$group1_recipients = ["用户A","用户B","用户C"];
$group2_recipients = ["用户D","用户B","用户E"];
$group3_recipients = ["用户A","用户B","用户E"];

//所有收件人的数组
$all_groups = [1=> &$group1_recipients,
               2=> &$group2_recipients,
               3=> &$group3_recipients];

//用户ID
$user = '用户A';

//初始设置为false
$match = false;

// 遍历每个image_group
foreach ($image_groups as $group) {

   //获取groupn_recipients数组
   $recipients = &$all_groups[$group];

   //如果用户在收件人数组中,则将Match设置为Group
   if (in_array ($user, $recipients)) {
       $match = $group;
       break;
   }
}


if ($match) {
  echo "与组 $group 匹配\n";
 }
else {
  echo '不匹配', "\n";
}

?>

This is one method. There are many variations. This sets $match to false or the matched group.

  1. Do not use 0 as a group (it is equivalent to false)
  2. These ampersands are pointers, so the contents of the array will not be copied. I'm assuming these arrays can get quite large.
  3. In practice you would put it in a function
  4. I'll expand this to clarify each step.
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template