简体   繁体   中英

PDO PHP Array issue

function chkQuestions($info)
    {
        $query = $this->handle->prepare("SELECT * FROM tbltest");
        $query->execute();
        $rows = $query->fetchAll();
        $getPost = $query->rowCount();  

        foreach($rows as $row) {
            for($i = 1; $i <= $getPost; $i++) {
                $answerID[$i] = array($info['answer'.$i]);
                $lo = array($answerID);
            }

            foreach($answerID as $question) {
                if(array_key_exists($row['answer'],$question)) {
                    $test = "found";
                    return $test;
                } else {
                    return $question;
                }
            }                           
        }   
    }//chkQuestions

Above is my function, all works, but when I compare the row in my database called answer with the array it doesn't work?

I tried hard coding in the value and it works correctly but not with the array.

I checked to see the values of array and the value is indeed there.

In logic $answerID[$i] = array($info['answer'.$i]); = option1 and if the array value option1 = the database row answer which is set to option1 than return found, but it tells me it cannot find it.

array(1)
    {
        [0]=> string(7) "option1"
    }

Above is the dum_var of the array $question . Any help?

foreach($questions as $question)
            {
            ?>
            <h3><?php echo $question['question'] ?></h3>

            <div class = "col-xs-12">

            <input type="radio" name="answer<?php echo $question['testID'] ?>" id="answer<?php echo $question['testID'] ?>" value="<?php echo $question['option1']?>" />

            <label for="question-1-answers-A">A)<?php echo $question['option1'] ?> </label>

            </div>

            <div class = "col-xs-12">

            <input type="radio" name="answer<?php echo $question['testID'] ?>" id="answer<?php echo $question['testID'] ?>" value="<?php echo $question['option2']?>" />

            <label for="question-1-answers-B">B)<?php echo $question['option2'] ?></label>

            </div>

            <div class = "col-xs-12">

            <input type="radio" name="answer<?php echo $question['testID'] ?>" id="answer<?php echo $question['testID'] ?>" value="<?php echo $question['option3']?>" />

            <label for="question-1-answers-C">C)<?php echo $question['option3'] ?></label>

            </div>

            <div class = "col-xs-12">

            <input type="radio" name="answer<?php echo $question['testID'] ?>" id="answer<?php echo $question['testID'] ?>" value="<?php echo $question['option4']?>" />

            <label for="question-1-answers-D">D)<?php echo $question['option4'] ?></label>
            <br>
            <br>


            </div>


            <?php
            }
                echo "<input type='hidden' name='subaction' id='subaction' value='chkQuestion'> <button class='btn btn-primary' name='submit' type='submit'>Submit</button> </form>";

        }
        else
            echo "No Quiz Found </form>";










    ?>   
   </div>    

I don't have a lot of confidence in my answer because your coding method does a good job of confusing me. However, I feel like I need to get something on the page so we have some sort of solid base to work from:

function chkQuestions($info){
    $check=array(); // this will hold the results to return
    $stmt=$this->handle->prepare("SELECT `testID`,`answer` FROM tbltest");
    $stmt->execute(); 
    while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
        if($info['answer'][$row['testID']]==$row['answer']){
            $check[$row['testID']]="correct";
        }else{
            $check[$row['testID']]="incorrect";
        }
    }
    return $check;  // handle this however you like on your main page
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM