php中构造方法的返回数组[duplicate]

nwlqm0z1  于 2022-12-10  发布在  PHP
关注(0)|答案(2)|浏览(128)

此问题在此处已有答案

Constructor returning value?(6个答案)
Returning a value in constructor function of a class(8个答案)
What is the use of return $this in php constructors?(2个答案)
php OOP objects/classed return values(2个答案)
PHP Get Returned Value From Constructor(1个答案)
5天前关闭。
我正在尝试创建一个主页,我将在其中输出问题及其答案
我有一个有3个答案的问题,但是当我创建对象时,它只返回1个答案,而我需要它来返回答案数组。我需要创建额外的类答案来完成这个任务吗?
我的代码:

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;
        } 
    }

我试过更改为查询并通过answerID检索数据,但我得到了同样的问题3次。有人能帮助解决这个问题吗?

5f0d552i

5f0d552i1#

您可以将创建新问题示例与向现有问题添加新答案分开,如下所示:

$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

nfs0ujit

nfs0ujit2#

只需使用单独的queries为问题和答案。
如果你想使用group by answerID,当然它会返回同一个问题有多个答案的结果。mysql返回的结果是平面的。只需将值保存在数组中
$array[questionID]['answer'][] = $var['answer'];
然后通过循环遍历每个questionID来构建类对象。

相关问题