bradymiller wrote on Thursday, January 26, 2012:
Hi,
This is one of the gems of Rod’s layout engine. The parsing and entering of this data to and from the database is centralized in the library/options.inc.php library.
The above code uses data_type 23:
// a set of exam results; 3 radio buttons and a text field:
else if ($data_type == 23) {
$tmp = explode('|', $currvalue);
$avalue = array();
foreach ($tmp as $value) {
if (preg_match('/^([^:]+):(.*)$/', $value, $matches)) {
$avalue[$matches[1]] = $matches[2];
}
}
$maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
$fldlength = empty($frow['fld_length']) ? 20 : $frow['fld_length'];
$lres = sqlStatement("SELECT * FROM list_options " .
"WHERE list_id = ? ORDER BY seq, title", array($list_id) );
echo "<table cellpadding='0' cellspacing='0'>";
echo "<tr><td> </td><td class='bold'>" .
htmlspecialchars( xl('N/A'), ENT_NOQUOTES) .
" </td><td class='bold'>" .
htmlspecialchars( xl('Nor'), ENT_NOQUOTES) . " </td>" .
"<td class='bold'>" .
htmlspecialchars( xl('Abn'), ENT_NOQUOTES) . " </td><td class='bold'>" .
htmlspecialchars( xl('Date/Notes'), ENT_NOQUOTES) . "</td></tr>";
while ($lrow = sqlFetchArray($lres)) {
$option_id = $lrow['option_id'];
$option_id_esc = htmlspecialchars( $option_id, ENT_QUOTES);
$restype = substr($avalue[$option_id], 0, 1);
$resnote = substr($avalue[$option_id], 2);
// Added 5-09 by BM - Translate label if applicable
echo "<tr><td>" . htmlspecialchars( xl_list_label($lrow['title']), ENT_NOQUOTES) . " </td>";
for ($i = 0; $i < 3; ++$i) {
$inputValue = htmlspecialchars( $i, ENT_QUOTES);
echo "<td><input type='radio'" .
" name='radio_{$field_id_esc}[$option_id_esc]'" .
" id='radio_{$field_id_esc}[$option_id_esc]'" .
" value='$inputValue'";
if ($restype === "$i") echo " checked";
echo " /></td>";
}
$fldlength = htmlspecialchars( $fldlength, ENT_QUOTES);
$maxlength = htmlspecialchars( $maxlength, ENT_QUOTES);
$resnote = htmlspecialchars( $resnote, ENT_QUOTES);
echo "<td><input type='text'" .
" name='form_{$field_id_esc}[$option_id_esc]'" .
" id='form_{$field_id_esc}[$option_id_esc]'" .
" size='$fldlength'" .
" maxlength='$maxlength'" .
" value='$resnote' /></td>";
echo "</tr>";
}
echo "</table>";
}
// a set of exam results; 3 radio buttons and a text field:
else if ($data_type == 23) {
$tmp = explode('|', $currvalue);
$avalue = array();
foreach ($tmp as $value) {
if (preg_match('/^([^:]+):(.*)$/', $value, $matches)) {
$avalue[$matches[1]] = $matches[2];
}
}
$maxlength = empty($frow['max_length']) ? 255 : $frow['max_length'];
$fldlength = empty($frow['fld_length']) ? 20 : $frow['fld_length'];
$lres = sqlStatement("SELECT * FROM list_options " .
"WHERE list_id = ? ORDER BY seq, title", array($list_id) );
echo "<table cellpadding='0' cellspacing='0'>";
echo "<tr><td> </td><td class='bold'>" .
htmlspecialchars( xl('N/A'), ENT_NOQUOTES) .
" </td><td class='bold'>" .
htmlspecialchars( xl('Nor'), ENT_NOQUOTES) . " </td>" .
"<td class='bold'>" .
htmlspecialchars( xl('Abn'), ENT_NOQUOTES) . " </td><td class='bold'>" .
htmlspecialchars( xl('Date/Notes'), ENT_NOQUOTES) . "</td></tr>";
while ($lrow = sqlFetchArray($lres)) {
$option_id = $lrow['option_id'];
$option_id_esc = htmlspecialchars( $option_id, ENT_QUOTES);
$restype = substr($avalue[$option_id], 0, 1);
$resnote = substr($avalue[$option_id], 2);
// Added 5-09 by BM - Translate label if applicable
echo "<tr><td>" . htmlspecialchars( xl_list_label($lrow['title']), ENT_NOQUOTES) . " </td>";
for ($i = 0; $i < 3; ++$i) {
$inputValue = htmlspecialchars( $i, ENT_QUOTES);
echo "<td><input type='radio'" .
" name='radio_{$field_id_esc}[$option_id_esc]'" .
" id='radio_{$field_id_esc}[$option_id_esc]'" .
" value='$inputValue'";
if ($restype === "$i") echo " checked";
echo " /></td>";
}
$fldlength = htmlspecialchars( $fldlength, ENT_QUOTES);
$maxlength = htmlspecialchars( $maxlength, ENT_QUOTES);
$resnote = htmlspecialchars( $resnote, ENT_QUOTES);
echo "<td><input type='text'" .
" name='form_{$field_id_esc}[$option_id_esc]'" .
" id='form_{$field_id_esc}[$option_id_esc]'" .
" size='$fldlength'" .
" maxlength='$maxlength'" .
" value='$resnote' /></td>";
echo "</tr>";
}
echo "</table>";
}
// a set of exam results; 3 radio buttons and a text field:
else if ($data_type == 23) {
$tmp = explode('|', $currvalue);
$avalue = array();
foreach ($tmp as $value) {
if (preg_match('/^([^:]+):(.*)$/', $value, $matches)) {
$avalue[$matches[1]] = $matches[2];
}
}
$lres = sqlStatement("SELECT * FROM list_options " .
"WHERE list_id = ? ORDER BY seq, title", array($list_id) );
$s .= "<table cellpadding='0' cellspacing='0'>";
while ($lrow = sqlFetchArray($lres)) {
$option_id = $lrow['option_id'];
$restype = substr($avalue[$option_id], 0, 1);
$resnote = substr($avalue[$option_id], 2);
if (empty($restype) && empty($resnote)) continue;
// Added 5-09 by BM - Translate label if applicable
$s .= "<tr><td class='bold' valign='top'>" . htmlspecialchars(xl_list_label($lrow['title']),ENT_NOQUOTES) . " </td>";
$restype = ($restype == '1') ? xl('Normal') : (($restype == '2') ? xl('Abnormal') : xl('N/A'));
// $s .= "<td class='text' valign='top'>$restype</td></tr>";
// $s .= "<td class='text' valign='top'>$resnote</td></tr>";
$s .= "<td class='text' valign='top'>" . htmlspecialchars($restype,ENT_NOQUOTES) . " </td>";
$s .= "<td class='text' valign='top'>" . htmlspecialchars($resnote,ENT_NOQUOTES) . "</td>";
$s .= "</tr>";
}
$s .= "</table>";
}
else if ($data_type == 23) {
// $_POST["form_$field_id"] is an array of text fields with companion
// radio buttons to be imploded into "key:n:notes|key:n:notes|...".
foreach ($_POST["form_$field_id"] as $key => $val) {
$restype = $_POST["radio_{$field_id}"][$key];
if (empty($restype)) $restype = '0';
$val = str_replace('|', ' ', $val);
if (strlen($value)) $value .= '|';
$value .= "$key:$restype:$val";
}
}
My thoughts are that it makes sense to make another data type (basically clone the elements above) that has 3 radio buttons, text field, and date field. And will have it flexible, so it can read either:
name:toggle:note
name:toggle:note:date
but only write:
name:toggle:note:date
This should only take some minor mods to the above code along with a couple other mods in other places:
change the data type for this field in layouts within database.sql and the upgrade file
enter the new datatype in the layouts configuration gui (one line to do this)
-brady