代码之家  ›  专栏  ›  技术社区  ›  Dixon Chaudhary

使用循环从PHP数据库中获取数据

  •  0
  • Dixon Chaudhary  · 技术社区  · 8 年前

    我想在数据库视图中显示问题及其各自的答案:

    我有一个名为 反馈 以下列形式:

    这是我的表名: 反馈

    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.
    

    和另一个名为 问题 :

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

    现在我得到以下输出 员工编号: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.
    

    我这里需要什么更正?我做错什么了?

    我们非常感谢你的建议。

    4 回复  |  直到 8 年前
        1
  •  1
  •   Pradeep    8 年前

    希望这对你有帮助:

    你必须添加一个 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>   
    
        2
  •  2
  •   Atural    8 年前

    尝试以下操作

    你的模特

    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 并激活查询生成器(如果尚未激活)。

        3
  •  1
  •   Abdul Rehman Mehar    8 年前

    只需在视图中添加一个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>
    
        4
  •  0
  •   Afshin    8 年前

    您好,您的查询必须基于问题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; 
        }
    
        ?> 
    
    推荐文章