0

I am trying to display a checked checkbox if the store has been assigned a value in the database, i have the following but am not sure why it is not working, it is currently not checking any checkboxes even though they have values assigned.

$stores = array();
    $stmt = DB::query(Database::SELECT, 'SELECT `id`, `store`, `store_name`, `branch_name` FROM `mystores` WHERE `site` = :site ORDER BY `store_name` ASC, `branch_name` ASC');
    $stmt->param(':site', $this->permissions['site'][0]);
    try {
        $result = $stmt->execute()->as_array();
        if (count($result) >= 1) {
            foreach ($result as $row) {
                $checked = in_array($row['store'], $assigns) ? 'checked="checked"' : '';
                $stores[] = '<label><input type="checkbox" name="stores['.$row['store'].']" value="'.$row['store'].'"'.$checked.' />'.$row['store'].' - '.$row['store_name'].' - '.$row['branch_name'].'</label>';
            }
        }
    } catch (Exception $e) {
        FB::error($e);
    }
1
  • Did you check the contents of $row['store'] and $assigns? Commented Mar 18, 2014 at 17:02

2 Answers 2

1

I think you miss a space here .'"'.$checked.' and it should be .'" '.$checked.'

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

1 Comment

other things is fine (ideone.com/N6WFM3), check $assigns array and query
0

I banged my head against that in_array conditional (writing it at least three different ways) and various table-joined mysqli_querys for two days before I gave up on either approach working. No matter what I did I could not get the code to check checkboxes automatically according to values in the array.

In the end I devised my own solution with jQuery and it works like a charm.

Check it out here if you are interested: https://stackoverflow.com/a/26433841/1896348

Comments

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.