1

我正在尝试编译一个 if 语句来检查回复类型是否为SingleMultiple通过检查 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]));

}

?>
4

2 回答 2

3

$qandaReplyType函数内部不存在。

您需要先包含global $qandaReplyType在函数内部,然后才能使用变量的全局版本。

复习一下PHP 中的变量范围不会有什么坏处。


编辑:从外观上看,$arrQuestionId也不$arrOptionType在函数范围内。
global $qandaReplyType, $arrQuestionId, $arrOptionType;

于 2013-02-13T21:08:09.603 回答
0

嗯嗯......你的功能......试试这个:

function ExpandOptionType($option) { 
$qandaReplyType = "Single";
$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>';
 }
 }
 }

如果可行,则 $qandaReplyType 未在函数“内部”定义....您应该找到一种方法来恢复该值 o 可以向函数添加另一个参数并传递 $qandaReplyType 值,例如:

function ExpandOptionType($option,$qandaReplyType)

萨卢多斯 ;)

于 2013-02-13T21:14:38.383 回答