0

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;
 }
 }
}
?>
mcryan
1,57610 silver badges20 bronze badges
asked Feb 25, 2013 at 11:35

2 Answers 2

1

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
}
answered Feb 25, 2013 at 11:47
Sign up to request clarification or add additional context in comments.

1 Comment

tnx for posting can u comment on your answer so i could have a better understanding in the future :)
0

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);
answered Feb 25, 2013 at 11:39

1 Comment

i defined the variables in the controller but the variables in my view are still uninitialized can u suggest something for this ?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.