0

I have the table "mlw_results" which stores results of quizzes on my WordPress website. I use "Quiz And Survey Master" plugin to create these quizzes. Now I need to display all the results of a particular quiz session (Result Id=7).

Though I get all other fields as required, two Arrays are displayed where I want the real values instead of arrays.

<?php
define( 'SHORTINIT', true ); 
require( 'wp-load.php' );
$id=7;
global $wpdb;
$result_data = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}mlw_results WHERE result_id = {$id}", ARRAY_A);
        $quiz_result = unserialize($result_data['quiz_results']);
        $response_data = array(
            'quiz_id' => $result_data['quiz_id'],
            'quiz_name' => $result_data['quiz_name'],
            'quiz_system' => $result_data['quiz_system'],
            'quiz_payment_id' => '',
            'user_ip' => $result_data['user_ip'],
            'user_name' => $result_data['name'],
            'user_business' => $result_data['business'],
            'user_email' => $result_data['email'],
            'user_phone' => $result_data['phone'],
            'user_id' => $result_data['user'],
            'timer' => 0,
            'time_taken' => $result_data['time_taken'],
            'contact' => $quiz_result['contact'],
            'total_points' => $result_data['point_score'],
            'total_score' => $result_data['correct_score'],
            'total_correct' => $result_data['correct'],
            'total_questions' => $result_data['total'],
            'question_answers_array' => $quiz_result[1],
            'comments' => ''
        );
        foreach($response_data as $value=> $quiz_result[1]){
        echo $quiz_result[1]."<br>";}
?>

The result I get is:

1
New Quiz
0

137.97.167.96
John
Webmaster
me@mypersonalmail.com
998121
1
0

02:09:32 PM 04/08/2020
Array

1
50
1
2

Array

I would like to replace those arrays with real values. Is it possible? The data stored in the column "quiz_results" is as follows:

a:5:{i:0;i:89;i:1;a:2:{i:0;a:9:{i:0;s:17:"Capital of Kerala";i:1;s:9:"Trivandum";i:2;s:9:"Trivandum";i:3;s:0:"";s:7:"correct";s:7:"correct";s:2:"id";s:1:"1";s:6:"points";d:1;s:8:"category";s:0:"";s:13:"question_type";s:1:"0";}i:1;a:9:{i:0;s:20:"Capital of Karnataka";i:1;s:13:"None of these";i:2;s:0:"";i:3;s:0:"";s:7:"correct";s:9:"incorrect";s:2:"id";s:1:"2";s:6:"points";d:0;s:8:"category";s:0:"";s:13:"question_type";s:1:"0";}}i:2;s:0:"";s:7:"contact";a:4:{i:0;a:3:{s:5:"label";s:9:"Your Name";s:5:"value";s:4:"John";s:3:"use";s:4:"name";}i:1;a:3:{s:5:"label";s:10:"Your Email";s:5:"value";s:21:"me@mypersonalmail.com";s:3:"use";s:5:"email";}i:2;a:3:{s:5:"label";s:13:"Your Business";s:5:"value";s:9:"Webmaster";s:3:"use";s:4:"comp";}i:3;a:3:{s:5:"label";s:10:"Your Phone";s:5:"value";s:6:"998121";s:3:"use";s:5:"phone";}}s:8:"timer_ms";i:89634;}

1 Answer 1

0

I would like to replace those arrays with real values. Is it possible?

I am assuming you want to access and print those two variables which is shown as "Array" in the print out (contact, question_answers_array)

Solution as follows - replace your foreach look with below (I didn't test code but should give you an idea):

foreach($response_data as $key=> $value){
    if($key=='contact'){
        echo 'contact array:<br>';
        $value = unserialize($value); //turn serialized string into array
        print_r($value)."<br>"; //prints out the array all at once for debug
        //if you want to loop through
        foreach($value as $contactKey=>$contactValue){
                echo $contactKey."<br>";
                echo $contactValue['label']."<br>";
                echo $contactValue['value']."<br>";
                echo $contactValue['use']."<br>";
        }
    }elseif($key=='question_answers_array'){
        echo 'question_answers_array:<br>';
        $value = unserialize($value);
        print_r($value)."<br>"; 
        //if you want to loop through
        foreach($value as $qKey=>$qValue){
                echo $qKey."<br>";
                echo $qValue['0']."<br>";
                echo $qValue['1']."<br>";
                echo $qValue['2']."<br>";
                echo $qValue['3']."<br>";
                echo $qValue['correct']."<br>";
                //so on....
        }
    }else{
        echo $value."<br>";
    }
}

Give it a try; may have to fix some syntax / code issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.