doumianfeng5065 2013-03-05 11:04 采纳率: 0%
浏览 7
已采纳

表中的重复答案

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

}
  • 写回答

1条回答 默认 最新

  • douyanzhou1450 2013-03-05 11:16
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题