Thanks for your help. I got it working with a few minor changes to the code I had been working with (which is some that I grabbed off an existing script I have that is a similar situation). Based on your replies, I realized that I really didn't explain the situation that clearly I guess.
This is part of an administrative form for the backend of a website. This section is one field in a larger form where the admins can edit existing records or add new records.
drawmack: That code works for printing the checkboxes, but didn't help me with regards to inserting the "checked" value on existing records.
benkillin: I understand using "return", but in this case, I'm not returning a value per se, just shelling out from the form to the library of functions to perform the queries etc. Hope that makes sense.
Anyway, here's the working code:
This prints the list of checkboxes from the profile db and then populates (adds "checked") to the appropriate ones based on the cross reference table called profile_to_offer (one offer can have several profiles):
function CheckBox ($name,$query) {
global $offer_id;
if ($offer_id != "") {
$qid = db_query("Select * FROM profile_to_offer "
."WHERE offer_id = $offer_id");
while ($myrow = mysql_fetch_array($qid)) {
$list_index = $myrow["profile_id"];
$permlist[$list_index] = $list_index;
}
}
$qry = db_query($query);
$rcnt = mysql_num_rows($qry);
$fcnt = mysql_num_fields($qry);
$fidx = 0; while ($fidx < $fcnt) { $fname[$fidx] = mysql_field_name($qry, $fidx); $fidx++; }
for ($ridx = 0; $ridx < $rcnt; $ridx++) {
$number = mysql_result($qry,$ridx,$fname[0]);
echo "<input type=\"checkbox\" name=\"list[$number]\" value=\"$number\"";
if(isset($permlist[$number])) echo ($number == $permlist[$number]) ? " checked" : "";
echo "> ".mysql_result($qry,$ridx,$fname[1])."<br>\n";
}
mysql_free_result($qry);
}
And this updates existing and/or inserts new records:
// delete all previous records -- it's just easier to do this.
$qid2 = db_query("
DELETE FROM profile_to_offer
WHERE offer_id = $offer_id
");
if (!$qid2)
{
echo "<font color=#0000CC>Update-Delete profile_to_offer:
<p><pre>".mysql_errno().": ".mysql_error()."
<br></pre></font>\n";
exit;
}
//step through the $list[] and see what's set and insert them
reset($list);
while (list($key,$value) = each($list))
{
$qid2 = db_query("
INSERT into profile_to_offer
VALUES ($offer_id,$value)");
if (!$qid2)
{
echo "<font color=#0000CC>Update profile_to_offer List
<p><pre>".mysql_errno().": ".mysql_error()."
<br></pre></font>\n";
it;
}
}[code=php]