我正在尝试编译一个 if 语句来检查回复类型是否为Single
或Multiple
通过检查 mysqli 变量。如果Single
则将选项输出为单选按钮,如果Multiple
则将选项输出为复选框。
但是我在 if 语句中收到一个未定义的变量错误$qandaReplyType
。如何解决这个问题?
下面的代码:
$qandaquery = "SELECT q.QuestionId, r.ReplyType
FROM Question q
LEFT JOIN Reply r ON q.ReplyId = r.ReplyId
WHERE SessionId = ?
GROUP BY q.QuestionId
ORDER BY RAND()";
$qandaqrystmt=$mysqli->prepare($qandaquery);
// get result and assign variables (prefix with db)
$qandaqrystmt->execute();
$qandaqrystmt->bind_result($qandaQuestionId,$qandaReplyType);
$arrReplyType = array();
while ($qandaqrystmt->fetch()) {
$arrReplyType[ $qandaQuestionId ] = $qandaReplyType;
}
$qandaqrystmt->close();
function ExpandOptionType($option) {
$options = explode('-', $option);
if(count($options) > 1) {
$start = array_shift($options);
$end = array_shift($options);
do {
$options[] = $start;
}while(++$start <= $end);
}
else{
$options = explode(' or ', $option);
}
if($qandaReplyType == 'Single'){
foreach($options as $indivOption) {
echo '<div class="ck-button"><label class="fixedLabelCheckbox"><input type="radio" name="options_<?php echo $key; ?>[]" id="option-' . $indivOption . '" value="' . $indivOption . '" /><span>' . $indivOption . '</span></label></div>';
}
}else if($qandaReplyType == 'Multiple'){
foreach($options as $indivOption) {
echo '<div class="ck-button"><label class="fixedLabelCheckbox"><input type="checkbox" name="options_<?php echo $key; ?>[]" id="option-' . $indivOption . '" value="' . $indivOption . '" /><span>' . $indivOption . '</span></label></div>';
}
}
}
foreach ($arrQuestionId as $key=>$question) {
echo ExpandOptionType(htmlspecialchars($arrOptionType[$key]));
}
?>