sleepydad wrote:I have the following FOREACH comparing a session variable to a database string delimited with ",". Assuming that string is "1,2" and $_SESSION['id']="1", it should break the loop and forget the ELSE - shouldn't it??
Yes. Here is an example script:
<?php
$input = "1,2";
$id = "1";
foreach (explode(",", $input) as $subscriber) {
if ($subscriber == $id) {
echo '<input type="checkbox" name="subscription" value="unsubscribe" /> Unsubscribe from this topic';
break;
} else {
echo '<input type="checkbox" name="subscription" value="subscribe" /> Subscribe to this topic';
}
}
Running this script, I got the output of a checkbox and "Unsubscribe from this topic". In other words, the problem is more likely to lie with your input than with the code logic, e.g., $_SESSION['id'] does not contain "1" or "2".
By the way, this is a poor approach to begin with. Your database field should not be a multi-valued field. Instead of storing values like "1,2", you should be normalising your database to have different rows.