这是我的数据库:

表名称:user_account

   Account_no       Firstname       Lastname       Username       Password
      1              Larry            Bird         larryB         larrylarry
      2              Magic           Johnson       magic         magiclakers


表名称:tbl_items

   Item_ID         Item_Name       Quantity       Price       Directory
     1              gown              5           1000         gown.jpg
     2              bridal            3           1500         bridal.png


表名称:tbl_item_availed

 Item_availed_ID   Item_ID        Account_no     Date_reserved   Quantity
     1               1               2              9/14/2016        2
     2               2               1              9/14/2016        1
     3               1               2              9/14/2016        1


是的,这是我的桌子,我已经加入了它们3,我的问题是这样的:

客户预订视图:

   CLIENT       DETAILS
   Magic        details
   Bird         details
   Magic        details


所以我只希望它像这样:

   CLIENT       DETAILS
   Magic        details
   Bird         details


我已经知道了详细信息区域,唯一的是,CLIENT的名称也很宽,我希望它只能是一个,我该怎么做?这是我的控制器代码:

   <div id="page-wrapper">
   <table class="table table-hover">
    <tr>
        <th>CLIENT</th>
        <th>DETAILS</th>
    </tr>
    <?php foreach($posts as $post) { ?>
    <tr>
        <td><?php echo $post->Firstname ?> <?php echo $post->Lastname; ?> </td>
        <td><a href="<?php echo base_url(); ?>index.php/Pages_Controller/show_receipt/<?php echo $post->Username; ?>">details</a></td>
    </tr>
    <?php } ?>
</table>


  

以“模型”形式请求的查询:

public function getServicesAvailed($id){
    $this->db->select('*');
    $this->db->from("tbl_service_type");
    $this->db->join("tbl_service_availed","tbl_service_availed.Service_ID = tbl_service_type.Service_ID");
    $this->db->where('Account_ID',$id);
    $this->db->where('active',1);
    $query = $this->db->get();
    return $query->result();
}

最佳答案

使用group_by并按如下所示修改代码

public function getServicesAvailed($id){
    $this->db->select('*');
    $this->db->from("tbl_service_type");
    $this->db->join("tbl_service_availed","tbl_service_availed.Service_ID = tbl_service_type.Service_ID");
    $this->db->where('Account_ID',$id);
    $this->db->where('active',1);
    $this->db->group_by('tbl_item_availed.user_id');//add this line
    $query = $this->db->get();
    return $query->result();
}

07-27 19:26