0

我从一个帖子中看到了这个答案,该帖子旨在发布来自特定 set_id 的数据库数据及其以下问题和答案。我发现很难将它转换为codeigniter的models-controllers-views原则。我需要一些帮助

它有 3 个表并提出了以下代码:

<?php
$this->db->select('s.id as set, s.name as name, q.id as qid, q.question as qu, a.id as aid, a.answer as an, a.points as p')
     ->from('sets s')
     ->join('questions q', 'q.set_id = s.id')
     ->join('answers a', 's.set_id = s.id')
     ->where('s.id', 'SET ID');

$questions = $this->db->get();
$set = array('questions' => array());

foreach($questions as $s){
  $set['id'] = $s->set;
  $set['name'] = $s->name;
  $set['questions'][$s->qid]['id'] = $q->qid;
  $set['questions'][$s->qid]['question'] = $q->qu;
  if(!isset($set['questions'][$s->qid]['answers']))
    $set['questions'][$s->qid]['answers'] = array();
  $set['questions'][$s->qid]['answers'][] = array(
    'id' => $q->aid,
    'answer' => $q->an',
    'points' => $q->p
  );
}

echo '<h2>'.$set['name'].'</h2>';
foreach($set['questions'] as $q){
  echo '<div class="question">';
  echo '<h3>'.$q['question'].'</h3>';
  echo '<div class="answers">';
  foreach($q['answers'] as $a){
    echo '<label for="a'.$a['id'].'">'.$a['answer'].'<input type="checkbox         value="'.$a['id'].'" name="q'.$q['id'].'" /></label><br />';
  }
  echo '</div>';
  echo '</div>';
}
4

2 回答 2

0

In this instruction there is a problem

$set = $questions->result_array();
于 2012-04-26T09:20:23.500 回答
0

一旦你习惯了 MVC 模式,它就很简单。模型组件基本上处理数据库查询。您可以为不同的查询编写简单的函数并将它们放在模型类下。Controller 基本上是拉取模型数据并提交给 View 组件。在控制器内部,您将定义如何以及在何处将数据放入不同的视图类的逻辑。以下链接是查看的好地方。

http://kyokasuigetsu25.wordpress.com/2012/01/06/codeigniter-beginner-tutorial-3-playing-with-the-database/

于 2012-04-26T08:36:28.443 回答