0

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?

6
  • 1
    What is the goal of $answers = array(); inside answers() method? You overrides the given parameter, to store an empty array. Commented Dec 4, 2022 at 13:32
  • you are doing inner join that's why the number of data is repeating. first try to understand the difference between inner join and left join Commented Dec 4, 2022 at 13:33
  • What's the purpose of that useless try-catch? Did you know that this is a very bad practice as this exposes sensitive information to your users? Commented Dec 4, 2022 at 13:34
  • 1
    By using $questionList[$var['questionID']] = new, you override previous results with the key $var['questionID']. Commented Dec 4, 2022 at 13:34
  • 1
    Perhaps you wanted to do $questionList[$var['questionID']][] = new questions Commented Dec 4, 2022 at 13:35

2 Answers 2

1

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 = [];
    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) { 
        // debug only:
        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) {
            $this->question = $question;
            $this->feedback = $feedback;
            $this->mark = $mark;
            $this->questionTypeID = $questionTypeID;
        }
        
        public function addAnswer($answer) {
            $this->answers[] = $answer;
        }
    }

PHPize - online PHP environment

Sign up to request clarification or add additional context in comments.

Comments

0

Just use separate queries 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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.