Reputation: 575
hello everyone I just started working with codeigniter and php. I am making a simple survey type website whit check-boxes the questions are going to be multiple choice and the results will be stored in a database if a check-box is checked. my question is how would I go about of doing that.here is my form and thanks for everyone help in advance.
view
<?php foreach($survay_data as $survay):?>
<ul>
<li><h1><?php echo $survay->Question;?></h1></li>
<li><?php echo $survay->qA; ?><input type="checkbox" name="q1[]" value="qA"></li>
<li><?php echo $survay->qB; ?><input type="checkbox" name="q2[]" value="qB"></li>
<li><?php echo $survay->qC; ?><input type="checkbox" name="q3[]" value="qC"></li>
<?php endforeach; ?>
<input type="textarea" value='a' name="comment">
<br>
<input type="submit" value="Submit">
</ul>
controller
<?php
class Survay extends CI_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);
$query = $this -> db -> get();
if($query -> num_rows() == 1)
{
return $query->result();
}
else
{
return false;
}
}
}
?>
model
<?php
class Survaycontroller extends CI_Controller{
// 'QID, Question, qA, qB, qC'
function index()
{
$arrData = array();
$arrData["qA"] = $this->input->post("qA");
$arrData["qB"] = $this->input->post("qB");
$arrData["qC"] = $this->input->post("qC");
$arrData["Question"] = $this->input->post("Question");
$this->load->model('survay');
$survay_data = $this->survay->dosurvay($arrData);
$viewData['survay_data'] = $survay_data;
$this->load->view('survay_view', $viewData);
}
}
?>
Upvotes: 1
Views: 11881
Reputation: 1179
This should do what you want. Based on our Chat on Stackover, the answer has been provided for you. Your question is not as clear as the request you made on the chat. Find below the codes that can fix your issue
I'm providing this codes just for you to reduce the amount of comments we have going. It's certain you are pretty new to Codeigniter. I can only help as I can.
Step 1: THE DATABASE
Create the database table "tblquestions". fields should be QID, qA, qB and qC. populate the fields with records like up to 43 if you have that much. As little a 5 records should do.
Step 2: THE MODEL
<?php
class Survay extends CI_Model {
function dosurvay($question_id = null) {
$this->db->select('QID, Question, qA, qB, qC');
$this->db->from('tblquestions');
if ($question_id) {
$this->db->where('QID', $question_id);
}
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
return $query->result();
} else {
return false;
}
}
function addsurvay($arrData) {
$this->db->insert('tblanswers', $arrData);
if ($this->db->affected_rows() > 0) {
return $this->db->insert_id();
} else {
return false;
}
}
}
?>
Step 3: THE CONTROLLER
<?php
class Survaycontroller extends CI_Controller {
// 'QID, Question, qA, qB, qC'
function __construct() {
parent::__construct();
$this->load->model('survay');
}
function index() {
//This should select the survey question
$data = array();
$question_id = $this->uri->segment(3);
$data[survay_data] = $this->survay->dosurvay($question_id);
$this->load->view('survay_view', $data);
}
function addanswer() {
//The answer is submitted to this...
$arrData = array();
$userid = null;
if ($this->session->userdata("userid")) {
$userid = $this->session->userdata("userid");
}
if ($this->input->post()) {
$arrData["answerid"] = $this->input->post("QID");
$arrData["questionid"] = $this->input->post("qA");
if ($this->input->post("qA")) {
$arrData["answerA"] = $this->input->post("qA");
}
if ($this->input->post("qB")) {
$arrData["answerB"] = $this->input->post("qB");
}
if ($this->input->post("qC")) {
$arrData["answerC"] = $this->input->post("qC");
}
$arrData["userid"] = $userid;
}
$viewData[survay_data_id] = $this->survay->addsurvay($arrData); //Get the ID of the answer stored
$this->load->view('survay_view', $viewData);
}
}
?>
Step 4: THE VIEW
<?php if(isset($survay_data)) : ?>
<form action="http://localhost/Surva/index.php/survaycontroller/addanswer/" name="myform" id="myform" method="post">
<?php foreach ($survay_data as $survay): ?>
<ul>
<li><h1><?php echo $survay->Question; ?></h1></li>
<li><?php echo $survay->qA; ?><input type="checkbox" name="qA" value="<?php echo $survay->qA; ?>"></li>
<li><?php echo $survay->qB; ?><input type="checkbox" name="qB" value="<?php echo $survay->qA; ?>"></li>
<li><?php echo $survay->qC; ?><input type="checkbox" name="qC" value="<?php echo $survay->qA; ?>"></li>
<li><input type="hidden" name="QID" value="<?php echo $survay->QID; ?>"></li>
<li><input type="submit" name="btn" value="Answer"></li>
</ul>
<?php endforeach; ?>
</form>
<?php endif; ?>
TEST IT:
http://localhost/Surva/index.php/survaycontroller/index/2
to retrieve question number 2
Now this is sure to work. Replace everything you already have. Let me know if these new sets of codes replace the ones I have above more efficiently.
Upvotes: 5
Reputation: 4527
<?php foreach($survay_data as $survay):?>
<ul>
<li><h1><?php echo $survay->Question;?></h1></li>
<li><?php echo $survay->qA; ?><input type="checkbox" name="q1[]" value="<?php echo $survay->qA; ?>"></li>
<li><?php echo $survay->qB; ?><input type="checkbox" name="q2[]" value="<?php echo $survay->qB; ?>"></li>
<li><?php echo $survay->qC; ?><input type="checkbox" name="q3[]" value="<?php echo $survay->qC; ?>"></li>
<?php endforeach; ?>
<input type="textarea" value='a' name="comment">
<br>
<input type="submit" value="Submit">
</ul>
You need to put the data retrieved on the database as the value for the checkbox.. On your controller you can var_dump($this->input->post())
to see what data has been submitted then you can manipulate it as you like.
EXPANDED a LITTLE:
In short you are trying to assign the values from the database to the value attr, which will be sent as the value when a user checks a checkbox.
on the form whether you are using a form_open()
or a standard <form>
tag type in the controller that will receive your data.
function dosurvay($arrData){
// you're get survay things here
if($this->input->post())// will execute only if a post happens
{
echo '<pre>';
print_r($this->input->post()); // just print out everything
echo '</pre>';
}
}
Upvotes: 0