【问题标题】:Codeigniter, Multiple queries from controller to viewCodeigniter,从控制器到视图的多个查询
【发布时间】:2019-01-05 13:29:21
【问题描述】:

尝试让我的控制器从两个查询中返回数据,然后将它们显示出来以供查看。第一个查询结果为一条记录,即练习,第二个查询结果为多条记录,即问题。

我的视图既不加载练习的段落,也不加载练习的问题。

运动控制器:

public function exquestdisplay()
{
  $this->load->model('Exercise_model');

  $data['exquest'] = $this->Exercise_model->get_ex_quest();

  $this->load->view('readeralter/header');
  $this->load->view('readeralter/menu_2');
  $this->load->view('readeralter/dashboard_view');
  $this->load->view('readeralter/play_quiz', $data);
  $this->load->view('readeralter/footer');
}

练习模型:

function get_ex_quest(){
//get exercise's paragraphs
$this->db->select("exercise_id, exercise_difficulty, exercise_title, exercise_par1, exercise_par2, exercise_par3, exercise_par4, exercise_par5, exercise_par6");
$this->db->from("exercises");
$this->db->where("exercise_id","1");

$query1 = $this->db->get();

//get exercise's questions
$this->db->select("question_id, question_title, question_choice1, question_choice2, question_choice3, question_answer");
$this->db->from("questions");
$this->db->where("question_exercise_id","1");

$query2 = $this->db->get();

if ($query1->num_rows() < 1 && $query2->num_rows() < 1) {
          echo "There is no data in the database";
          exit();
        }
  elseif($query1->num_rows() == 1 && $query2->num_rows() < 1){
          echo "There is no data in the database";
          exit();
        }
      else {
        $paragraphs = $query1->row();
        $questions = $query2->result();
        return array($paragraphs, $questions);
      }

}

运动视图:

<p><b><?=$row->exercise_id?>.<?=$row->exercise_difficulty?>.<?=$row->exercise_title?></b></p>
<p><?=$row->exercise_par1?></p>
<p><?=$row->exercise_par2?></p>
<p><?=$row->exercise_par3?></p>
<p><?=$row->exercise_par4?></p>
<p><?=$row->exercise_par5?></p>
<p><?=$row->exercise_par6?></p>

<?php } ?>

<?php foreach($exquest['questions'] as $row) { ?>

    <?php $ans_array = array($row->question_choice1, $row->question_choice2, $row->question_choice3, $row->question_answer);?>

    <p><b><?=$row->question_id?>.<?=$row->question_title?></b></p>

    <input type="radio" name="ex_id<?=$row->question_id?>" value="<?=$ans_array[0]?>" required> <?=$ans_array[0]?> &nbsp;
    <input type="radio" name="ex_id<?=$row->question_id?>" value="<?=$ans_array[1]?>"> <?=$ans_array[1]?> &nbsp;
    <input type="radio" name="ex_id<?=$row->question_id?>" value="<?=$ans_array[2]?>"> <?=$ans_array[2]?> &nbsp;
    <input type="radio" name="ex_id<?=$row->question_id?>" value="<?=$ans_array[3]?>"> <?=$ans_array[3]?><br>

<?php } ?>

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    我看到的问题,一旦解决应该会有所帮助:

    select 方法接受以逗号分隔的字段名称字符串,而不是看似数组元素的字符串。

    所以,不要这样做:

    $this->db->select('a','b','c');
    

    你应该这样做:

    $this->db->select('a, b, c');
    

    那么where方法也没有正确使用。它应该接受两个参数。

    那么你的用法是这样的:

    $this->db->where('a = 1');
    

    你应该这样做:

    $this->db->where('a', 1);
    

    您可能还有其他问题,但我看到的另一个问题是,在您看来,您的数据应该作为查询对象$exquest['paragraphs']$exquest['questions'] 可用,但这些不是立即可用的,因为您从不应用-&gt;result() 或@ 987654328@。通常,当您进行查询时,您会想要检查是否有结果,所以回到您的模型中,执行以下操作:

    $query = $this->db->get();
    if( $query->num_rows() > 0 ){
        // you can use the result
        $results = $query->result();
    }
    

    或者,如果您只期望一行:

    $query = $this->db->get();
    if( $query->num_rows() == 1 ){
        // you can use the result
        $row = $query->row();
    }
    

    这些信息应该可以帮助您完成工作。

    【讨论】:

    • 根据您的评论更改了模型,但没有任何反应。我错过了什么?
    【解决方案2】:

    也许你应该在你的模型中试试这个:

    $query1 = $this->db->select('...')->from('...')->where('...')->get();
    $query2 = $this->db->select('...')->from('...')->where('...')->get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-08
      • 1970-01-01
      • 1970-01-01
      • 2017-07-28
      • 1970-01-01
      • 2013-12-06
      相关资源
      最近更新 更多