asyourattorney Posted January 9, 2008 Share Posted January 9, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85251-solved-php-data-driven-checkboxes/ Share on other sites More sharing options...
GingerRobot Posted January 9, 2008 Share Posted January 9, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/85251-solved-php-data-driven-checkboxes/#findComment-434940 Share on other sites More sharing options...
asyourattorney Posted January 9, 2008 Author Share Posted January 9, 2008 Perfect. Works like a dream, cheers. Quote Link to comment https://forums.phpfreaks.com/topic/85251-solved-php-data-driven-checkboxes/#findComment-434991 Share on other sites More sharing options...
GingerRobot Posted January 9, 2008 Share Posted January 9, 2008 No problem, if you're all done here - can you mark this as solved? Edit: Whoops, you already did Quote Link to comment https://forums.phpfreaks.com/topic/85251-solved-php-data-driven-checkboxes/#findComment-434992 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.