0

I have a column(apps) in the database which has a list of words separated with commas like - AAA, BBB, CCC I have another column(appsSelected) that will be populated in the same format with the words that the user checks with check boxes values.

On one page i list all of the words in apps with check boxes beside them, on submit it inserts those checked into appsSelected. On another page it lists only the words in the appsSelected column, I have managed to grab appsSelected and explode it to list each word.

<?php
$appsString = $pi_row['appsSelected'];
// break $appsString using the comma as the delimiter
$appliances = explode(', ', $appsString);                                    
// loop through and print all the words
echo '<ul>';
    for ($i = 0; $i < count($appliances); $i++)
    {   
        echo '<li>' . $appliances[$i] . '</li><br/>';
    }
echo '</ul>';
?>

The problem i have is when i go back to the page with check boxes, i want them to stay checked on the words that are in appsSelected. So im guessing i have to compare apps with appSelected and echo out either a checked box or un-checked depending on what words match.

for ($i = 0; $i < count($appliances); $i++)
{
$codes = array($pi_row['apps']);
$codesSelected = array($pi_row['appsSelected']);
//if (in_array($appliances[$i], $codesSelected)) {

if (array_intersect($ap, $aps)) {
    echo '<li><input type="checkbox" checked="checked" value="' . $appliances[$i] . '"     name="applianceCheckbox[]">' . $appliances[$i] . '</input></li><br/>';
} else{
    echo '<li><input type="checkbox" value="' . $appliances[$i] . '" name="applianceCheckbox[]">' .      $appliances[$i] . '</input></li><br/>';
}
}

The code above only works if appsSelected matches the entire array in apps: for example in the apps column - AAA, BBB, CCC if i tick AAA and BBB or any other combonation that doesnt involve matching every word in apps no check boxes get ticked but if i tick AAA, BBB, CCC the check boxes will return all ticked

I need to find a way to see if any word in apps matches any word in appSelected.

3
  • But they're called applianceCheckbox[]. Shouldn't they be applianceCheckbox[][$i] or something like that? Commented May 8, 2014 at 12:25
  • Use in_array like you commented out, just don't forget to create the array by using explode(', ', $pi_row['appsSelected']) as you do in your first post and you should be fine. Commented May 8, 2014 at 12:31
  • Thanks alot! @ccKep it worked when i exploded it. Commented May 8, 2014 at 12:42

1 Answer 1

1

Use in_array like you did in your comment, you have to pass it the correct array though:

$codesSelected = explode(', ', $pi_row['appsSelected']);

for ($i = 0; $i < count($appliances); $i++)
{
    if (in_array($appliances[$i], $codesSelected)) {
        echo '<li><input type="checkbox" checked="checked" value="' . $appliances[$i] . '"     name="applianceCheckbox[]">' . $appliances[$i] . '</input></li><br/>';
    } else {
        echo '<li><input type="checkbox" value="' . $appliances[$i] . '" name="applianceCheckbox[]">' .      $appliances[$i] . '</input></li><br/>';
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Another thing, if i have 2 of the same words in the array and only tick one of the checkboxes, all of the same words end up ticked. how would i go about just keeping it to which one i ticked? Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.