我想編譯一個if語句,通過檢查mysqli變量來檢查答覆類型是Single
還是Multiple
。如果Single
然後輸出選項作爲單選按鈕,如果Multiple
然後輸出選項作爲複選框。在if語句中的未定義變量
但是,我在$qandaReplyType
的if語句中收到未定義的變量錯誤。這個問題怎麼解決?下面
代碼:
$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]));
}
?>
我不能看到從任何你定義$ qandaReplyType變量的地方,它通常是空的或未定義的。 – 2013-02-13 21:10:16