Php 如何在模板内的模板中从数据库中提取数据?[F3]

Php 如何在模板内的模板中从数据库中提取数据?[F3],php,fat-free-framework,Php,Fat Free Framework,我正在F3中构建一个简单的测验应用程序 每个测验都有很多问题。每个问题都有许多(可能不同的)选项。我已经正确地设置了MySQL数据库和表:(1)测验表,(2)问题表,(3)问题选项表,(4)。。。和其他一些记录用户响应等 我不知道如何使用F3结构从更广泛的问题模板中正确调用每个问题的选项。以下是模型的外观: 这是git项目: php:(目前我已经为测试目的硬编码了quiz1.html链接) class QuizController extends Controller { functi

我正在F3中构建一个简单的测验应用程序

每个测验都有很多问题。每个问题都有许多(可能不同的)选项。我已经正确地设置了MySQL数据库和表:(1)测验表,(2)问题表,(3)问题选项表,(4)。。。和其他一些记录用户响应等

我不知道如何使用F3结构从更广泛的问题模板中正确调用每个问题的选项。以下是模型的外观:

这是git项目:

php:(目前我已经为测试目的硬编码了quiz1.html链接)
class QuizController extends Controller {

    function beforeroute() {
        // session management if required
        // render header
        echo \Template::instance()->render('header.html');
    }

    function render($f3){
        $quizzes = new Quizzes($this->db);
        $quiz = $quizzes->getById(1)[0];
        $f3->set('quiz',$quiz);

        $thisquiz = new Questions($this->db);
        $questions = $thisquiz->getByQuizId(1);
        $f3->set('questions',$questions);

        echo \Template::instance()->render('quiz1.html');
    }
}
这是quiz1.html中调用选项的部分:

<repeat group="{{ @questions }}" key="{{ @count }}" value="{{ @question }}">
     <div class="row">
        <div class="col-md-6">
           <h5>{{ @count + 1 }}. {{ @question.text }}</h5> 
        </div>
        <div class="col-md-6">
           <select class="form-control bg-silver"> 
              <include href="options.html" with="id={{@question.id}}" />                                           
           </select>                     
        </div>
     </div>    
</repeat>

{{@count+1}}。{{@question.text}
这就是我被困的地方:我想实现一个类似“QuestionOptions控制器”的东西,在这里我使用getByQuestionId($question.id),从QuestionOptions中提取所有数据,并用数据库填充select。所以本质上,我希望选项看起来像这样(但这显然是错误的,因为我现在无法提取正确的数据)

My desired options.html,将数据从表中拉入@options:

<repeat group="{{ @questions }}" key="{{ @count }}" value="{{ @question }}">
     <div class="row">
        <div class="col-md-6">
           <h5>{{ @count + 1 }}. {{ @question.text }}</h5> 
        </div>
        <div class="col-md-6">
           <select class="form-control bg-silver"> 
              <include href="options.html" with="id={{@question.id}}" />                                           
           </select>                     
        </div>
     </div>    
</repeat>
<repeat group="{{@options}}" value="{{@option}}">
   <option>{{@option.text }}</option>
</repeat>

{{@option.text}

如何将数据正确地拉入@options?我应该使用QuizController将所有相关的QuestionOptions表数据拉入视图,然后在视图中进行排序(我觉得很混乱),还是有更好的方法?

您可以创建一个
QuestionsOptions
模型,并在
问题
模型中实现一个
getOptions()
方法

class Questions extends \DB\SQL\Mapper {

  function getOptions() {
    $options=new QuestionsOptions($this->db);
    return $options->find(['question=?',$this->id],['order'=>'weight']);
    // NB: field names to be adjusted
  }

  ...

  function __construct(\DB\SQL $db) {
    parent::__construct($db,'Questions_table');
  }

}
然后在
quiz1.html
模板中:

<select class="form-control bg-silver">
  <repeat group="@question.getOptions()" value="@option">
    <option>{{ @option.text | esc }}</option>
  </repeat>
</select>
您的观点如下所示:

function render($f3){
    $quizzes = new Quizzes($this->db);
    $quiz = $quizzes->getById(1)[0];
    $f3->set('quiz',$quiz->cast());

    $thisquiz = new Questions($this->db);
    $questions = [];
    foreach ($thisquiz->getByQuizId(1) as $question) {
      $options = [];
      foreach ($question->getOptions() as $option)
        $options[] = $option->cast();
      $questions[] = $question->cast()+['options => $options];
    }
    $f3->set('questions',$questions);

    echo \Template::instance()->render('quiz1.html');
}
<select class="form-control bg-silver">
  <repeat group="@question.options" value="@option">
    <option>{{ @option.text }}</option>
  </repeat>
</select>

{{@option.text}

如您所见,控制器代码现在看起来有点复杂。但是现在您的视图只处理数组和HTML编码的字符串。

谢谢@xfra35!我想过要这么做。不过,这是否会在视图中混合一些控制器逻辑?如果我离开纯“MVC”风格,有什么问题吗?我担心这会造成“混乱”的编码。我在我的问题模型中创建了这个函数:公共函数getOptions(){return(new QuestionOptions)->getByQuestionId($this->id);},我在我的问题选项模型中创建了这个函数:公共函数getByQuestionId($id){$this->load(数组('question_id_fk=?',$id));return$this->query;}我得到这个错误:内部服务器错误:未定义字段getOptions我想我不允许从视图内部调用Questions.php模型中的函数?@gauravkeerthi我已经更新了关于“这是否会在视图中混合一些控制器逻辑?”的答案。关于这个错误“未定义字段getOptions”,可能是因为您忘记了括号
@question.getOptions()
。谢谢!这很有教育意义,有助于我更好地遵守MVC。我将其放在问题模型中:
公共函数getChoices(){return(new Choices)->find(['question\u id\u fk=?,$This->id])
我得到了这个错误:
参数1传递给选项::\uu construct()必须是DB\SQL的一个实例,没有给定,在第32行的/Users/Gaurav/Sites/php/reallymh/app/models/Questions.php中调用,并且定义了
选项是我创建的模型的新名称,您称之为QuestionOptions