user2056342
user2056342

Reputation: 305

undefined variable in an if statement

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

}

?>

Upvotes: 1

Views: 1862

Answers (2)

Hackerman
Hackerman

Reputation: 12305

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

Upvotes: 0

Mr. Llama
Mr. Llama

Reputation: 20889

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

Upvotes: 3

Related Questions