Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 可变点火器_Php_Codeigniter_Controller - Fatal编程技术网

Php 可变点火器

Php 可变点火器,php,codeigniter,controller,Php,Codeigniter,Controller,我只是php和codeigniter的初学者,我试图将数据从模型传递到控制器,然后在视图中传递,但我有未初始化的变量,我该如何初始化它们? 这是我的密码: survaycontroller.php <?php class Survaycontroller extends CI_Controller{ // 'QID, Question, qA, qB, qC' function index() { $this->load->

我只是php和codeigniter的初学者,我试图将数据从模型传递到控制器,然后在视图中传递,但我有未初始化的变量,我该如何初始化它们? 这是我的密码:

survaycontroller.php

    <?php
class Survaycontroller extends CI_Controller{
   // 'QID, Question, qA, qB, qC'

    function index()
    {
            $this->load->view('survay_view');

            $this->load->model('survay');
            $survay_data = $this->survay->dosurvay($Question, $qA, $qB, $qC);

            $viewData = array();
            $viewData['survay_data'] = $survay_data;
            $this->load->view(survay_view, $viewData);   
        }
}
?>

survay_view.php


survay.php

  <?php
class Survay extends CI_Model{

    function dosurvay($Question, $qA, $qB, $qC){
        $this->db->select('QID, Question, qA, qB, qC');
        $this->db->from('tblquestions');
        $this->db->where('Question', $Question);
        $this->db->where('qA', $qA);
        $this->db->where('qB', $qB);
        $this->db->where('qC', $qC);
         $this -> db -> limit(1);

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

   if($query -> num_rows() == 1)
   {
     return $query->result();
   }
   else
   {
     return false;
   }
 }
}
?>

这是给您带来问题的一行,因为您尚未初始化变量:

$survay_data = $this->survay->dosurvay($Question, $qA, $qB, $qC);
变量
$Question、$qA、$qB、$qC
尚未传递给函数,也未初始化代码中的任何地方

您需要定义要发送给函数的变量:

$question = 'How are you?';
$qA = 'qA';
$qB = 'qB';
$qC = 'qC';
除此之外,还将向视图传递一个包含1个元素的数组:
$viewData['survay_data']
。此变量包含要查找的变量数组。相反,只要做这样的事情:

$viewData = $survay_data;
您还需要将视图名称加引号:

$this->load->view('survay_view', $viewData);
在控制器中:

function index()
    {
        //store your post form data into an array 
         $arrData = array();
         $arrData["qA"]     = $this->input->post("qAfieldName");
         $arrData["qB"]     = $this->input->post("qBfieldName");
         $arrData["qC"]     = $this->input->post("qCfieldName");
         $arrData["question"]     =  $this->input->post("questionfieldName");
         $this->load->model('survay');
         //pass data array into model          
         $survay_data = $this->survay->dosurvay($arrData);
    }
在模型中:

    function dosurvay($arrData){        
        $this->db->select('QID, Question, qA, qB, qC');
        $this->db->from('tblquestions');
        $this->db->where('Question', arrData['Question']);
        $this->db->where('qA', $arrData['qA']);
        $this->db->where('qB', $arrData['qB']);
        $this->db->where('qC', $arrData['qC']);
        $this -> db -> limit(1);

............other stuff
}

我在控制器中定义了变量,但我认为这些变量尚未初始化。你能为此提出建议吗?tnx发布你的答案,你能评论一下吗?这样我以后才能更好地理解:)
    function dosurvay($arrData){        
        $this->db->select('QID, Question, qA, qB, qC');
        $this->db->from('tblquestions');
        $this->db->where('Question', arrData['Question']);
        $this->db->where('qA', $arrData['qA']);
        $this->db->where('qB', $arrData['qB']);
        $this->db->where('qC', $arrData['qC']);
        $this -> db -> limit(1);

............other stuff
}