Search code examples
phpcodeignitercodeigniter-3codeigniter-query-builder

Fetching the data from the database in PHP using loop


I want to show the questions and their respective answers in the view from the database:

I have got the database table named Feedback in the following form:

This is my Table Name: Feedback

id | employee_id | question_id |   answer            |
______________________________________________________

1  |   100       |   1         | That was awesome. 
2  |   100       |   2         | That was excellent.  
3  |   100       |   3         | That was good.  
4  |   101       |   1         | That was better.  
5  |   101       |   2         | That was interesting.  
6  |   101       |   3         | That was fine.

And another table named with Questions:

id |      Question        |
_______________________________________________

1  | How was your day? 
2  | How was the task?
3  | How was the Project?

Here is my model's code :

function get_answers_supervisee () {
    $userid = $this->session->userdata('userid');
    $result = array ();
    $sql = "select answer from Feedback where employee_id = '$userid'";
    $query = $this->db->query ( $sql );
    foreach ( $query->result_array () as $row ) {
        array_push ( $result, $row );
    }
    return $result; 
}

Below is the my Form (Client Side View):

<table class="table">       
     <?php foreach($form_data_supervisee as $question) {?>
     <tr>      
         <td>
         <?php echo $question['ID'].". ".$question['DESCRIPTION']?>
         </td>
    </tr>   
         <?php foreach($form_answers_supervisee as $answer) {?>                 
    <tr>   
          <td>
          <textarea rows="5" name="<?php echo $answer['ANSWER']?></textarea>
          </td> 
    </tr>   
    <?php } ?>  
    <?php }?> 
  </table>

Here is the part of my controller:

 $data['form_answers_supervisee'] = $this->appraisal_model->get_answers_supervisee();
     $data['form_answers_supervisor'] = $this->appraisal_model->get_answers_supervisor();

     $this->load->view('main_form',$data);

Now I am getting the following output for employee having employee_id: 100

1. How was your day?
   That was awesome. 
   That was excellent.
   That was good.

2. How was the task?
   That was awesome. 
   That was excellent.
   That was good.

3. How was the Project?
   That was awesome. 
   That was excellent.
   That was good.

The required output should be:

1. How was your day?
   That was awesome. 

2. How was the task?
   That was excellent.

3. How was the Project?
   That was good.

What correction do I need here? What am I doing wrong?

Suggestions are highly appreciated.


Solution

  • Hope this will help you :

    You have to add a join query based on questions table with feedback table like this :

    function get_answers_supervisee () 
    {
        $employee_id = $this->session->userdata('userid');
        $this->db->select('q.id , q.question,f.answer ,f.employee_id');
        $this->db->from('questions q');
        $this->db->join('feedback f','f.question_id = q.id');
        $this->db->where('f.employee_id', $employee_id);
        $result = $this->db->get()->result();
        return $result;
        //print_r($result);
    }
    

    The output looks like :

    id  question                 answer           employee_id
    1   How was your day?     That was awesome.     100
    2   How was the task?     That was excellent.   100
    3   How was the Project?  That was good.        100
    

    Your view access table column like this :

    <table class="table">       
         <?php foreach($form_data_supervisee as $item) {?>
         <tr>      
             <td>
             <?php echo $item->id;?>
             </td>
             <td>
             <?php echo $item->quesion;?>
             </td>
             <td>
             <?php echo $item->answer;?>
             </td>
          </tr>  
    
        <?php }?> 
    </table>