0

Before, I managed to save an object inside the database. Now, I need to save each single data in an array in a row. I tried using array_column to save a specific set of data to a column but it saved as a word 'Array' instead. If I implode them, they will just go back to being saved as objects. I am using a built in prepared statement called thingEngineer.

Frontend

<template>
<q-card
            class="exam-section"
            style="width:auto"
            v-for="(tdata, index) in testData"
            :key="index"
          >
            <h5>{{ index + 1}}. {{ tdata.question }}</h5>
            <q-input
              filled
              v-model="tdata.studentAnswer"
              label="Enter Your Answer Here"
              align="center"
            />
          </q-card>
</template>
<script>
export default {
 data() {
    return {
      option: [],
      testData: [],
      token: this.$store.state.token,
      subData: this.$store.state.subData,
      assignData: this.$store.state.assignData,
      testItems: this.$store.state.testItems,
      gotEm: false
    };
  },
methods: {
 submitMyAnswer() {
      console.log(this.testData);
      this.$store
        .dispatch("SAVE_STUDENT_ANSWER", {
          token1: this.assignData.teacher,
          token2: this.token,
          testData: this.testData
          // .testId,
          // studentAnswer: this.testData.studentAnswer
        })
        .then(response => {
          alert("Test Item Has Been Answered!");
          console.log(response.data);
        });
    }
}
}

Backend

  if (isset($_GET['submitId']) && $_GET['submitId'] == 2) {

            $testdata = file_get_contents('php://input');
            $testdecodedData = json_decode($testdata);
            $token1 = $testdecodedData->{'token1'};
            $token2 = $testdecodedData->{'token2'};
            $qna1 = $testdecodedData->{'testData'};
            // $qna2 = $testdecodedData->{'question'}; This was on the frontend 
            // $qna3 = $testdecodedData->{'studentAnswer'}; This was on the frontend 



            $qna2 = (array) $qna1;
            $qna3_val = array_column($qna2, 'question');
            $qna4_val = array_column($qna2, 'studentAnswer');


            $testAnswer = array(array(
                'AccessId' => $token1,
                // 'Question' => implode(", ", array_values($qna2)),
                'StudentAccessId' => $token2,
                // 'TestId' => implode(" || ", array_values($qna2_val)),
                'Question' => $qna3_val,
                'StudentAnswer' => $qna4_val
            ));

            $keys = array("AccessId", "StudentAccessId", "Question", "StudentAnswer");

            $insertTest = $db->insertMulti('answertable', $testAnswer, $keys);

            if ($insertTest) {
                echo json_encode($insertTest);
            }
        }

1 Answer 1

0

array_column() returns an array of values based on column name.

Does $qna1 only have one question and answer attached to it and are the values strings?

If so, you should be able to just do this, unless I'm misunderstanding the question:

$qna3_val = $qna1->question;
$qna4_val = $qna1->studentAnswer;

Otherwise as an array:

$qna3_val = $qna2['question'];
$qna4_val = $qna2['studentAnswer'];

UPDATE: I don't remember one of your deleted comments about the details of the data structure, but for clarification this is the general idea of what I was thinking:

$testAnswers = array();
foreach ($qna1 as $questionData) {
    $testAnswers[] = array(
        'AccessId' => $token1,
        // 'Question' => implode(", ", array_values($qna2)),
        'StudentAccessId' => $token2,
        // 'TestId' => implode(" || ", array_values($qna2_val)),
        'Question' => $questionData->question,
        'StudentAnswer' => $questionData->studentAnswer
    );
}

$keys = array("AccessId", "StudentAccessId", "Question", "StudentAnswer");

$insertTest = $db->insertMulti('answertable', $testAnswers, $keys);
Sign up to request clarification or add additional context in comments.

10 Comments

Use print_r($qna1) and exit() to see what values are set in PHP. It sounds like question and answer are not getting passed in right. Add those vars to the if statement or create an additional if statement to make sure PHP has those variables before moving further in the code.
If it's showing an error that those fields are not available, then I don't think you're getting to the insertMulti().
Cool, hopefully that helps. I'm not familiar with thingEngineer to know what structure it needs to do the insertMulti() call. Check their documentation to see what it needs to look like and use print_r() and exit() to create that structure. Once it looks right, then test the database call again.
Take a look at this answer. The approach depends on the version of PHP you're using.
Nice! It changed the array of objects into a set of array which separated them based on their specific key columns. However, how many I suppose to save them for each row in my database? Is this where the insertMulti comes in?
|

Your Answer

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