【问题标题】:Displaying relational tables in CodeIgniter with simple way在 CodeIgniter 中以简单的方式显示关系表
【发布时间】:2019-09-07 19:59:12
【问题描述】:

我只是想知道如何在 CI 中显示关系表:

客户表如下:

+----+------+
| id | name |
+----+------+
|  1 | John |
|  2 | Jane |
+----+------+

还有像这样的水果

 +----+----------+------------+
 | id | customer_id | fruit |
 +----+----------+------------+
 |  1 |        1 | Apple      |
 |  2 |        1 | Grape      |
 |  3 |        1 | Banana     |
 |  4 |        2 | Pear       |
 |  5 |        2 | Mango      |
 +----+----------+------------+

我想要这样的结果:

  +------+------------+
  | John | : • Apple  |
  |      |   • Grape  |
  +      +   • Banana +
  |      |            |
  + Jane + : • Pear   +
  |      |   • Mango  |
  +------+------------+

在 CI 中有什么简单的方法可以做到这一点吗?

【问题讨论】:

    标签: php mysql sql codeigniter


    【解决方案1】:

    您可以使用连接,例如:

    $this->db->select('*');
    $this->db->from('customer'); 
    $this->db->join('fruits', 'customer.id = fruits.customer_id','LEFT');
    
    $query = $this->db->get();
    $result = $query->result();
    
    print_r($result);
    

    【讨论】:

    • 是否可以用一个 foreach 完成我的要求?
    • 如果需要显示水果项,则需要第二个子 foreach
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2017-11-03
    • 2021-12-05
    • 2011-12-17
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多