开发者

Arrays operiation

How to move in order to fit the answers to questions in array or how to make sql query for the desired effect? I tired of this alredy a few days.

<?php
    public function getQuestions()
    {
        $sql = 'SELECT * FROM questions';
        $result = Database::query($sql);
        while ($row = mysql_fetch_assoc($result)) {
            $questions[] = $row;
        }
        return $questions;
    }

    public function getAnswers($qid)
    {
        $sql = 'SELECT idq, answer, truth FROM answers WHERE idq = '.$qid;
        $result = Database::query($sql);
        while ($row = mysql_fetch_assoc($result)) 
        {
            $answers[] = $row;
        }   
        return $answers;
    }

    public static function array_add($a1, $a2) 
    {
        $aRes = $a1;
        foreach (array_slice(func_get_args(), 1) as $aRay) 
        {
            foreach (array_intersect_key($aRay, $aRes) as $key => $val) $aRes[$key] += $val;
            $aRes += $aRay;
        }
        return $aRes; 
    }


    public static function getQuiz()
    {
        $db = new Database();
        self::$questions = $db->getQuestions();
        foreach( self::$questions as $q )
        {
            $qid = $q['qid'];

            self::$answers[$qid] = $db->getAnswers($qid);
        }

        $s = Quiz::array_add(self::$questions, self::$answers);
        print "<pre>";
        print_r($s);
        print "</pre>";
    }
?>

output:

Array
(
    [0] => Array
        (
            [qid] => 1
            [question] => Bunty szlachty pod has?ami obrony praw nazywamy?
        )

    [1] => Array
        (
            [qid] => 2
            [question] => Kto dowodzi? wojskami kozackimi podczas powstania Chmielnickiego?
            [0] => Array
                (
                    [idq] => 1
                    [answer] => liberum veto
                    [truth] => 1
                )

            [1] => Array
                (
                    [idq] => 1
                    [answer] => jurydyki
                    [truth] => 0
                )

            [2] => Array
                (
                    [idq] => 1
                    [answer] => rokosze
                    [truth] => 0
                )

        )

    [2] => Array
        (
            [qid] => 3
            [question] => W którym roku odby?a si?, morska, bitwa pod Oliw??
            [0] => Array
                (
                    [idq] => 2
                    [answer] => Bohdan Chmielnicki
                    [truth] => 1
                )

            [1] => Array
                (
                    [idq] => 2
                    [answer] => Gustaw II Adolf
                    [truth] => 0
                )

            [2] => Array
                (
                    [idq] => 2
                    [answer] => Iwan IV Gro?ny
                    [truth] => 0
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [idq] => 3
                    [answer] => 1627
                    [truth] => 1
                )

            [1] => Array
                (
                    [idq] => 3
                    [answer] => 1608
                    [truth] => 0
                )

            [2] => Array
                (
                    [idq] => 3
                    [answer] => 1654
                    [truth] => 0
                )

        )

)开发者_StackOverflow

I tried this solution yet but I need to have answers separately:

function get_quiz()
{
    $sql = 'SELECT question, GROUP_CONCAT(answer ORDER BY answer SEPARAtoR " ") as answers
            FROM questions 
            LEFT JOIN answers ON (questions.qid=answers.idq) 
            GROUP BY answers.idq;';
    $result = mysql_query($sql);
    while ($row = mysql_fetch_assoc($result)) 
    {
        $quiz[] = $row;
    }   
    return $quiz;
}

output:

Array
(
    [0] => Array
        (
            [question] => Bunty szlachty pod has?ami obrony praw nazywamy?
            [answers] => jurydyki liberum veto rokosze
        )

    [1] => Array
        (
            [question] => Kto dowodzi? wojskami kozackimi podczas powstania Chmielnickiego?
            [answers] => Bohdan Chmielnicki Gustaw II Adolf Iwan IV Gro?ny
        )

    [2] => Array
        (
            [question] => W którym roku odby?a si?, morska, bitwa pod Oliw??
            [answers] => 1608 1627 1654
        )

)


The problem is this line:

        self::$answers[$qid] = $db->getAnswers($qid);

The question with ID "1" is being put into the 0th position of the array so all of the answers will end up off by 1.

One potential fix is to change:

    foreach( self::$questions as $q )
    {
        $qid = $q['qid'];
        self::$answers[$qid] = $db->getAnswers($qid);
    }

to:

    foreach( self::$questions as $idx=>$q )
    {
        $qid = $q['qid'];
        self::$answers[$idx] = $db->getAnswers($qid);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜