質問

I am trying to code a php script that automaticly selects the selected users (checkboxes) I don't know what it is i am missing, maybe you guys can help out. What i am trying is:

$get_users = $mysqli->query("SELECT * FROM users WHERE user_type='5' AND user_id_parent='". get_uid() ."'");

$get_checkedusers = $mysqli->query("SELECT * FROM modules_users_availability WHERE module_id='". $mid ."' AND shopid='". get_uid() ."'
");

while ($row = $getcheckedusers->fetch_assoc()) {
     $chosenCategory[] = $row['userid'];                    
}

while($users = $getusers->fetch_assoc()){
     foreach($chosenCategory as $chosencategories){
          if($users['user_id'] == $chosenCategory){
               echo $checked = "checked";                       
          }                 
     }?>

<tr>
            <td><input type="checkbox" <?php echo $selected; ?> name="access[]" value="<?php echo $users['user_id']; ?>" /></td>
            <td><?php echo $users['name']; ?></td>
            <td><?php echo $users['email']; ?></td>
        </tr>
        <?php } ?>

My problem is that i can't use the getcheckedusers array correctly in getusers while. It returns trible result if there is 3 users, and double result if here is 2. Maybe i'm missing something? :)

役に立ちましたか?

解決

while($users = $getusers->fetch_assoc()){
     $checked="";
      if(in_array($users['user_id'],$chosenCategory)){
           $checked = "checked";                       

     }?>

<tr>
        <td><input type="checkbox" <?php echo $checked; ?> name="access[]" value="<?php echo $users['user_id']; ?>" /></td>
        <td><?php echo $users['name']; ?></td>
        <td><?php echo $users['email']; ?></td>
    </tr>
    <?php } ?>

他のヒント

If condition is wrong.. I hope it should like this

if ($users['user_id'] == $chosencategories) {}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top