Edvinas Liutvaitis
Edvinas Liutvaitis

Reputation: 575

variable in codeigniter

i am only a beginner at php and codeigniter i am trying to pass data from model in to controller and then in view but i have uninitialized variables how would i initialize them ? here is my code:

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

Upvotes: 0

Views: 147

Answers (2)

Rahul Chipad
Rahul Chipad

Reputation: 2401

In your controller:

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);
    }

And in model:

    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
}

Upvotes: 1

mcryan
mcryan

Reputation: 1576

This is the line that is giving you problems, because you haven't initialized the variables:

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

The variables $Question, $qA, $qB, $qC haven not been passed to the function or initilized anywhere in your code.

You need to define the variables they you are sending to the function:

$question = 'How are you?';
$qA = 'qA';
$qB = 'qB';
$qC = 'qC';

In addition to this, you're passing an array with 1 element to your view: $viewData['survay_data']. This variable contains an array of the variables you are looking for. Rather, just do something like this:

$viewData = $survay_data;

You also need to put your view name in quotes:

$this->load->view('survay_view', $viewData);

Upvotes: 0

Related Questions