1

I am trying to compile an if statement to check if a reply type is Single or Multiple by checking the mysqli variable. If Single then output options as radio buttons, if Multiple then output options as checkboxes.

But I am receiving an undefined variable error inside the if statement for $qandaReplyType. How can this problem be solved?

Code below:

    $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]));

}

?>
1
  • I cant see from any where that you defined $qandaReplyType variable, its usually empty or undefined. Commented Feb 13, 2013 at 21:10

2 Answers 2

3

$qandaReplyType doesn't exist inside the function.

You'll need to include global $qandaReplyType inside the function before you can use the global version of the variable.

Couldn't hurt to brush up on variable scope in PHP.


Edit: By the looks of it, $arrQuestionId and $arrOptionType aren't in the function scope either.
global $qandaReplyType, $arrQuestionId, $arrOptionType;

Sign up to request clarification or add additional context in comments.

Comments

0

Mmmmmmm.....your function....try this:

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>';
 }
 }
 }

If this works, then $qandaReplyType is not defined "inside" the function....you should find a way to recover that value o maybe add another parameter to the function and pass the $qandaReplyType value, like:

function ExpandOptionType($option,$qandaReplyType)

Saludos ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.