0

我在yii工作。我的控制器功能为-

public function actiongetPublicQuestion()
{
    $model=new Qbquestion();
    $new = Yii::app()->db->createCommand()->select('max(tranId)')->from('qbpublicuserquestion')->queryScalar();
    $CurrentQuestionId=Qbpublicuserquestion::model()->findByPk($new);
    $currentQuestion=Qbquestion::model()->findByPk($CurrentQuestionId->questionId);
    echo "Currently displayed question is=".$currentQuestion->question."</Br>";
//  $answer=Qbquestionoption::model()->findAllByAttributes(array('questionId'=>$currentQuestion1->questionId));



    $old = Yii::app()->db->createCommand()->select('max(tranId-1)')->from('qbpublicuserquestion')->queryScalar();
    $PreviousQuestionId=Qbpublicuserquestion::model()->findByPk($old);
    $currentQuestion1=Qbquestion::model()->findByPk($PreviousQuestionId->questionId);
    echo "previously displayed question is=".$currentQuestion1->question;
    $answer=Qbquestionoption::model()->findAllByAttributes(array('questionId'=>$currentQuestion1->questionId));

    foreach($answer as $ans)
    {
    //  var_dump($ans);

        if($ans->isAnswer==1)
        {
            echo "</br>"."correct Answer is=".$ans->option."with optionId=".$ans->optionId;
            $true=$ans->option;
            echo "</br>";
            echo "{'questions':[".CJSON::encode(array("question"=>$currentQuestion1->question,"answer"=>$true,"question1"=>$currentQuestion->question))."] }";
        }
    }
}

所以我从数据库中检索两个问题,这样表的最后一个条目是当前显示的问题,而倒数第二个条目是以前显示的问题。我也想发布以前显示的问题的答案。我以 json 编码格式发送此信息。我想以格式输出=

"Questions": [
    {"question": "Who is the prime minister of india?","answer":"Manmohan sinh"},
    {"question": "2 when who will win the test?"},
]

但是当我在 CJSON::encode(array("question"=>$currentQuestion1->question,"answer"=>$true,"question"=>$currentQuestion->question))."] 中使用相同的变量时";},如果我们在编码的 cjson 数组中使用相同的变量,它只给出一个问题。那么如何获得所需的输出。

4

1 回答 1

3

由于您想要的输出有两个 JSON 对象(即大括号中的两个项目),因此您传递给 CJSON::encode 的数据结构需要是对象或数组的数组,即:

CJSON::encode(array(array("question"=>$currentQuestion1->question,"answer"=>$true), 
                    array("question"=>$currentQuestion->question),
                    )
            );
于 2013-01-01T01:15:15.773 回答