0

大家好,我正在从事一个小型初学者项目,目前我正在努力在 HTML 表格的网页上显示两个单独的表格数据,但我遇到了一些数据问题。有一个凭据表和一个答案表,用户首先输入其凭据,然后按下按钮获取 survay,该按钮将凭据存储到表中并将用户重定向到问题页面,在该页面中他选择了一个多项选择问题,用户的选择是保存到表中。然后所有这些数据都显示在网页上,但是当显示数据时,每个输入其详细信息的用户的答案都会重复。有人可以告诉我哪里出错了吗?tnx 提前为您提供帮助。

看法

 <table border="1">


  <tr>
     <th>Name</th>
     <th>Second Name</th>
     <th>Phone</th>
     <th>Email</th>
     <th>Answer</th>
     <th>Comment</th>
 </tr>
  <?php foreach ($query as $row): ?> 
 <tr>

     <td><?php echo $row->name; ?></td>
     <td><?php echo $row->second_name; ?></td>
     <td><?php echo $row->phone; ?></td>
     <td><?php echo $row->email; ?></td>
      <td> <?php echo $row->answerA;?>
      <?php echo $row->answerB;?>
      <?php echo $row->answerC;?></td>
     <td><?php echo $row->comment;?><br></td>

 </tr>

  <?php endforeach; ?>

 </table>

控制器

function getall(){

    $this->load->model('result_model');
    $data['query'] = $this->result_model->result_getall();
    $this->load->view('result_view', $data);

    }

模型

function result_getall(){
  return $this->db->select('tblanswers.*,credentials.*')
                 ->from('tblanswers, credentials')
                 ->get()
                 ->result_object();

}
4

1 回答 1

4

您正在从 tblanswers、凭据中选择所有内容,而无需加入任何列。您最终会得到一个交叉连接,它将每一行与另一个表中的每一行配对。您需要指定 tblanswers 中的哪些行与凭证中的哪些行相关。例如,如果凭据中有一个 id 列,那么您需要在 tblanswers 中创建一个 credentials_id 列并输入数据以便它们匹配。您的 getall() 应如下所示:

function result_getall(){
  return $this->db->select('tblanswers.*,credentials.*')
                 ->from('tblanswers')
                 ->join('credentials', 'tblanswers.credentials_id = credentials.id')
                 ->get()
                 ->result_object();

}

说的那一行

join('credentials', 'tblanswers.credentials_id = credentials.id')

指定两个表如何关联。

Suppose Mr Jones enters his credentials and they are saved in a row in the credentials table with id 72. Then he enters his answers. You need to make sure that when you save them, you enter "72" in the credentials_id column of the answers table. That signifies that these answers belong to Mr Jones. When you run the query with the join specified above, it will return Mr Jones' credentials against Mr Jones' answers.

于 2013-03-05T11:16:27.977 回答