CodeIgniter Forums

Full Version: Need guidance on displaying data
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]ede196620[/eluser]
So i have this application where i have a list of questions that have to be selected and then answered the questions are not related to each other they are individual displayed. What i have is a
Code:
anchor();
links that display question that are generated from the database and when the link is clicked it should take the user to the question that he will answer.

currently i have this view
Code:
<div>  
           <h1>  &lt;?php
         foreach($query as $row){
            echo anchor('survaycontroller', $row->Question);
            echo '<br>';
            
         }
         ?&gt;</h1>
       </div>
and this controller for the questions
Code:
&lt;?php

class Survaycontroller extends CI_Controller {

  
    function __construct() {
        parent::__construct();
        $this->load->model('survay');
    }

    function index() {
  
        $data = array();
        $question_id = $this->uri->segment(3);
        $data['survay_data'] = $this->survay->dosurvay($question_id);
        $this->load->view('survay_view', $data);
    }
    
    

    function addanswer() {
      
        $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["comment"] = $this->input->post("comment");
            
            $arrData["userid"] = $userid;
        }
        $viewData['survay_data_id'] = $this->survay->addsurvay($arrData);
        $this->load->view('survay_view', $viewData);
        
        redirect('end');
  
    }

}
?&gt;
this is the part where i am stuck i cant figure out how to display the question with optional answers with the corresponding anchor link. I tried to research this but could not find anything on this topic.
o and here is the view for the question section:
Code:
&lt;body&gt;
      
&lt;?php if(isset($survay_data)) : ?&gt;
&lt;form acti name="myform" id="myform" method="post"&gt;
   &lt;?php foreach ($survay_data as $survay): ?&gt;
        <ul>
            <li><h1>&lt;?php echo $survay->Question; ?&gt;</h1></li>
            <li>&lt;?php echo $survay->qA; ?&gt;&lt;input type="checkbox" name="qA" value="&lt;?php echo $survay-&gt;qA; ?&gt;"&gt;&lt;/li>
            <li>&lt;?php echo $survay->qB; ?&gt;&lt;input type="checkbox" name="qB" value="&lt;?php echo $survay-&gt;qB; ?&gt;"&gt;&lt;/li>
            <li>&lt;?php echo $survay->qC; ?&gt;&lt;input type="checkbox" name="qC" value="&lt;?php echo $survay-&gt;qC; ?&gt;"&gt;&lt;/li>
            Comment:
            <li>&lt;?php echo form_textarea('comment')?&gt; </li>        
            <li>&lt;input type="hidden" name="QID" value="&lt;?php echo $survay-&gt;QID; ?&gt;"&gt;&lt;/li>
            <li>&lt;input type="submit" name="btn" value="Answer"&gt;&lt;/li>
        </ul>
    &lt;?php endforeach; ?&gt;
    
&lt;/form&gt;
      
&lt;?php endif; ?&gt;
    &lt;/body&gt;
I am still very new to codeigniter and cant figure this out any help would be greatly appreciated.
Would the solution be similar to this example :
Code:
function view($id){
  // set common properties
  $data['title'] = 'Question Details';
  $data['link_back'] = anchor('display/index/','List Of Questions',array('class'=>'back'));

  // get Question details
  $data['query'] = $this->display_model->get_by_id($id)->row();
            
              
                
  // load view
  $this->load->view('viewquestion', $data);
}
tnx for the guidance.

El Forum

[eluser]TheFuzzy0ne[/eluser]
Sorry, but I'm confused. Why is each question a link? In all the surveys I've ever done, there's anything from one question to a few questions per page. Once you answer them, you are redirected onto the next page.

Also, how does your controller know which question has been selected? I can't see where you pass the question ID to your controller.

El Forum

[eluser]ede196620[/eluser]
Its jest the way i want them to be displayed its the design i chose because i want the questions to be separate and only one question will be displayed on the page.

This is what i ma trying to figure out how to select the questions to be displayed.

El Forum

[eluser]TheFuzzy0ne[/eluser]
You'll need to pass the ID of the questions into the view, and use them in the links.

El Forum

[eluser]ede196620[/eluser]
can you code an example for me to use? i am still very confused by this. tnx for posting