簡體   English   中英

使用循環從PHP中的數據庫中獲取數據

[英]Fetching the data from the database in PHP using loop

我想在數據庫的視圖中顯示問題及其相應的答案:

我已經以以下形式獲得了名為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.

還有另一個名為Questions的表:

id |      Question        |
_______________________________________________

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

這是我模型的代碼:

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

以下是我的表單(客戶端視圖):

<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>

這是我的控制器的一部分:

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

現在,對於具有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.

所需的輸出應為:

1. How was your day?
   That was awesome. 

2. How was the task?
   That was excellent.

3. How was the Project?
   That was good.

我在這里需要什么更正? 我究竟做錯了什么?

建議非常贊賞。

嘗試以下

您的模型

function get_answers_supervisee () 
{
    $userid = $this->session->userdata('userid');

    $query = $this->db
        ->select('f.*, q.id AS question_id, q.Question')
        ->from('Feedback f')
        ->join('Questions q', 'f.question_id = q.id', 'left')
        ->where('employee_id', $userid)
        ->get();

    return $query->result();
}

控制器應保持不變。

您的看法

<table class="table">       
     <?php 
     foreach($form_data_supervisee as $question) {
     ?>
     <tr>      
         <td>
         <p><?php echo $question->question_id.". ".$question->Question; ?></p>
         <p>Answer: <?php echo $question->answer; ?></p>
         </td>
    </tr>   
    <?php 
    }
    ?> 
</table>

作為附加說明:

您的查詢對SQL注入完全開放-您確實應該出於這些目的和自己的利益使用內置的QueryBuilder。

在此處查看https://www.codeigniter.com/userguide3/database/configuration.html#query-builder並激活查詢構建器(如果尚未安裝)。

只需在視圖中添加一條if語句即可根據問題輸出答案。

 <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>   
          <!-- check "id" in  "questions" table matches the "question_id" in "feedback" table --> 
          <?php if($question['ID'] == $answer['question_id']){ ?>
              <td>
                    <textarea rows="5" name="<?php echo $answer['ANSWER']?></textarea>
              </td>
          <?php } ?> 
    </tr>   
    <?php } ?>  
    <?php }?> 
  </table>

希望這個能對您有所幫助 :

你必須添加一個join基於查詢questions與表feedback這樣的表:

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

輸出如下:

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

您的view訪問表列如下所示:

<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>   

你好,你的查詢必須基於question_id

    <?php 

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

    ?> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM