表中的重复答案

Hi everyone i am working on a small beginner project currently i am working on displaying two separate table data on a web page in HTML table but i am having some problems with the data. There is a credentials table and an answers table a user first enters its credentials and then presses the button take survay which stores the credentials on to a table and redirects the user to a question page where he picks a multiple choice question and the users choices are saved to a table. all this data then is displayed on web page but when the data is displayed the answers get duplicated fore every user that enter its details. can someone tell me where have i gone wrong ? tnx for your help in advance.

view

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

controller

function getall(){

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

    }

model

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

}

You are selecting everything from tblanswers, credentials without joining on any column. You're ending up with a cross join, which pairs each row with every row from the other table. You need to specify which rows from tblanswers relate to which rows from credentials. E.g. if there is an id column in credentials then you need to create a credentials_id column in tblanswers and enter the data so that they match. Your getall() should look something like this:

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

}

The line which says

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

specifies how the two tables relate.

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.