Jump to content

[SOLVED] PHP Data Driven Checkboxes


asyourattorney

Recommended Posts

This title is a bit of a misnomer but I didn't want to make it anything too overwhelming.  Here is my situation:

 

I have a form that is a long list of checkboxes driven by a data table, it relates to possible psychiatric issues.  It looks like this:

 

condition_id

condition_name

condition_type

 

I am presenting it on the form as follows:

 

<?

 

$var = "select * from medical_conditions where condition_type = 'warrior'";

$lresult = mysql_query($var);

$myconsql = "select condition_id from application_medical_conditions where app_id = " . $appid;

$myconres = mysql_query($myconsql);

$myconarray = mysql_fetch_array($myconres);

 

while($ldata = mysql_fetch_array($lresult)){

if (in_array($ldata['condition_id'], $myconarray)) {

$stringmodifier = "checked />";

} else {

$strmodifier = " />";

}

$string = "<input type = 'checkbox' name = '" . $ldata['condition_id'];

$string = $string . "' class = 'boxstyle' ";

$string = $string . $strmodifier;

 

 

?>

<span class = 'plabel'><? echo $ldata['condition_name'] ?></span>

<span class = 'pinfo'>

<? print $string ?>

</span>

 

<?

};

?>

 

Looking at the code you can probably see my issue, I am working with one table to present the general information, and another table to tell whether or not the condition exists on a specific application, what I want is to do the following:

 

Return the checkbox as checked if the condition from the lookup table also exists in the application table.

 

e.g. If the applicant has that condition (defined as existing in the application_medical_conditions table) I want to present the checkbox as checked.  If the applicant does not have the condition, I still want to present the condition on the page, as a checkbox that is not checked.

 

I realize this is quite confusing, but any help would be appreciated.  I see my methodology is flawed in the use of the in_array function, but unfortunately am a little hazy on mysql_fetch_array.

Link to comment
Share on other sites

Well, you can accomplish a lot with the mysql query. With this, i've assumed that you have a field called condition_id in both tables. Try:

 

<?php
$sql = "SELECT m.condition_name, m.condition_id,a.app_id FROM medical_conditions as m ";
$sql .= "LEFT JOIN application_medical_conditions as a ON a.condition_id=m.condition_id ";
$sql .= "WHERE (a.app_id=$appid OR a.app_id is null) AND m.condition_type='warrior'";
$result = mysql_query($sql) or die(mysql_error());
while(list($condtion_name,$condition_id,$app_id) = mysql_fetch_row($result)){
if($app_id != NULL){
	$checked = 'checked="checked"';
}else{
	$checked = '';
}
echo $condition_name. ' <input type="checkbox" name="'.$condition_id.'" '.$checked.' /><br />'."\n";
}
?>

 

My mysql can be a bit rusty at times, but i think that should do the trick.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.