0

I have an array with 8 different application names in it. I loop through it and check a checkbox if a user has selected that option on previous login.

foreach($appsArray as $app){ 
    $query = " SELECT UK, SPAIN, FRANCE, BENELUX, GERMANY, SWEEDEN 
    FROM ".$app." 
    WHERE userid = ".$_SESSION['user']['id']; 

    $stmt = $db->prepare($query); 
    $result = $stmt->execute(); 
    $result = $stmt->fetchAll(); 

    echo("<td>".$app."</td> <td><center>
    <input type = 'checkbox' name=\"".$app."[]\"". 
    "value =\"".$app."UK\" checked=\"".$result[0]['UK']."\"/> ");

    $result=""; 
} 

This works to an extent, however- what options I select for the first app is reflected across all of them. This must be to do with the way foreach loops in php, however I'm not sure how.

Any advice would be truly appreciated.

EDIT: just to clarify, the options I pick for one application are reflected across all application check boxes. I want them to be independent.

7
  • I'd suggest use proper code formatting for start Commented Nov 1, 2013 at 15:21
  • I have fixed too many of posts like this. Please fix the code formatting Commented Nov 1, 2013 at 15:22
  • sure- copy and paste messed up. Apologies. Commented Nov 1, 2013 at 15:23
  • @Programatt: You might want to read this question -- I've fixed it for you this time. :) Commented Nov 1, 2013 at 15:23
  • What exactly are you expecting this code to do? Commented Nov 1, 2013 at 15:24

1 Answer 1

1

I believe the issue could be more of how you are using the value for the checkbox. I've ran into some issues where the "checked" attribute doesn't like to work if you aren't using checked="checked". Try something like this.

$checked = $result[0]['UK'] ? ' checked="checked"' : '';

echo '<td>'.$app.'</td><td><center><input type="checkbox" name="'.$app.'[]"'. 
    ' value="'.$app.'UK"'.$checked.' /> ';

If this still doesn't work, make sure to do a var_dump or similar on the $result to make sure your query is working correctly.

Once you have this working, I would possibly see about combining those queries into one. I could help having 8 sub-queries hitting the database instead of 8 separate database calls.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, I checked and my query is running correctly. I am using checked = "on"..could this still work?
For HTML4 it should be checked, for XHTML it should be checked="checked". checked="on" shouldn't be valid.
so even though they are being checked with "on", it could still be wrong?
If you are using XHTML, yes. I think HTML4 is more lenient. Check the source code of the page (from the browser) to see what is being generated. If all of them still are having the attribute added, then double-check the $result[0] is the correct data you are expecting.
you are correct so thanks! The reason all boxes are checked is because it seems checked="off" still ticks a box. Even checked =" " checks the box. Any suggestions of what I can put as the value to not check it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.