Tick checkbox if database field contains value

Hi Guys,

I have used a bit of PHP but I’m not very good at it!

I was hoping someone could help with my problem.

I have a table with several checkboxes.
The user can select the boxes, the values are made into a comma separated string and added to a DB field.
I want the checkboxes that have been previously checked (the field contents) to be checked when the user revisits that page.

So it’s something like.

if($field CONTAINS $checkBoxValue){
checkbox = checked
}

I am having trouble thinking of a way of implementing this.

Here is my form code:

<form method=post action=''>
        
    <table width="100">
    <tr>
    <td width="60">Identity</td>
    <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Identity'></td>
    </tr>
    
    <tr>
    <td width="60">Stationery</td>
    <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Stationery'></td>
    </tr>
    
    <tr>
    <td width="60">Print</td>
    <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Print'></td>
    </tr>
    
    <tr>
    <td width="60">Packaging</td>
    <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Packaging'></td>
    </tr>
    
    <tr>
    <td width="60">Exhibition</td>
    <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Exhibition'></td>
    </tr>
    
    <tr>
    <td width="60">Web</td>
    <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='Web'></td>
    </tr>
    
    <tr>
    <td width="60">3D</td>
    <td width="40"><input type="checkbox" <?php echo $checked; ?> name=fields[] value='3D'></td>
    </tr>
    
    </table><br>
    
    <input type="submit" name="editFields" value="Select">
    
    
    </form>

Here is my PHP code:

$checked = "";

$WordsToFind = "Identity,Stationery,Print,Packaging,Exhibition,Web,3D";
$WordsToFind = explode(',',$WordsToFind);

$text = "Identity,Stationery,Print,Packaging,Exhibition,Web,3D";

for($i = 0; $i<count($WordsToFind); $i++) {
    if(stripos($text,$WordsToFind[$i])) { 
        $checked='checked'; break; 
    }
}

NOTE: $WordsToFind is actually pulled from a DB. I have written it out so you can see the values.

The PHP works, just not in the way I would like. i.e. $checked does equal “checked” but I need it to only equal checked for the correct checkboxes.

Thanks in advance.

Sean