Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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? :)

share|improve this question
 
you missed posting the rest of your code that is related to the above code. –  Prix Aug 29 at 9:48
 
Please provide your sql statements to select the checkedusers. –  Dennis Meissner Aug 29 at 9:48
 
Thanks for notice - i have edited the topic :) –  Simon Duun Aug 29 at 9:52

2 Answers

up vote 1 down vote accepted
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 } ?>
share|improve this answer
 
I have changed my original post a little. Please review if you have the time :) –  Simon Duun Aug 29 at 9:59
 
this should work,there is no selected variable declared in the loop –  Bhadra Aug 29 at 10:01
 
Worked perfectly. Thanks for you help, i will definitely check up on in_array :) –  Simon Duun Aug 29 at 10:08

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

if ($users['user_id'] == $chosencategories) {}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.