Home > database >  Return array in construct method in php
Return array in construct method in php

Time:12-04

I am trying to create a homepage where I will output question with its answers

I have a question which has 3 answers, but when I create the object it only return 1 answer, whereas I need it to return the array of answers. Do I need to create additional class answers in order to do that?

My code:

    include("connect-database.inc.php");

    $question_query = "SELECT
        questions.questionID,
        answers.answer,
        questions.question,
        questions.feedback,
        questions.mark,
        questions.questionTypeID 
    FROM questions 
    JOIN answers ON questions.questionID=answers.questionID";
    
    $questionList=array();
    $answerList = array();
    try {
        $mysqliResult = $link->query($question_query);
        while($var=$mysqliResult->fetch_assoc()){
            $questionList[$var['questionID']]=new questions($var['question'],$var['feedback'], $var['mark'], $var['questionTypeID'], $var['answer']);
        }
    } catch (Exception $e) { 
        echo "MySQLi Error Code: " . $e->getCode() . "<br />";
        echo "Exception Msg: " . $e->getMessage();
        exit();
    }   
    var_dump($questionList);


    class questions {

        public function __construct($question, $feedback, $mark, $questionTypeID, $answerList){
            $this->question = $question;
            $this->feedback = $feedback;
            $this->mark = $mark;
            $this->questionTypeID = $questionTypeID;
            $this->answers($answerList);
        }

        public function answers($answers) {
            $answers = array();
            $this->answers = $answers;
        } 
    }

I have tried to change to query and retrieve data by answerID, but then I get the same question 3 times. Can anybody help with the solution?

CodePudding user response:

You can separate new Question instance creating from add new answers to existing Question like:

$question_query = "SELECT
        questions.questionID,
        answers.answer,
        questions.question,
        questions.feedback,
        questions.mark,
        questions.questionTypeID 
    FROM questions 
    JOIN answers ON questions.questionID=answers.questionID";
    
    $questionList=array();
    $answerList = array();
    try {
        $mysqliResult = $link->query($question_query);
        while($var=$mysqliResult->fetch_assoc()){
            if (!isset($questionList[$var['questionID']])) {
                $questionList[$var['questionID']]= new Question(
                    $var['question'],
                    $var['feedback'], 
                    $var['mark'], 
                    $var['questionTypeID']
                );
            }
            $questionList[$var['questionID']]->addAnswer($var['answer']);
        }
    } catch (Exception $e) { 
        echo "MySQLi Error Code: " . $e->getCode() . "<br />";
        echo "Exception Msg: " . $e->getMessage();
        exit();
    }   
    var_dump($questionList);


    class Question {

        public function __construct($question, $feedback, $mark, $questionTypeID, $answerList = []){
            $this->question = $question;
            $this->feedback = $feedback;
            $this->mark = $mark;
            $this->questionTypeID = $questionTypeID;
            $this->setAnswers($answerList);
        }
        
        public function addAnswer($answer) {
            $this->answers[] = $answer;
        }
        
        public function setAnswers($answers) {
            $this->answers = $answers;
        } 
    }

PHPize - online PHP environment

CodePudding user response:

Just use seperate quieries for both question and answer.

If you want to use group by answerID, of course it will return result with multiple answer with same question. mysql return result as flat. Just save the value inside an array as such

$array[questionID]['answer'][] = $var['answer'];

Then you build the class object by looping through each questionID.

  • Related