0

我只是 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

</head>

<body>
    <?php form_open('index'); ?>
    <h1><?php echo $Question;?></h1>
    <?php echo $qA; ?><?php form_checkbox('qA'); ?>
    <?php echo $qB; ?><?php form_checkbox('qB');?>
    <?php echo $qC; ?><?php form_checkbox('qC'); ?>

</body>

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;
   }
 }
}
?>
4

2 回答 2

1

在您的控制器中:

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
}
于 2013-02-25T11:47:12.193 回答
0

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

$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);
于 2013-02-25T11:39:20.203 回答