Greetings,
I have searched the forum topics(and most of the www) and have not been
able to find an answer. I am so very close, but no prize yet.
I have an update form that I am populating from a mysql db. No problems except for
those dreaded checkboxes.
Here is what I have so far.
[code to call info from db]
/**************************************/
<?php
include 'dbconn.php';
$id = $_GET['id'];
$sql = $mysqli->query("SELECT * FROM $members_tbl WHERE id = '$id'");
while ($row= $sql->fetch_array()) {
$id = $row['id'];
$busname = $row['busname'];
$contact = $row['contact'];
$street = $row['street'];
$city = $row['city'];
$stzip = $row['stzip'];
$phone = $row['phone'];
$cell = $row['cell'];
$email1 = $row['email1'];
$email2 = $row['email2'];
$website = $row['website'];
$hours = $row['hours'];
$descrip = $row['descrip'];
$cat = $row['cat'];
//var_dump($cat);
$arr = explode(',',$row['cat']);
//var_dump($arr);
include 'fullCatList.php';
foreach ($arr as $c) {
//var_dump($c);
if (in_array($c, $fullCatList)) {
$checked = 'checked';
} else {
$checked = FALSE;
}
}
}
?>
/*************************************/
[end code]
This is the part where all the work goes for the checkboxes.
The variable $cat holds the values of all the categories selected.
/*******************************************/
$cat = $row['cat'];
//var_dump($cat);
$arr = explode(',',$row['cat']);
//var_dump($arr);
include 'fullCatList.php';
foreach ($arr as $c) {
//var_dump($c);
if (in_array($c, $fullCatList)) {
$checked = 'checked';
} else {
$checked = FALSE;
}
}
/******************************************/
The goal of all this is to re-populate the appropriate checkboxes.
You will notice the various âvar_dump()â. Here is what each outputs
/**********************************/
$cat = $row['cat'];
//var_dump($cat);
will output a string = string(18) "busHomeSvc,citySvc"
$arr = explode(',',$row['cat']);
//var_dump($arr);
will output = array(2) { [0]=> string(10) "busHomeSvc" [1]=> string(7) "citySvc" } as expected
foreach ($arr as $c) {
//var_dump($c);
will output = string(10) "busHomeSvc" string(7) "citySvc" I get the two check values as expected
/************************************/
The included file âfullCatList.phpâ is an array holding all the possible categories. I then
check each variable $c against the $fullCatList array(). If any of the $c values are in the
$fullCatList array, it then is told to turn the variable $checked into an echoed âcheckedâ, which works.
The problem is it turns on all the check values, even though they are not one of the $c values.
This is the problem.
How do I keep the other checkboxes from getting âturned onâ, when they have not been asked to.
Iâm sure my logic is flawed somewhere, I just canât find out where. As I said, I am so close, but no prize.
There are very good reasons I must approach it like this. I know there are other more efficient ways of
going aboutthis. If I was starting this project from scratch, I would certainly do things differently.
However, as it stands, Iâm sort of stuck going this way. Any help is greatly appreciated.